CENG 360 Operating systems and system programming Lecture

  • Slides: 40
Download presentation
CENG 360 - Operating systems and system programming Lecture 6 Windows XP

CENG 360 - Operating systems and system programming Lecture 6 Windows XP

Operating system essentials (review) OS features can be described by considering the most important

Operating system essentials (review) OS features can be described by considering the most important resources that a modern OS manages: • • • Memory: The OS manages a large, flat, virtual memory address space and transparently moves information between physical memory and disk and other secondary storage. File systems: The OS manages a named file space and provides both direct and sequential access as well as directory and file management. Resource naming and location: File naming allows for long, descriptive names, and the naming scheme is extended to objects such as devices, synchronization, and interprocess communication objects. Multitasking: The OS must manage processes, threads, and other units of independent, asynchronous execution. Tasks can be preempted and scheduled according to dynamically determined priorities. Communication and synchronization: The OS manages task-to-task communication and synchronization within single systems as well as communication between networked systems and with the Internet. Security and protection: The OS provides flexible mechanisms to protect resources from unauthorized and accidental access and corruption.

Windows overview • The original Microsoft Operating System was MS-DOS, in the 1980's. This

Windows overview • The original Microsoft Operating System was MS-DOS, in the 1980's. This was a simple OS with no security, no multiprocessing, and a command line interface. • Apple was making inroads into Microsoft's business with its GUI based systems, and so Microsoft put a GUI on top of MS-DOS. Windows 3. 0 came out in 1990, Windows 3. 1 (a dramatic improvement) in 1992. But all programs ran in the same address space. • Windows 95 (1995) contained most of the features of modern operating systems including virtual memory, process management, and multiprogramming. But there was still no security. Windows 95 was a commercial success, in spite of the fact that it crashed a lot. • By this time Microsoft had figured out that they could not just keep adding more features to MS-DOS, so they decided to build a new operating system from scratch. The result was Windows NT (New Technology). The API system was called WIN 32. • Microsoft operating systems have to be backward compatible; that is, all of the applications that ran on the old Windows systems should still run on newer systems. This requires another layer in the API software to deal with this.

Windows overview • Windows 2000 introduced Plug-and-Play (Pn. P) which allowed users to install

Windows overview • Windows 2000 introduced Plug-and-Play (Pn. P) which allowed users to install new devices on the fly. Prior to this, users would sometimes have to manually set jumper switches or dip switches, and occasionally two (or more) devices would use the same interrupt handler address, and so one interrupt would trash the other. • The original release of NT was about 3 million lines of C/C++ code. Windows Vista is more than 70 million lines of code. Part of this is due to the need for backward compatibility, nothing ever gets deleted, but most of the bloat is due to the addition of new features.

Windows overview • There are multiple layers of system calls. At the core is

Windows overview • There are multiple layers of system calls. At the core is the NT kernel ntoskrnl. exe, which has traditional system call interfaces, this is only used by Microsoft programmers. There a number of subsystems that run on top of NTOS. The only subsystem that we will use is Win 32. • Windows can run on a number of different hardware architectures, not only different processors, but different support chipsets. In order to minimize the amount of hardware specific programming, the lowest layer of the OS is the Hardware Abstraction Layer (HAL). This layer translates all calls which deal directly with hardware into hardware accesses. Examples include the CPU itself, the Memory Management Unit, Interrupt controllers, physical devices, and the BIOS.

Windows: the registry • The registry is a special file system, (optimized for small

Windows: the registry • The registry is a special file system, (optimized for small files) attached to the NT namespace. The registry is organized into separate volumes called hives, which are like files. One of these hives is called SYSTEM. This is a file which keeps track of the system configuration. Other hives keep track of the configuration of installed software etc. Here is a list of some of the hives. • In Unix systems, generally each program consults a plain text resource file when it is opened. This contains all of the configuration information. Example: . bashrc

Windows APIs • In the Microsoft world, a system call is referred to as

Windows APIs • In the Microsoft world, a system call is referred to as an Win 32 API (Application Program Interface) (or a WIN 64 API). If you use any of these, you should include the header file windows. h. This file contains definitions, macros, and structures for source code. As in Unix, the Win 32 APIs have the same form as C function calls. • The number of Win 32 APIs is much larger than the number of Unix system calls. One of the major reason why the number is so much larger is that all of the graphics calls which create and manipulate windows are part of the Win 32 APIs. • All versions of the Windows operating systems use the Win 32 APIs, but each uses a slightly different subset. • The Windows API is different in many ways from the Linux POSIX (Portable Operating System Interface) API – Many system resources are represented as a kernel object identified and referenced by a handle (file descriptors and process IDs in Linux) – Kernel objects must be manipulated by Windows APIs – Objects include files, processes, threads, memory mapping, events, and many more – The Windows thread is the basic unit of execution. A process can contain one or more threads

Windows: Hungarian notation • To understand Microsoft C/C++ documentation or Microsoft sample code, you

Windows: Hungarian notation • To understand Microsoft C/C++ documentation or Microsoft sample code, you will need to understand Hungarian Notation. This is a method of naming variables (Microsoft convention). • For example, the online help which describes the function Read. File looks like this: BOOL Read. File( HANDLE h. File, LPVOID lp. Buffer, DWORD n. Number. Of. Bytes. To. Read, LPDWORD lp. Number. Of. Bytes. Read, LPOVERLAPPED lp. Overlapped ); • A variable name consists of an optional prefix, a tag which indicates the variable type, and the variable name. The prefix is lower case, the variable name itself starts with an upper case letter.

Windows: Hungarian notation Most Microsoft sample code includes the header file <windows. h> which

Windows: Hungarian notation Most Microsoft sample code includes the header file <windows. h> which redefines most data types. • • DWORD unsigned long (stands for double word) WORD unsigned short (16 bits) BOOL boolean (Possible values are TRUE and FALSE) BYTE unsigned char LPDWORD pointer to a DWORD LPVOID Pointer to type void (a generic pointer) LPCTSTR Pointer to a const string

Windows: file system Windows supports four file systems on directly attached devices 1. The

Windows: file system Windows supports four file systems on directly attached devices 1. The NT file system (NTFS) is a modern file system supporting long file names, security, fault tolerance, encryption, compression, extended attributes, and support for very large files and volumes. 2. The File Allocation Table (FAT and FAT 32) file systems descend from the original MS-DOS and Windows 3. 1 FAT (or FAT 16) file systems. FAT does not support Windows security, among other limitations. FAT is increasingly obsolete and is most frequently seen on older systems 3. The CD-ROM file system (CDFS), as the name implies, is for accessing information provided on CD-ROMs. 4. The Universal Disk Format (UDF) supports DVD drives and will ultimately supplant CDFS. The format of a file system (FAT, NTFS), as a disk volume or partition, is determined when a disk is partitioned

Windows: file naming • • • The full pathname of a disk file starts

Windows: file naming • • • The full pathname of a disk file starts with a drive name, such as A: or C: . Alternatively, a full pathname, or Universal Naming Code (UNC), can start with a double backslash (\), indicating the global root, followed by a server name and a share name to indicate a path on a network file server. The first part of the pathname, then, is \servernamesharename. The pathname separator is the backslash (), although the forward slash (/) can be used in API parameters, which is more convenient in C. Directory and file names cannot contain any of the ASCII characters with values in the range 131 or any of these characters: < > : " | ? * / Names can contain blanks. However, when using file names with blanks on a command line, be sure to put each file name in quotes so that the name is not interpreted as naming two distinct files. Directory and file names are case-insensitive, but they are also case-retaining, so that if the creation name is My. File, the file name will show up as it was created, but the file can also be accessed with the name my. FILE. File and directory names can be as many as 255 characters long, and pathnames are limited to MAX_PATH characters (currently 260). A period (. ) separates a file's name from its extension, and extensions (usually two or three characters after the rightmost period in the file name) conventionally indicate the file's type. Thus, atou. EXE would be an executable file, and atou. C would be a C language source file. File names can contain multiple periods. A single period (. ) and two periods (. . ), as directory names, indicate the current directory and its parent, respectively.

Windows: batch files • Batch files allow Microsoft Windows users to create a lists

Windows: batch files • Batch files allow Microsoft Windows users to create a lists of commands to run in sequence. • A good example of a batch file for someone who is more familiar with Windows is to think of a batch file as a shortcut. Much like a shortcut, batch files could be used to run one or more commands and/or programs through the command line. • To create a batch file do the following: o open a Command Prompt window o Type edit b. bat (we are creating a batch file named b. bat) o Type in the following lines: @echo off echo Hello this is a test batch file pause o Save and close the file o To run the file, type in the file name

Windows: batch files • Just like all MS-DOS commands, all batch file commands are

Windows: batch files • Just like all MS-DOS commands, all batch file commands are not case sensitive. %1 The percent followed by a numeric value, allows users to receive arguments in a batch file. : : A way to add remarks in the batch file : LABEL By adding a colon in front of a word, such as LABEL, you create a label. This allows you to skip to certain sections of a batch file. See GOTO CALL This is used to run another batch file within a batch file. When the batch file that is called is completed, the remainder of the original batch file is completed. Note if the batch file does not exist it will give an error message CLS Clear the screen ECHO Will echo a message in the batch file. However, without @ECHO OFF at the beginning of the batch file you'll also get each line of the batch file. Type ECHO. To create an empty line.

Windows: batch files EXIT Exits out of the DOS window if the batch file

Windows: batch files EXIT Exits out of the DOS window if the batch file is running from Windows. GOTO LABEL Used to go to a certain label IF Used to check for a certain condition if the condition exists PAUSE Prompt the user to press any key to continue. START Used to start an application SET [variable=[string]] Specifies the environment-variable name and value You can type SET to display environment variables. /P to prompt the user to enter a value Environment variable substitution can be as follow: %PATH: ~0, -2% extract all but the last 2 characters of the PATH variable. %PATH: ~-10% extract the last 10 characters of the PATH variable. %PATH: str 1=str 2% would expand the PATH environment variable, substituting each occurrence of str 1 in the expanded result with str 2 To get help for any of the batch commands: type help followed by the command. If you type help only, you will get the list of commands (but not all)

Batch file: example 1 @ECHO off cls : start ECHO 1. Print Hello ECHO

Batch file: example 1 @ECHO off cls : start ECHO 1. Print Hello ECHO 2. Print Bye ECHO 3. Print Test set choice= set /p choice=Type the number to print text: . if not '%choice%'=='' set choice=%choice: ~0, 1% if '%choice%'=='1' goto hello if '%choice%'=='2' goto bye if '%choice%'=='3' goto test You should know: To terminate a job Ctrl+C ECHO "%choice%" is not valid please try again ECHO. goto start : hello ECHO HELLO goto end : bye ECHO BYE goto end : test ECHO TEST goto end : end

Windows: batch files For loop FOR variable IN (argument list) DO command Example: FOR

Windows: batch files For loop FOR variable IN (argument list) DO command Example: FOR %%a IN (Andrew Bob Carol Daisy Ellen) DO echo %%a FOR %%a IN (*. txt) DO echo %%a You should know: To run the Windows calculator use the command calc To open a new Command prompt window type start cmd taskkill and tasklist are important commands try them with care

Batch file: exercise 1 In this exercise you are requested to create a batch

Batch file: exercise 1 In this exercise you are requested to create a batch file similar to question 4 of homework 1 Build a batch file that does the following: Give a menu and wait for an input 1. 2. 3. 4. Launch a calculator Launch a terminal window Close the calculator Exit

Exercise 1 - solution @echo off : start cls echo "Please select one of

Exercise 1 - solution @echo off : start cls echo "Please select one of the following options: " echo " 1 - Launch a calculator" echo " 2 - Launch a terminal" echo " 3 - close calculator" echo " 4 - exit" set /P choice="Your choice is: " if '%choice%' == '1' goto launchcalc if '%choice%' == '2' goto launchterminal if '%choice%' == '3' goto closecalc if '%choice%' == '4' goto end echo "Bad selection: goto start : launchcalc start calc goto start : launchterminal start cmd goto start : closecalc taskkill /IM calc. exe goto start : end

Windows: commands • To view the important commands type help ASSOC Displays or modifies

Windows: commands • To view the important commands type help ASSOC Displays or modifies file extension associations Exercise : See FTYPE! AT Schedules commands and programs to run on a computer ATTRIB Displays or changes file attributes Exercise: type help to understand attributes in Windows. BREAK Sets or clears extended CTRL+C checking CACLS Displays or modifies access control lists (ACLs) of files Exercise: What is the equivalence in Linux CALL Calls one batch program from another CD Displays the name of or changes the current directory CHCP Displays or sets the active code page number Exercise: what is the current active page number? What is page number? Create a file, output it on screen, change code page, output the file… CHDIR Displays the name of or changes the current directory

Windows: commands CHKDSK Checks a disk and displays a status report CHKNTFS Displays or

Windows: commands CHKDSK Checks a disk and displays a status report CHKNTFS Displays or modifies the checking of disk at boot time CLS Clears the screen CMD Starts a new instance of the Windows command interpreter COLOR Sets the default console foreground and background colors Exercise: Set the background to Blue and the foreground to yellow COMP Compares the contents of two files or sets of files Exercise: Create a file , copy it, slightly change the second and discover the compare options. COMPACT Displays or alters the compression of files on NTFS partitions CONVERT Converts FAT volumes to NTFS. You cannot convert the current drive COPY Copies one or more files to another location Exercise: find out how to copy a directory! DATE Displays or sets the date

Windows: commands DEL Deletes one or more files Exercise: find out how to add

Windows: commands DEL Deletes one or more files Exercise: find out how to add Aliases and create one so that del always ask for confirmation before deletion DIR Displays a list of files and subdirectories in a directory Like ls in Linux. DISKCOMP Compares the contents of two floppy disks DISKCOPY Copies the contents of one floppy disk to another DOSKEY Edits command lines, recalls Windows commands, and creates macros Exercise: help to understand doskey F 7 = ? ENDLOCAL Ends localization of environment changes in a batch file EXIT Quits the CMD. EXE program (command interpreter). FC Compares two files or sets of files Exercise: what is the difference with COMP? FIND Searches for a text string in a file or files FINDSTR Searches for strings in files

Windows: commands FOR Runs a specified command for each file in a set of

Windows: commands FOR Runs a specified command for each file in a set of files FORMAT Formats a disk for use with Windows. FTYPE Displays or modifies file types used in file extension associations Exercise: type FTYPE and see the association. IF Performs conditional processing in batch programs. MD/MKDIR Creates a directory MODE Configures a system device. MORE Displays output one screen at a time. MOVE Moves one or more files from one directory to another directory PATH Displays or sets a search path for executable files. Exercise: where is path? How to update? PRINT Prints a text file PROMPT Changes the Windows command prompt Exercise: type prompt $p and see what happen? Now fix it!

Windows: commands PUSHD Saves the current directory then changes it RD Removes a directory

Windows: commands PUSHD Saves the current directory then changes it RD Removes a directory RECOVER Recovers readable information from a bad or defective disk REN Renames a file or files. You can also use RENAME REPLACE Replaces files RMDIR Removes a directory SET Displays, sets, or removes Windows environment variables Exercise: display all the variables. Learn how to modify and how to delete SETLOCAL Begins localization of environment changes in a batch file SHIFT Shifts the position of replaceable parameters in batch files SORT Sorts input

Windows: commands START Starts a separate window to run a specified program or command

Windows: commands START Starts a separate window to run a specified program or command Exercise: try start cmd SUBST Associates a path with a drive letter Exercise: test it. TIME Displays or sets the system time TITLE Sets the window title for a CMD. EXE session Exercise: try it TREE Graphically displays the directory structure of a drive or path Exercise : try it TYPE Displays the contents of a text file VER Displays the Windows version. Try it then try winver VERIFY Tells Windows whether to verify that your files are written correctly to a disk VOL Displays a disk volume label and serial number

Windows: code pages 437 — The original IBM PC code page 737 — Greek

Windows: code pages 437 — The original IBM PC code page 737 — Greek 775 — Estonian, Lithuanian and Latvian 850 — "Multilingual (Latin-1)" (Western European languages) 852 — "Slavic (Latin-2)" (Central and Eastern European languages) 855 — Cyrillic 857 — Turkish 858 — "Multilingual" with euro symbol 860 — Portuguese 861 — Icelandic 862 — Hebrew 863 — French Canadian 865 — Nordic 866 — Cyrillic 869 — Greek

Updating registery Environment variables are saved in: HKEY_LOCAL_MACHINESYSTEMCurrent. Control. SetSession ManagerEnvironment HKEY_CURRENT_USEREnvironment HKEY_USERS. DEFAULTEnvironment

Updating registery Environment variables are saved in: HKEY_LOCAL_MACHINESYSTEMCurrent. Control. SetSession ManagerEnvironment HKEY_CURRENT_USEREnvironment HKEY_USERS. DEFAULTEnvironment • To edit the registry type regedit • Browse but don’t modify… Close the registry • Check the command reg – By typing reg /? – Note that this is sometimes an alternative to get help. • Now write a script (batch file) to add the environment variable for the current user CENG 360 = Operating Systems and System Programming

Solution reg ADD HKEY_CURRENT_USEREnvironment /v CENG 360 /t REG_EXPAND_SZ /d “Operating Systems and System

Solution reg ADD HKEY_CURRENT_USEREnvironment /v CENG 360 /t REG_EXPAND_SZ /d “Operating Systems and System Programming” • You need to logout / login so that changes take effect • To test, open a cmd window and type echo %ceng 360%

Aliases To add an alias we use the DOSKEY command Example: doskey ls=dir Equivalent

Aliases To add an alias we use the DOSKEY command Example: doskey ls=dir Equivalent System call commands: Add. Console. Alias(TEXT(“ls”), TEXT(“dir"), TEXT(“cmd. exe”)); • Parameters are: source / target / executable name • TEXT is used to avoid ASCII/Unicode errors other. Otherwise, we need to use Add. Console. Alias. A. • See also: Get. Console. Alias, Get. Console. Aliases, Get. Console. Alias. Exes

Permanent Aliases What we learned about aliases works only in the current CMD environment.

Permanent Aliases What we learned about aliases works only in the current CMD environment. In order to create an alias that will be available everywhere and every time you need to update the registry. • Goto HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrent. VersionApp Paths • Create a subkey with the name of the alias ex: ls. exe • In this subkey, set the default value to the alias equivalence ex: “c: windowssystem 32notepad. exe” • To test, goto start / run / and type ls

Net command • Type net /? to see the help of this powerful command

Net command • Type net /? to see the help of this powerful command • We will focus on – Net use – Net view – Net share • Type net view to see all computers on the LAN • Go to hosting computer and share a folder. (find out how) • Go to XP and find out how to view the shared folder

Net share command • Net share – Allows you to see the shared folders

Net share command • Net share – Allows you to see the shared folders on your computer. – Type net share /? To see the help of this command – Type net share to see all the shares – On the hosting computer, create the directory d: share and share it under the name of hostshare – There is also a GUI way to share (right click on the folder / properties) What does a $ means at the end of a share? You will find out later

Net view command • Net view – Allows you to view connected computers –

Net view command • Net view – Allows you to view connected computers – Type net view /? To see the help of this command – Type net view to see the connected computers

 • Net use command – Allows you to connect to shared resources –

• Net use command – Allows you to connect to shared resources – Type net use /? To see the help of this command – Type net use to see the resources that are shared – Use this command to connect to the shared folder – Go to k: and list the files

Some other system calls BOOL Copy. File (LPCTSTR lp. Existing. File. Name, LPCTSTR lp.

Some other system calls BOOL Copy. File (LPCTSTR lp. Existing. File. Name, LPCTSTR lp. New. File. Name, BOOL f. Fail. If. Exists) BOOL Delete. File (LPCTSTR lp. File. Name) BOOL Create. Hard. Link ( LPCTSTR lp. File. Name, LPCTSTR lp. Existing. File. Name, BOOL lp. Security. Attributes) • Chapter 2, Windows system programming. • When f. Fail. If. Exists = true, the copy will fail if the destination file exists. • With a hard link, a file can have two separate names but there is only one file. (a Linux option) so a change to the file will be available regardless of which name was used to open the file. • Check fsutil command especially “fsutil hardlink create …” • Test hardlink by creating a file and creating a hard link of the file.

Some other system calls BOOL Move. File ( LPCTSTR lp. Existing. File. Name, LPCTSTR

Some other system calls BOOL Move. File ( LPCTSTR lp. Existing. File. Name, LPCTSTR lp. New. File. Name) BOOL Move. File. Ex ( LPCTSTR lp. Existing. File. Name, LPCTSTR lp. New. File. Name, DWORD dw. Flags) • Move. File fails if the new file already exists • Move. File. Ex should be used to move even if the destination exists. • Note that the Ex suffix is commonly used to create an enhanced version of an existing function in order to provide additional functionality.

Some other system calls BOOL Create. Directory ( LPCTSTR lp. Path. Name, LPSECURITY_ATTRIBUTES lp.

Some other system calls BOOL Create. Directory ( LPCTSTR lp. Path. Name, LPSECURITY_ATTRIBUTES lp. Security. Attributes) BOOL Remove. Directory (LPCTSTR lp. Path. Name) BOOL Set. Current. Directory (LPCTSTR lp. Path. Name) DWORD Get. Current. Directory (DWORD cch. Cur. Dir, LPTSTR lp. Cur. Dir) • All lp. Path. Name are absolute or from the current directory. • lp. Path. Name points to a null-terminated string with the name of the directory that is to be created or deleted. • The security attributes should be NULL for the time being • Only an empty directory can be removed.

Exercise Write a program cpcr that receives as argument a file and perform the

Exercise Write a program cpcr that receives as argument a file and perform the following: a) If the file exists, creates a hard link of the file in the same directory using the same file name but prefixed with a number starting by 1 b) If the file does not exist, create an empty file c) The program should provide help when the option is /? Example: cpcr test. c cprc /? ₋ if test. c exists, the system should create the new file test_1. c, if test_1. c exists, it should create test_2. c … ₋ if test. c does not exist, the system should create the empty file test. c For help NOTES: To compare use _tcscmp, to get the length use _tcslen To write a string in a generic format use _T. _T Example: _T(“/? ”) To copy a string use _tcscpy, _tcscpy to concatenate use _tcscat

Exercise solution #include <windows. h> #include <tchar. h> #include <stdio. h> void usage() {

Exercise solution #include <windows. h> #include <tchar. h> #include <stdio. h> void usage() { printf("nnusage: tcpcr filenament-filename can be a new file or an existing onen"); printf("t-if new file, create it as empty filen"); printf("t-if existing create a hard link with the same name prefixed by a numbern"); printf("t-cpcr /? for help"); }

Exercise solution Second part File name int _tmain(int argc, LPTSTR argv[]) First part {

Exercise solution Second part File name int _tmain(int argc, LPTSTR argv[]) First part { File number HANDLE f; TCHAR fp 1[100], fp 2[100], fnb[100]; if (argc != 2 || (argc== 2 && (_tcscmp(argv[1], _T("/? "))==0))) usage(); //Create the file if new: f = Create. File(argv[1], GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL); if (f == INVALID_HANDLE_VALUE) // Existing file { int nb=1, i, j; int s = _tcslen(argv[1]); for (i=0; i<s; i++) //Get the first part of the filename { if (argv[1][i]==_T('. ')) break; fp 1[i]=argv[1][i]; } fp 1[i]=_T('');

Exercise solution } for (j=i; j<s; j++) // Get the second part fp 2[j-i]=argv[1][j];

Exercise solution } for (j=i; j<s; j++) // Get the second part fp 2[j-i]=argv[1][j]; fp 2[j-i]=_T(''); bool ok = false; while(!ok) { _tcscpy(fn, _T("")); _tcscat(fn, fp 1); // Build the file name s = _tcslen(fn); fn[s]=_T('_'); // Add _ fn[s+1]=_T(''); _itot(nb, fnb, 100); _tcscat(fn, fnb); // Add the number _tcscat(fn, fp 2); ok = Create. Hard. Link(fn, argv[1], NULL); // Create link if possible nb++; } } return 0;