advanced system-programming exercises in the win32...

52
Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School of Computer Science Raimond and Beverly Sackler Faculty of Exact Sciences Tel-Aviv University 31st August 2004 This web site contains system-programming exercises in the Windows environ- ment. These exercises are designed to serve as programming assignments in “Op- erating Systems” courses in universities and colleges, but they are also appropriate for related courses and for self study. In a university course, these exercises are designed to be assigned at a rate of one per week. The scope of each exercise and the API documentation that each exercise provides allows students with some programming experience in the C lan- guage to complete each assignment in a few hours. Obviously, the amount of effort required to solve each exercise varies with the exercise and with the programming proficiency of the student, but normally, solving an assignment should not take more than a couple of hours. Obviously, not all exercises can be assigned in a sin- gle 13 or 14-week semester. In Tel-Aviv University, we assign between 10 and 12 exercises each semester. We built the exercises according to several principles: Computer-science graduates (and programmers in general) should be famil- iar with the entire range of services that the operating system provides to programs. This familiarity helps students understand the structure of the op- erating system and its components, and improves their programming skills. Therefore, the exercises cover the entire range of operating-system services, even though they do not cover each API call (Windows supports hundreds of system calls, so covering all of them is impractical). The best way to cover such a large amount of material is using a series of rel- atively small programming assignments. Small weekly programming assign- ments allow students to focus on one operating-system service type in each assignment, they avoid software-engineering issues that always arise in large programs, and they encourage students to work throughout the semester. To allow students to solve each exercise within a few hours, the assignments include most of the API documentation that is required. That is, the system calls and other aspects of the operating-system interface are documented in 1

Upload: buidat

Post on 11-Feb-2018

242 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Advanced System-Programming Exercises in theWin32 Environment

Sivan ToledoSchool of Computer Science

Raimond and Beverly Sackler Faculty of Exact SciencesTel-Aviv University

31st August 2004

This web site contains system-programming exercises in the Windows environ-ment. These exercises are designed to serve as programming assignments in “Op-erating Systems” courses in universities and colleges, but they are also appropriatefor related courses and for self study.

In a university course, these exercises are designed to be assigned at a rate ofone per week. The scope of each exercise and the API documentation that eachexercise provides allows students with some programming experience in the C lan-guage to complete each assignment in a few hours. Obviously, the amount of effortrequired to solve each exercise varies with the exercise and with the programmingproficiency of the student, but normally, solving an assignment should not takemore than a couple of hours. Obviously, not all exercises can be assigned in a sin-gle 13 or 14-week semester. In Tel-Aviv University, we assign between 10 and 12exercises each semester.

We built the exercises according to several principles:

• Computer-science graduates (and programmers in general) should be famil-iar with the entire range of services that the operating system provides toprograms. This familiarity helps students understand the structure of the op-erating system and its components, and improves their programming skills.Therefore, the exercises cover the entire range of operating-system services,even though they do not cover each API call (Windows supports hundreds ofsystem calls, so covering all of them is impractical).

• The best way to cover such a large amount of material is using a series of rel-atively small programming assignments. Small weekly programming assign-ments allow students to focus on one operating-system service type in eachassignment, they avoid software-engineering issues that always arise in largeprograms, and they encourage students to work throughout the semester.

• To allow students to solve each exercise within a few hours, the assignmentsinclude most of the API documentation that is required. That is, the systemcalls and other aspects of the operating-system interface are documented in

1

Page 2: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

the text of the assignment. This documentation allows students to quicklylearn the relevant part of the API.

• Being able to browse and understand the API documentation is an imporantskill for programmers. To help students acquire this skill, the assignmentssometimes do not include all the required documentation. The students areencouraged to browse the online documentation and to find out the missingdetails. Under windows, this documentation is part of the Microsoft Devel-oper Network (MSDN), which is available online at msdn.microsoft.com. Thedocumentation is also usually installed as part of the development tools.

While developing these exercises, we also implemented solution programs to all theexercises. We will be happy to distribute these solutions to teachers who use theseexercises in their courses. We do not make these solusions available to students,and we ask other teachers not to distribute our solutions to students.

Hebrew-language programming exercises. The exercises are designed to teachWin32 system programming. They are intended for use as homework assignmentsin Operating Systems courses in universities, but they are also suitable for self-study. The exercises were prepared by Sivan Toledo from the School of ComputerScience, Raimond and Beverly faculty of exact sciences, Tel-Aviv University, wherethey were used in Operating-Systems courses.

These teaching materials are also available in a printer-friendly PDF format, aswell as in Hebrew.

These teaching materials were prepared by Sivan Toledo from the school ofComputer Science in Tel-Aviv University, where they were used in Operating-Systems courses. These exercises were prepared with support from a CurriculumDevelopment Grant from Microsoft Research.

2

Page 3: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Basic File Input/Output

This programming exercise shows how to write a simple Windows program andhow to use basic system calls related to file input-output.

Structure of a C program under Windows (ellipsis indicate missing code):

#define UNICODE#define _UNICODE#include <windows.h>#include <tchar.h>...int _tmain(int argc, LPTSTR argv[]){

if (argc < 2) { /* assuming the program needs 1 ar-gument */

_tprintf( _T("usage: %s <arg>\n"),argv[0]);

exit(1); /* stop the program; report error */ }...return 0; /* successful return */

}

Even such a simple program shows two interesting aspects of Win32 programming.The way characters are encoded is the first aspect. A Win32 program can representcharacters using either 8 or 16 bits. When each character is represented by 8 bits,the meaning of each character dependes on an encoding, which is a mapping of in-tegers to characters. In a Hebrew encoding, for example, the number 224 representsthe Hebrew letter Aleph, but in a European encoding, 224 represents an accentedLatin letter. When each characters is represented by 16 bits, the operating systemalways uses an encoding called Unicode. In Unicode, each letter has a distinct rep-resentation, so a single string can contains text in both Hebrew, French, Japanese,and Arabic, for example.

To allow the same source program to be compiled into either an 8-bit programand into a 16-bit program, Microsoft defined a generic data type for characters,TCHAR. When you build a program and the two preprocessor variables UNICODEand _UNICODE, are defined, as in this program, TCHAR stands for a 16-bit datatype. But when the two variables are not defined, TCHAR stands for an 8-bit datatype. These preprocessor variables also control the meaning of the macro _T and oflibrary functions like _tprintf. This program will compile into a 16-bit programthat calls Win32 system calls that expect and return strings of 16-bit characters, butif we remove the first two lines, the program will compile into a valid 8-bit program.

To ensure that your programs work in both 16-bit and 8-bit environment, youneed to follow the following rules:

• The function called by the operating system is _tmain, not main.

• Literal strings must be wrapped by the macro _T.

3

Page 4: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

• Characters and character arrays must be declared using the data type TCHAR,not CHAR.

• The program must call Microsoft version of the string-handling utility func-tions, not the functions in the C standard library.

Other operating systems, such as Unix, Linux, and MacOS X, represent Unicodestrings differently. In these operating systems, Unicode strings are usually encodedby strings of 8-bit bytes in an encoding called UTF-8. Some Unicode charactersare encoded by one byte, some by two, and some by three or more. The maindisadvantage of this approach is that the number of words (8-bit bytes or 16-bitunsigned shorts) in a string no longer corresponds to the length of the strings inletters. The main advantage of this approach is that strings passed to and fromthe operating system continue to be of 8-bit bytes, and that the encoding of ASCIIstrings is the same under UTF-8 as under ASCII.

Another important aspect of Win32 that this program shows is the use of pre-defined data types. The most importat of these are

• Boolean variables of type BOOL and their corresponding literal constantsTRUE and FALSE.

• 32-bit nonnegative integers of type DWORD.

• Pointers, whose name usually follows the pattern LP*, such as LPVOID,pointers to TCHAR whose name is (for some strange reason) LPTSTR, andso on.

• Variables that represent some resource that the operating system has grantedaccess to, of type HANDLE. Handles represent open files, for example.

Almost any program that uses Win32 system calls must include the header fileswindows.h and tchar.h. From now on we will not mention them with everysystem call, but they are always needed.

Many Win32 system calls return a boolean value that indicates success or fail-ure. The value TRUE represents success and the value FALSE represents failure.To find out why a system call failed, call GetLastError(), which returns an er-ror code of type DWORD. The library routine FormatMessage translates the errorcode into a human-readable string. (Readable, but unfortunately, not always easyto understand.)

Opening a File: The system call CreateFile opens an existing file and/or createsa new one. It returns an identifier, which the program can later use to read andwrite from and to the file, as well as to perform certain other actions on the file. Ifthe call fails, it returns the value INVALID_HANDLE_VALUE.

4

Page 5: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

HANDLE CreateFile(LPCTSTR filename,DWORD access_flags,DWORD share_mode_flags,LPSECURITY_ATTRIBUTES sa,DWORD create_flags,DWORD attributes_and_flags,HANDLE template_file);

The first argument is the name of the file that you want to open, such as ..\dataor C:\Temp\xyz.dat.

The second argument specifies whether you want to open the file for reading,writing, or both. These actions are denoted by the constants GENERIC_READ andGENERIC_WRITE. You can request to perform both reads and writes by or-ing thetwo constants.

The third argument allows or denies other processes (programs) access to thefile while it is open. The value 0 forbids the operating system to open the file again(from other files or even again from this program) until we close it. The valuesFILE_SHARE_READ and FILE_SHARE_WRITE allow other programs to open thefile for reading or writing while it is open. You can allow both reading and writingby or-ing the two values.

The fourth argument is related to access permissions; for now, we will use NULL,which instructs the operating system to use the default behavior.

The fifth argument tells the operating what to do if the file exists or what to doif it does not exist.

• The value create_new causes the operating system to create a new file if nosuch file exists, or to fail the system call if the file already exists.

• The value CREATE_ALWAYS causes the creation of a new file, whether or nota file with the specified name exists.

• The value OPEN_EXISTING causes failure if the file does not exist.

• The value OPEN_ALWAYS causes an existing file to be opened, if it exists, or anew one to be created, otherwise.

• The value TRUNCATE_EXISTING causes failure if the file does not exist. Oth-erwise, the existing file is trunctated to size zero.

The sixth argument allows us to specify the attributes of a new file, if the callcreates a new file, and to request special access mechanisms. For now, we willuse the default value, FILE_ATTRIBUTE_NORMAL. Attributes that you can requestwith this argument include deletion of the file when the handle to it is closed, thatsystem calls that write to the file will block (wait) until the data has been physicallywritten to disk, and to declare that file accesses are mostly sequential or mostlyrandom.

The seventh and last argument allows the program to create a new file with theattributes of an existing file. We will not use this capability, so we will use the valueNULL as the last argument.

µ

Page 6: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

∫eldnah a yb detneserper ecruoser rehto eos gninruter ro elif a gnisolC

BOOL CloseHandle(HANDLE h);

rehto eos yb spahrep ro ¨ yb denruter saw taht eldnah eht si tneugra ehTro sseccus setacidni eulav nruter ehT Æeldnah a snruter taht llac etsystneugra eht taht si eruliaf elbissop fo esuac ylno eht ¨sesac tso nI Æeruliafeht ¨si taht ªeldnah dilav a ton si llac etsys eht ot dessap argorp eht tahtetsys suoiverp eos yb denruter saw taht eldnah a ton si tneugra eht fo eulav

Ædesolc tey ton dna llac

∫gnitirW dna gnidaeR

BOOL ReadFile (HANDLE h,LPVOID buffer,DWORD number_of_bytes_to_read,

LPDWORD number_of_bytes_actually_read,LPOVERLAPPED overlapped);

BOOL WriteFile(HANDLE h,LPVOID buffer,DWORD number_of_bytes_to_read,

LPDWORD number_of_bytes_actually_read,LPOVERLAPPED overlapped);

The first argument is a handle to an open file. The second is a pointer to an arraythat should be filled from data from the file or that should be written to the file.The third argument is the number of bytes that the program wants to transfer, andthe fourth is a pointer to a variable that will contain, once the system call returns,the number of bytes that were actually transfered. That number might be smallerthan the number of bytes that the program wanted to transfer, if a read requestreaches the end of the file, or if a write request encounters a full disk. The lastargument is used for a form of file I/O that we will not discuss now. For now, useNULL for the last argument.

For every open file, the operating system maintains a read/write pointer, whosevalue determines the file location where the next read or write operation will occur.Each read and write operation advances this pointer by the number of bytes actuallytransfered. When a file is opened, this pointer points to the beginning of the file.

A comment about fopen, fread, etc.: The functions fopen, fclose, fread, fwrite(and several more) perform services similar to those performed by the system callsthat we described. These functions, however, are part of the standard library ofthe C language. They are not a direct interface to the operating system. Programsthat use them, instead of the native system calls, are more portable (can be movedmore easily to other operating systems). However, not every service that you canrequest through the native system calls is available through the standard C libraryfunctions. In these exercises we will use the Win32 system calls, not the C libraryfunctions.

6

Page 7: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

The assignment. Write a program that copies a file. The program should existwith an error message if it receives less than three arguments, if the input file doesnot exist, or if the output file does exist. The program must use the Win32 systemcalls described above. The file must be copied using an n-byte buffer. That is, theprogram reads n bytes in one system call, writes them to the output file, and so on.The program expects three arguments: an input file name, an output file name,and n. To translate the string that contains the input argument n to an integer, youcan use _stscanf(argv[3],_T("%d"),&n). Use malloc to allocate the spacefor the buffer:

#include <stdlib.h>...char* buffer;...buffer = (char*) malloc(n);

Measure the time it takes your program to copy a file with a million bytes or more˙Perform the measurements using the utility processtimes, which is available onthe web site. Measure the running times when you use buffers of size 1, 64, 512,1024, 8192, and 65536. If different buffer sizes lead to significantly different run-ning times, suggest possible causes for the differences.

7

Page 8: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Working with Temporary Files!

Still needs to be written.

8

Page 9: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Creating Processes and Running Programs

·Â˙΢ ˙ÈÎÂ˙‰ Ï˘ ‰ÏÂÚÙ‰ ͯ„ Æ˙ÈÎÂ˙ · ıȯ‰Ï ˘„Á ÍÈω˙ ¯ÂˆÈÏ „ÓÏ ‰Ê ÏÈ‚¯˙·˘ÂÓÈ˘ ÍÂ˙ ÈΠ̇ ¨cmd.exe Ô‚Π®shell© ˙ÙËÚÓ ˙ÂÈÎÂ˙ Ï˘ ‰ÏÂÚÙ‰ ͯ„Ï „Â‡Ó ‰Ó„

Æ˙·Î¯ÂÓ ‰„˜٠˙¯Â˘ Ï˘ ÁÂÚÙ ˘¯Â„ Âȇ˘ ‰·¯‰· ËÂ˘Ù ˜˘ÓÓ·

∫˘„Á ÍÈω˙ ˙¯ÈˆÈ

BOOL CreateProcess(LPCTSTR path,LPTSTR command_line,LPSECURITY_ATTRIBUTES process_sa,LPSECURITY_ATTRIBUTES thread_sa,BOOL inherit_handles,DWORD creation_flags,LPVOID environment,LPTSTR current_directory,

LPSTARTUPINFO startup_info,

LPPROCESS_INFORMATION process_info);

ÍÈω˙ ˙¯ˆÂÈ ‰‡È¯˜‰ ¨‰˘ÚÓÏ Æ˙ÈÎÂ˙ · ‰ˆÈ¯Ó ˘„Á ÍÈω˙ ˙¯ˆÂÈ ˙ίÚÓ‰ ˙‡È¯˜·˘ÁÓ· ÈχÂ˯È „·ÚÓ ‡Â‰ ËÂÁ‰Â ¨È˯٠Ô¯ÎÈÊ ÌÚ ÈχÂ˯È ·˘ÁÓ ÔÈÚÓ ‡Â‰ ÍÈω˙‰—ËÂÁÂ˙‡Â ıȯ‰Ï ÌÈ˘˜·Ó ‡˘ ˙ÈÎÂ˙‰ ˙‡ Ìȯ‡˙Ó ÌÈ¢‡¯‰ ÌÈËÓ‚¯‡‰ È˘ ÆÈχÂ˯ÈÂ‰Ì˘ ÏÈÎ‰Ï ·ÈÈÁ ‡Â‰ ʇ ¨NULL Âȇ Ô¢‡¯‰ ËÓ‚¯‡‰ ̇ Æ‰Ï˘ ®‰„˜ى ˙¯Â˘© ÌȯËÓ¯Ù‰¨‰Óȇ˙Ó ˙ÈÎÂ˙ ˙˘ÙÁÓ ‰È‡ ‰ÏÚÙ‰‰ ˙ίÚÓ ¨‰ÊÎ ‰¯˜Ó· Æıȯ‰Ï ÌÈ˘˜·Ó˘ ı·Â˜ Ï˘‰„˜ى ˙¯Â˘ ˙‡ ÏÈÎÓ È˘‰ ËÓ‚¯‡‰ ÆÔÂ˙ ÂÓ˘˘ ‰ˆÈ¯‰ ı·Â˜ ˙‡ ‰ÏÈÚÙÓ ËÂ˘Ù ‡È‰ËÓ‚¯‡Ï ˙ÒÁÈÈ˙Ó ‰ÏÚÙ‰‰ ˙ίÚÓ Ê‡ ¨NULL ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ ̇ ÆÏ·˜˙ ˙ÈÎÂ˙‰˘¨‰ÊÎ ‰¯˜Ó· Æcmd.exe ÏÏΠͯ„· ¨®shell© ˙ÙËÚÓ‰ ˙ÎÂ˙· ˙„ϘÂÓ˘ ‰¯Â˘ Ï‡Î È˘‰˘ÂÙÈÁ‰ ª‰„˜ى ˙¯Â˘· ‰Â˘‡¯‰ ‰ÏÈÓ‰ ‡Â‰ ‰Ó˘˘ ˙ÈÎÂ˙ ˙˘ÙÁÓ ‰ÏÚÙ‰‰ ˙ίÚÓÏ˘ ÈÁΉ Íȯ„Ó· ¨˙ίÚÓ‰ ˙‡È¯˜ ˙‡ ‰ÚˆÈ·˘ ˙ÈÎÂ˙‰ ‰ÏÚÙ‰ ÂÓÓ˘ Íȯ„Ó· Úˆ·˙Ó‰·È·Ò‰ ‰˙˘Ó· ÚÈÙÂÓ ÌÓ˘˘ ÌÈÎȯ„Ó·Â ¨‰ÓˆÚ ‰ÏÚÙ‰‰ ˙ίÚÓ Ï˘ ÌÈÎȯ„Ó· ¨˙ÈÎÂ˙‰

ÆPATHͯډ ƯˆÂÂÈÈ˘ ËÂÁÏ ÍÈω˙Ï ‰˘È‚ ˙‡˘¯‰ Ú·˜Ï Ìȯ˘Ù‡Ó Ìȇ·‰ ÌÈËÓ‚¯‡‰ È˘ÌÈÈÈÂÚÓ Â‡ ̇‰ ÔÈÈˆÓ ‡·‰ ËÓ‚¯‡‰ ÆÏ„ÁÓ ˙¯ȯ·· ˘ÂÓÈ˘Ï ‰˘˜· ÔÈÈˆÓ NULLÆ̉· ˘Ó˙˘‰Ï ÏÎÂÈ ÌȯÁ‡ ÌÈ·‡˘Ó ÌÈÁÂ˙Ù ÌȈ·˜ Ï˘ ÌȉÊÓ‰ ˙‡ ˘¯ÈÈ ˘„Á‰ ÍÈω˙‰˘˙‡ ¯ÂˆÈÏ Ô˙È Æ˘„Á‰ ÍÈω˙‰ ¯ˆÂÂÈÈ ‰·˘ ‰¯Âˆ· ËÂÏ˘Ï ¯˘Ù‡Ó È˘È˘‰ ËÓ‚¯‡‰Ô˙È ÆÏÏÎ ÔÂÏÁ ‡ÏÏ Â‡ ¨˘„Á ÔÂÏÁ· ¨®Â˙‡ ¯ˆÂÈ˘ ‰¯Â‰‰ Ï˘© ÌÈȘ‰ ÔÂÏÁ· ıÂ¯È˘ ÍÎ ÍÈω˙‰Â‡ ¨‰Èχ ÍÈÈ˘ ‰¯Â‰‰ Ì‚˘ ÌÈÎÈω˙‰ ˙ˆÂ·˜Ó ˜ÏÁ ‰È‰È˘ ÍÎ ˘„Á‰ ÍÈω˙‰ ˙‡ ¯ÂˆÈÏÔÂÈÒ Ô‚ΠÌÈȈÈÁ ÌÈÚÂ¯È‡Ï ‰·Â‚˙ ÏÚ ‰ÚÈÙ˘Ó ÌÈÎÈω˙‰ ˙ˆÂ·˜ Ɖ˘„Á ‰ˆÂ·˜ ÏÈÁ˙È˘ËÓ‚¯‡‰ ͯ„ ̉· ËÂÏ˘Ï Ô˙È˘ ÌÈÙÒ ÌȯËÓ¯Ù ˘È Æcontrol-c ˙˘˜‰ È„È ÏÚ ‰ˆÈ¯ ˜ÈÒÙ‰ÏÌȯ˜ÓÏ ˙ÂÁ τÁÓ ˙¯ȯ·· ˘Ó˙˘Ó 0 ͯډ ÆÔ˜Ӊ „ÂÚÈ˙· ‡¯ ¨ÌÈÙÒ ÌÈ˯ÙÏ Æ‰Ê‰

ÆÌÈË¢ÙÏÎÂ˙ ˙ÈÎÂ˙‰˘ ‰·È·Ò‰ È˙˘Ó ˙‡ ¯‡˙Ó˘ Ô¯ÎÈÊ ÁË˘Ï ÚÈ·ˆÓ ÈÚÈ·˘‰ ËÓ‚¯‡‰˙¯ÊÚ· ÌÎ¯Ú ˙‡ ÛÂÏ˘Ï ÌÎ¯Ú ˙‡ Ú·˜Ï Ô˙È˘ ÌÈ˙˘Ó‰ ̉ ‰·È·Ò‰ È˙˘Ó Æ̉Èχ ˙˘‚ÏÏÚ Ìȯ‡Â˙Ó ‰·È·Ò‰ È˙˘Ó Æ˙ÂÈÎÂ˙ ÍÂ˙Ó Ì‚ Ì˙‡ ‡Â¯˜Ï Ô˙È ¨˙ÙËÚÓ· set ‰„˜ىۈ¯· ˙¯˘¯Â˘Ó Ô‰ ¨∞≠· ˙ÓÈÈ˙ÒÓ Ô‰Ó ˙Á‡ Ï΢ name=value ‰¯Âˆ‰Ó ˙ÂʯÁÓ ˙¯„Ò È„È

π

Page 10: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ÈÂÂ˙ Ï˘ ÏÏΠͯ„· Ô‰ ÂÏω ˙ÂʯÁÓ‰ ÆÛÒ 0 Í¯Ú ˘È ‰Â¯Á‡‰ ȯÁ‡Â ¨‰È˘‰ ȯÁ‡ ˙Á‡Ì¯Â‚ NULL ͯډ Ƅ˜ÈÂÈ ˙ÂʯÁÓ Ô‰ ˙ÂʯÁÓ‰˘ ÔÈȈ ̄˜‰ ËÓ‚¯‡· Ï‚„ ̇ ‡Ï‡ ¨ASCII

˘„Á‰ ÍÈω˙‰ ˙‡ ÏÈÁ˙‰Ï ¯˘Ù‡Ó ÈÈÓ˘‰ ËÓ‚¯‡‰ Ɖ¯Â‰‰ Ï˘ ‰·È·Ò‰ È˙˘Ó ˙˘Â¯ÈÏÍÈω˙‰Ó ‰˘Â¯ÈÏ Ì¯Â‚ NULL ͯډ ԇΠ̂ Æ· ˘Ó˙˘Ó ¯ˆÂȉ ÍÈω˙‰˘ ‰ÊÓ ¯Á‡ Íȯ„Ó·

ƯˆÂȉıÂ¯È Â·˘ ÔÂÏÁ‰ Ï˘ ‰‡¯Ó‰ ¨Ô¢‡¯‰ ÆÌȯ·„ È˘ ÏÚ ËÂÏ˘Ï ¯˘Ù‡Ó ÈÚÈ˘˙‰ ËÓ‚¯‡‰ÌÈÈ˯„ËÒ‰ ‰‡È‚˘Â ¨ËÏÙ ¨ËϘ‰ ÈˆÂ¯Ú ÏÚ ¨È˘‰ Æ˘„Á ÔÂÏÁ· ıÂ¯È ‡Â‰ ̇ ¨˘„Á‰ ÍÈω˙‰‡Ï ̇ Æ®C ˙Ù˘ Ï˘ ˙È˯„ËÒ‰ ‰È¯ÙÒ· stderr≠ ¨stdout ¨stdin© ˘„Á‰ ÍÈω˙‰ Ï˘Ì‡ ¨ı¯ ‡Â‰ ·˘ ÔÂÏÁÏ Ìȯ·ÂÁÓ˘ ÌȈ¯ڷ ˘Ó˙˘È ˘„Á‰ ÍÈω˙‰ ¨‰Ï‡Î ÌÈˆÂ¯Ú ÌÈڷ˜ÒÂÙÈËÓ ‰·Ó Ï˘ ˙·Â˙Î ¯È·Ú‰Ï ˘È ªÔ‡Î ȘÂÁ Âȇ NULL ͯډ ÆËÒ˜Ë ÔÂÏÁ· ı¯ Ô· ‡Â‰Ì‡ ÌÈÒÙ‡ ÏÈÎ‰Ï ÏÂÎÈ Áˢ‰ ¯‡˘ ÆÌÈ˙·· Âτ‚ ˙‡ ÏÈÎÓ ÂÏ˘ cb ‰„˘‰˘ ¨STARTUPINFOÒÂÙȇ Í¯ÂˆÏ ZeroMemory ‰¯‚˘· ˘Ó˙˘‰Ï ¯˘Ù‡ ÆÏ„ÁÓ‰ ˙¯È¯·· ˘Ó˙˘‰Ï ÌÈÈÈÂÚÓ

ƉËÂ˘Ù ‰‡ÏÂÏ· ˘Ó˙˘‰Ï Ì‚ Ô˙È Ï·‡ ¨Áˢ‰ÒÂÙÈËÓ ‰·Ó· ¯ˆÂ˘ ˘„Á‰ ËÂÁ‰Â ÍÈω˙‰ ÏÚ Ú„ÈÓ ¯ÈÊÁÓ Ô¯Á‡‰ ËÓ‚¯‡‰Æ‰‡È¯˜‰ ÈÙÏ ‰Ê‰ ‰·Ó‰ ˙‡ Ì‚ ÒÙ‡Ï ˘È ¨‰Ó ÌÂ˘Ó ÆPROCESS_INFORMATION̉È˙ÂÓ˘˘ ¨ËÂÁÏ ÍÈω˙Ï ®HANDLE ÒÂÙÈËÓ© ÌÈÁÂ˙Ù ÌȉÊÓ ∫˙„˘ ‰Ú·¯‡ ÏÈÎÓ ‰·Ó‰ÌȉÊÓ‰ ÆdwThreadId≠ dwProcessId ¨Ì‰Ï˘ ȯÙÒÓ‰ ‰‰ÊӉ ¨hThread≠ hProcess˙˜ÒÙ‰ Ô‚Π˙ÂÏÂÚÙ Úˆ·Ï Ô˙È Ì˙¯ÊÚ·Â ¨ËÂÁ‰Â ÍÈω˙‰ Ï˘ ˙ÂÓ˘‰ ‰˘ÚÓÏ Ì‰ ÌÈȯÙÒÓ‰task≠· ®PID ‡ Process Identifier© ÍÈω˙ ÏÎ Ï˘ ‰‰ÊÓ‰ ˙‡ ˙‡¯Ï Ô˙È ÆÍÈω˙ Ï˘ ‰ÏÂÚÙÌÈÁÂ˙Ù‰ ÌȉÊÓ‰ ˙‡ Ɖ‚ˆ‰Ï Â˙‡ ¯ÂÁ·Ï Ô˙È Í‡ ¨ÏÏΠͯ„· ‚ˆÂÓ Âȇ ‰Ê‰ ‰„˘‰ ªmanager

Ư˙ÂÈ Ì‰Ï ÌÈ˜Â˜Ê ‡Ï ¯˘‡Î CloseHandle ˙ÂÚˆÓ‡· ¯Â‚ÒÏ ˘È

˙ÂÎÁÏ ˙ÈÎÂ˙Ï ˙Âӯ‚˘ ˙ίÚÓ ˙‡ȯ˜ ˘È ˙ÂÂÏÁ ˙ÂίÚÓ· Æ®¯·„ ÏÎÏ ËÚÓΩ ‰˙Ó‰ÏÂÎÈ ÌˆÚ ÏÎ ¨˙ÂÂÏÁ· Æ„ÂÚ ¨ÍÈω˙ ˙ÏÂÚÙ ÌÂÈÒ ¨ËÂÁ ˙ÏÂÚÙ ÌÂÈÒ Ô‚Π¨‡Â‰˘ÏÎ Ú¯ȇÏÏÎ ÌÈÓÂÒÓ Ìȇ ÍÈω˙ ‡ ËÂÁ ÆÔÓÂÒÓ ‡Ï ‡ ®signaled© ÔÓÂÒÓ ∫ÌÈ·ˆÓ È˘Ó „Á‡· ‡ˆÓ‰Ï

ÆÌ˙ÏÂÚÙ ˙‡ ÌÈÓÈÈÒÓ Ì‰ ¯˘‡Î ÌÈÓÂÒÓ ¨„È˙Ú· ıÂ¯Ï ÌÈÈÂ˘Ú Â‡ ÌȈ¯ ̉˘ ÔÓʇȉ ÔÓÂÒÓ ·ˆÓÏ ÚÈ‚È ÌˆÚ˘ ÔÈ˙Ó‰Ï ˙Ó ÏÚ ‰· ˘Ó˙˘˘ ˙ίÚÓ‰ ˙‡È¯˜Â‡ ¨Â˙ÏÂÚÙ ˙‡ ÌÈÈÒÈ ÍÈω˙ ‡ ËÂÁ˘ ‰È˙ÓÓ ‰‡È¯˜‰ ÆWaitForSingleObject

ÆÔÓÂÒÓ ·ˆÓÏ ÚÈ‚È ¯Á‡ ‚ÂÒÓ ÌˆÚ˘

DWORD WaitForSingleObject(HANDLE object,DWORD timeout_in_milliseconds);

ËÓ‚¯‡‰Â ¨Â· ¯Â˘˜‰ ÚÂ¯È‡Ï ˙ÂÎÁÏ ÌÈ˘˜·Ó˘ ̈ډ Ï˘ ‰‰ÊÓ‰ ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰‰˙Ó‰‰ ̇ INFINITE Ú·˜‰ ‡ ¨‰È˘ ˙ÂÈÙχ· ÈÏÓÈÒ˜Ó‰ ‰˙Ó‰‰ ÔÓÊ ‡Â‰ È˘‰‰˙Ó‰‰ ̇ WAIT_OBJECT_0 ∫ÌÈÎ¯Ú ‰˘ÂÏ˘Ó „Á‡ ‰¯ÈÊÁÓ ‰¯‚˘‰ ÆÔÓÊ· ˙Ï·‚ÂÓ ‰È‡ÈÏÓÈÒ˜Ó‰ ÔÓʉ ˜¯Ù˘ ÏÏ‚· ‰ÓÈÈ˙Ò‰ ‰˙Ó‰‰ ̇ WAIT_TIMEOUT ¨‰Áψ‰· ‰ÓÈÈ˙҉Ʈmutex© ÏÂÚÓ ‡Â‰ ̈ډ ¯˘‡Î ˜¯ ¯ÊÁÂÓ ˙ÂÈ‰Ï ÏÂÎÈ˘ ¨WAIT_ABANDONED ‡ ¨¯·Ú ÂÈȈ˘

‰ˆÈ¯‰ ˙Ù˜˙ È·‚Ï Ú„ÈÓ ‰¯ÈÊÁÓ GetProcessTimes ‰‡È¯˜‰ ÆÍÈω˙ Ï˘ ‰ˆÈ¯‰ ÔÓÊÆͯˆ ‡Â‰˘ „·ÚÓ‰ È·‡˘Ó ÍÈω˙ Ï˘

BOOL GetProcessTimes(HANDLE processLPFILETIME creation_time,LPFILETIME exit_time,LPFILETIME kernel_time,LPFILETIME user_time);

±∞

Page 11: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ÌÈËÓ‚¯‡‰ ¯‡˘ ÏÎ ÆÂÈ˙„‡ Ú„ÈÓ ÌÈ˘˜·Ó˘ ÍÈω˙‰ Ï˘ ‰‰ÊÓ‰ ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰˙¯ÊÚ· ¨˙ÂÈ˘– ±∞∞ Ï˘ ˙„ÈÁÈ· ÔÓÊ ˙Ù˜˙ Ìȯ‡˙Ó˘ FILETIME ‚ÂÒÓ ÌÈ·Ó Ì‰≠ dwLowDateTime ¨˙ÂÈ·ÈÒ ≥≤ Ï˘ ÌÈÏÈÓ È˙˘Ï ˙˜ÏÂÁÓ ÂÏω ˙ÂÈ·ÈÒ‰ Æ˙ÂÈ·ÈÒ ∂¥˙¯ÈˆÈ Ï˘ ¨˙ÈÙȈÙÒ ÔÓÊ ˙„˜ Ìȯ‡˙Ó È˘ÈÏ˘‰Â È˘‰ ÌÈËÓ‚¯‡‰ ÆdwHighDateTime‰Ú·˜ ÔÂȈ ˙„Â˜Ó˘ ‰Ù˜˙‰ ‡È‰ ˜ÈÊÁÓ ‰·Ó‰˘ ÔÓʉ ˙Ù˜˙ ªÂÏ˘ ‰ˆÈ¯‰ ÌÂÈÒ ÍÈω˙‰‰¯‚˘‰ Æ˙¯‡Â˙Ó‰ ÔÓʉ ˙„Â˜Ï „Ú ®‰ÈÏ‚‡ ¨ßıÈȯ‚ Ï˘ ÔÓʉ ¯ÂÊȇ· ±∂∞± ˙˘ ˙ÏÈÁ˙© ¯·Ú·Æ˘¯ÂÙÓ ÔÙ‡· ‰Ú˘Â Íȯ‡˙ ÏÈÎÓ˘ ‚ˆÈÈÏ ‰ÊÎ ‚ˆÈÈ ‰¯ÈÓÓ FileTimeToSystemTime„·ÚÓ‰ ÏÚ Í¯ˆ ÍÈω˙‰˘ ˙ÂÓΉ ˙‡ ¨ÔÓÊ ˙ÂÓÎ ËÂ˘Ù Ìȯ‡˙Ó ÌȯÁ‡‰ ÌÈËÓ‚¯‡‰ È˘

Æ˘Ó˙˘Ó ·ˆÓ·Â ÒÁÂÈÓ ·ˆÓ·

ÆÂ˙ÏÂÚÙ ˙‡ ÌÈÈÒÓ ‡Â‰ ¯˘‡Î ÌÏ˘ Í¯Ú ¯ÈÊÁ‰Ï ÏÂÎÈ ÍÈω˙ ÆÍÈω˙Ó ¯ÊÁÂÓ‰ ͯډÆÚˆ·Ï ‰ÒÈ ÍÈω˙‰˘ ‰ÓÈ˘Ó· ÔÂÏ˘ÈΠ‡ ‰Áψ‰ ÔÂÈˆÏ ÏÏΠͯ„· ˘Ó˘Ó ‰Ê‰ ͯډ‰‡È¯˜ È„È ÏÚ ¨˙ÈÎÂ˙· main ‰¯‚˘‰Ó return ‰„˜ى È„È ÏÚ ¯ÊÁÂÓ ˙ÂÈ‰Ï ÏÂÎÈ Í¯Ú‰≠ ExitProcess ˙ίÚÓ‰ ˙‡ȯ˜ È„È ÏÚ Â‡ ¨C ˙Ù˘ Ï˘ exit ‰È¯ÙÒ‰ ˙Ȉ˜ÂÙÏ

ÆTerminateProcess

BOOL GetExitCodeProcess(HANDLE processLPDWORD exit_code);

ÚÈ·ˆÓ ‡Â‰ È˘‰Â ¨ÂÈ˙„‡ Ú„ÈÓ ÌÈ˘˜·Ó˘ ÍÈω˙‰ Ï˘ ‰‰ÊÓ‰ ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ÈÏ·ÓÈÒ‰ ͯډ ‡ Â˙ÏÂÚÙ ˙‡ ÌÈÈÒ ‡Â‰ ̇ ¯ÈÊÁ‰ ÍÈω˙‰˘ ͯډ ˙‡ ÏÈÎÈ˘ ‰˙˘ÓÏ

ÆıÂ¯Ï ÈÂ˘Ú Â‡ ı¯ ÔÈÈ„Ú ‡Â‰ ̇ STILL_ACTIVE‡ ‰ˆÈ¯‰ ˙ÙËÚÓ‰˘ ‰Â¯Á‡‰ ˙ÈÎÂ˙‰Ó ¯ÊÁÂÓ‰ ͯÚÏ ˙˘‚Ï Ô˙È ¨®cmd.exe© ˙ÙËÚÓ·

Ï˘ÓÏ ¨%errorlevel% ‰˙˘Ó‰ ͯ„ ıȯ‰Ï ‰˙ÒÈ

c:\> lpq -Smambo -Phpintelhpintel@mambo 1 jobc:\> echo %errorlevel%0c:\> nosuchprogram’nosuchprogram’ is not recognized as an inter-nal or external command,operable program or batch file.c:\> echo %errorlevel%9009c:\>dir l:The system cannot find the path specified.c:\> echo %errorlevel%1

‰ÒÈÙ„Ó ˙¯Á‡ ˙ÈÎ˙ ‰ÏÈÚÙÓ‰ ex-processtimes Ì˘· ˙ÈÎÂ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰Ï·˜Ï ‰Îȯˆ ˙ÈÎ˙‰ Æ˙ÓÈÈÒÓ ‡È‰˘ ¯Á‡Ï ‰Ï˘ ‰ˆÈ¯‰ ÔÓÊ ˙‡Â ‰ÓÓ ¯ÊÁÂÓ‰ ͯډ ˙‡¨ÌÈÈÒ˙˘ ÔÈ˙Ó‰Ï ¨˙ÈÎÂ˙‰ ˙‡ ıȯ‰Ï ¨‰„˜ى ˙¯Â˘ ÏÚ ÌÈËÓ‚¯‡Â ˙ÈÎÂ˙ Ì˘ ˘Ó˙˘Ó‰Ó

∫˙ÈÎ˙‰ Ï˘ ‰Ó‚Â„Ï ‰ÏÚÙ‰ Ɖˆ¯Â‰˘ ˙ÈÎÂ˙‰ Ï˘ ‰ˆÈ¯‰ ÈÓÊ ˙‡ ÒÈÙ„‰Ï ÔÎÓ ¯Á‡ÏÂ

c:\> ex-processtimes c:\windows\sysem32\lpq.exe -Smambo -Phpintel

11

Page 12: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

hpintel@mambo 0 jobs

error level 0elapsed time 0.110 secondsuser time 0.010 secondskernel time 0.040 seconds

12

Page 13: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Using Signals

°°°˙ÂÂÏÁ ˙·È·ÒÏ ÔÂÎ„Ú ˘¯Â„ ÔÈÈ„Ú˙ÁÏ¢ ‰ÏÚÙ‰‰ ˙ίÚÓ˘ ˙ÂÈχÂ˯È ˙˜ÈÒÙ Ìȉ Ò˜ÈÂÈ ҘÂÈÏ· ®������� © ÌÈ˙Â˙ȇ˙ίÚÓ‰˘ ‰Ú„‰ ¨‰ÙÂÓÓ ‡Ï Ô¯ÎÈÊÏ ˙˘‚Ï ÔÂÈÒ Ï˘ÓÏ ÂÓÎ ¨ÌÈÓÈÈÂÒÓ ÌÈ·ˆÓ· ÍÈω˙ωÏÚÙ‰‰ ˙ίÚÓÏ ‰ÚÈ„ÂÓ ‰Ï‡‰ ÌÈ·ˆÓ‰ „Á‡· ÏÙËÏ ˙ÈÈÂÚÓ˘ ˙ÈÎ˙ Æ„ÂÚ ¨¯‚Ò‰Ï ˙„ÓÂÚ‰ÏÚÙ‰‰ ˙ίÚÓÓ ˘˜·Ï ‰ÏÂÎÈ ˙ÈÎ˙ ¨Ï˘ÓÏ Æ‰Ê‰ ·ˆÓ‰ ‰¯Â˜ ¯˘‡Î ÏÈÚÙ‰Ï ‰¯‚˘ ÂÊȇ˙ÂÒÏ ˙Ó ÏÚ ¨‰ÙÂÓÓ ‡Ï Ô¯ÎÈÊÏ ˙˘‚Ï ‰ÒÓ ˙ÈÎ˙‰ ¯˘‡Î ˙ÈÎ˙· ˙ÓÈÈÂÒÓ ‰¯‚˘ ÏÈÚىωÏÚÙ‰‰ ˙ίÚÓ ¨‰˙ÏÂÚÙ ˙‡ ˙ÓÈÈÒÓ ˙‡ÊÎ ‰¯‚˘ ¯˘‡Î ÆÛ„‰ ‚ȯÁÏ Ì¯‚˘ Û„Ï ı·Â˜ ˙ÂÙÓÏÔ¯ÎÈÊÏ ‰˘È‚ Ï˘ ‰¯˜Ó·© Æ„ÁÂÈÓ‰ ·ˆÓ‰ ‰¯˜ ·˘ ÌÂ˜Ó Â˙Â‡Ó ÍÈω˙‰ ˙‡ ıȯ‰Ï ‰ÎÈ˘ÓÓ̇—‰Ï˘Î˘ Ô¯ÎÈÊÏ ‰˘È‚ ‰˙Â‡Ó ÏÁ‰ ˙ÈÎ˙‰ ˙‡ ıȯ˙ ‰ÏÚÙ‰‰ ˙ίÚÓ ¨Ï˘ÓÏ ¨‰ÙÂÓÓ ‡Ï

Æ®·Â˘ Ï˘Î˙ ‰ÏÂÚÙ‰ ‰ÙÂÓÓ Âȇ ÔÈÈ„Ú Ô¯ÎÈʉ‡Ï ˙ÈÎ˙˘ ‰¯˜ÓÏ ˙ÈÏӯ ˙‚‰˙‰ ‰¯È„‚Ó ‰ÏÚÙ‰‰ ˙ίÚÓ ‰ÊÎ „ÁÂÈÓ ·ˆÓ ÏÎÏÌÈÈËȯ˜ ‡Ï ÌÈ·ˆÓÓ ˙ÂÓÏÚ˙‰ ‡È‰ ˙ÈÏӯ‰ ˙‚‰˙‰‰ ƉÈÚ·· ÏÂÙÈËÏ ‰¯‚˘ ÏÚ ‰ÚÈ„ÂÓ‰ÏÚÙ‰‰ ˙ίÚÓ˘ ÂÊÎ ‰„ÈÓ· ÌÈÈ˙ÈÈÚ· ÌÈ·ˆÓ ˘È ÆÌÈÈËȯ˜ Ìȯ˜Ó· ˙ÈÎ˙‰ ˙ÏÂÚÙ ˙˜ÒÙ‰Â

ÆÍÈω˙‰ ˙ÙÚ‰—˙ÈÏӯ‰ ˙‚‰˙‰‰ ˙‡ ˙Â˘Ï ˙ÈÎ˙Ï ‰˘¯Ó ‡Ï

∫˙Â˙ȇ· ÏÂÙÈË ˙¯‚˘ ÌÂ˘È¯

#include <signal.h>...void (*signal(int signum, void (*han-dler)(int)))(int);

Ï·˜˙Ó ¯˘‡Î ÏÈÚÙ‰Ï ˘È˘ ‰¯‚˘‰ ÏÚ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ‰ÚÈ„ÂÓ signal ˙ίÚÓ‰ ˙‡È¯˜‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ ÆÚÈ·ˆÓ ‰¯ÈÊÁÓ ‡È‰Â ÌÈËÓ‚¯‡ È˘ ˘È ‰‡È¯˜Ï ÆÌÈÂÒÓ ˙Â˙ȇÔ‚ΠÈÏ·ÓÈÒ Ú·˜ ‡Ï‡ ȯÙÒÓ ÌÏ˘ Í¯Ú ˙ÈÎ˙· ÌÈÈÈˆÓ ‡Ï Ôȇ ÏÏΠͯ„· Æ˙Â˙ȇ‰È˘‰ ËÓ‚¯‡‰ Æman 7 signal≠· ÌÈÚÈÙÂÓ ÌÈ˙Â˙ȇ‰ ÏÎ ˙ÂÓ˘ ÆSIGTERM ‡ SIGSEGV‰˙Èȉ˘ ‰¯‚˘Ï ÚÈ·ˆÓ ‰¯ÈÊÁÓ ‰‡È¯˜‰˘ ÚÈ·ˆÓ‰ Æ˙Â˙ȇ· ÏÂÙÈˉ ˙¯‚˘ Ï˘ ˙·Â˙Î ‡Â‰˙¯‚˘ ˙‡ ˙Â˘Ï ˙ÈÎ˙· Ï„ÂÓÏ ˙¯˘Ù‡Ó ‰Ê Í¯Ú ˙¯ÊÁ‰© ‰Î „Ú ˙Â˙ȇ· ÏÂÙÈËÏ ˙ȇ¯Á‡‡ÏÏ ¨Ï„ÂÓ‰Ó ‰‡ÈˆÈ‰ ÌÚ ˙ӄ˜‰ ˙‚‰˙‰‰ ˙‡ ¯ÈÊÁ‰Ï ÏÚÂÙ Ï„ÂÓ‰˘ ÔÓÊ· ˙Â˙ȇ‰

Æ®˙ӄ˜‰ ˙‚‰˙‰‰ ‰˙Èȉ ‰Ó ˜ÂÈ„· ˙Ú„Ï Í¯ËˆÈ Ï„ÂÓ‰˘Ô‰˘ ͯډ ÆÍ¯Ú ÏÎ ˙¯ÈÊÁÓ Ôȇ ¨ÌÏ˘ Í¯Ú ¨„Á‡ ËÓ‚¯‡ ˙ÂÏ·˜Ó ˙Â˙ȇ· ÏÂÙÈË ˙¯‚˘

ÆÌÈ˙Â˙ȇ ¯ÙÒÓ· ÏÙËÏ ˙Á‡ ‰¯‚˘Ï ¯˘Ù‡Ó˘ ˜˘ÓÓ ¨˙Â˙ȇ‰ ¯ÙÒÓ ‡Â‰ ˙ÂÏ·˜Óͯډ ÆSIG_DFL≠ SIG_IGN ÌÈίډ ˙‡ ¯È·Ú‰Ï Ì‚ Ô˙È ‰¯‚˘ Ï˘ ˙·Â˙Π̘ӷÚÈ„ÂÓ È˘‰Â ¨˙Â˙ȇ‰Ó ÌÏÚ˙‰Ï ‰ˆÂ¯ ˙ÈÎ˙‰˘ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ÚÈ„ÂÓ Ô¢‡¯‰

ÆÏ„ÁÓ‰ ˙¯È¯· ˙‚‰˙‰ ‡È‰ ‰Èˆ¯‰ ˙‚‰˙‰‰˘

˙ÈÈÂˆÓ Ì‚ ‰Ê Û„· Æman 7 signal≠· ÌÈÚÈÙÂÓ Ì‰È˙ÂӢ ÌÈ˙Â˙ȇ‰ È‚ÂÒ ÆÌÈ˙Â˙ȇ È‚ÂÒÆ˙Â˙ȇ ÏÎ Ï˘ ‰ÏÈ‚¯‰ ˙‚‰˙‰‰

˙˘‚Ï ‰ÒÓ ˙ÈÎ˙‰ ¯˘‡Î ÁÏ˘˘ ˙Â˙ȇ ¨SIGSEGV ÏÚ ˙Ú„Ï ·Â˘Á ‰Ê ÏÈ‚¯˙ ͯˆÏÌÈÈÒÈ ÍÈω˙‰˘ ˙ÈÈÂÚÓ ‰ÏÚÙ‰‰ ˙ίÚÓ ¯˘‡Î ÁÏ˘˘ SIGTERM ÏÚ ¨‰ÙÂÓÓ ‡Ï ˙·Â˙ÎÏ

Æ„ÈÈÓ ÌÈÈ˙Ò‰Ï ·ÈÈÁ ÍÈω˙‰ ¯˘‡Î ÁÏ˘˘ SIGKILL ÏÚ ¨Â˙ÏÂÚÙ ˙‡

∫˙Â˙ȇ ˙ÁÈÏ˘

13

Page 14: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

#include <signal.h>#include <sys/types.h>...int kill(pid_t pid, int sig);

˙‡ ÔÈȈ ÏÏΠͯ„· Æpid ¯ÙÒÓ˘ ÍÈω˙Ï sig ˙Â˙ȇ‰ ˙‡ ˙ÁÏ¢ kill ˙ίÚÓ‰ ˙‡È¯˜Æ¯ÙÒÓ È„È ÏÚ ‡Ï ÈÏ·ÓÈÒ Ú·˜ ˙¯ÊÚ· ˙Â˙ȇ‰

˙Ï·˜Ó ‰„˜ى ÆÈ·È˘‡¯Ëȇ ÔÙ‡· ÌÈÎÈω˙Ï ÌÈ˙Â˙ȇ ÁÂÏ˘Ï ˙¯˘Ù‡Ó kill ‰„˜ىȯÙÒÓ ÂȯÁ‡Â ˙Â˙ȇ‰ ¯ÙÒÓ ˙‡ ÔÈȈӉ - ÔÓÈÒ‰ ÚÈÙÂÓ ÂÈÙÏ˘ ÈÏÂȈÙ‡ ȯÙÒÓ ¯ËÓ¯Ù˙Â˙ȇ ˙ÁÏ¢ ¨Ï˘ÓÏ ¨kill -9 1234 ‰„˜ى Æ˙Â˙ȇ‰ ˙‡ Ì‰Ï ÁÂÏ˘Ï ˘È˘ ÌÈÎÈω˙Ï„ÁÓ‰ ˙¯È¯· ¨˙Â˙ȇ‰ ¯ÙÒÓ ˙‡ ÌÈÈÈˆÓ ‡Ï ¯˘‡Î Ʊ≤≥¥ ¯ÙÒÓ ÍÈω˙Ï ®SIGKILL© π ¯ÙÒÓ

ÆÍÈω˙‰ ˙ÏÂÚÙ ÌÂÈÒÏ ‰˘˜· ¨SIGTERM ‡È‰

ÏÚ Ægdb Ì˘· ®debugger© ‰ÙÓ ‰ÏÈÚÙÓ‰ ˙ÈÎ˙ ·Â˙Î ÏÈ‚¯˙‰ ˙¯‚ÒÓ· Ægdb≠· ˘ÂÓÈ˘¨˙Â¯Â˘ ȯÙÒÓ ¨Ì‰È˙·Â˙ΠÌÈ˙˘Ó ˙ÂÓ˘© ˙ÈÎ˙‰ ÈÂÙÈÏ Ú„ÈÓ ˜ÈÙÒÓ ‰È‰È gdb≠Ï˘ ˙Ó‰„˜ى Ô˙Ó È„È ÏÚ gdb ˙Á˙ ˙ÈÎ˙ ÏÈÚÙ‰Ï Ô˙È Æ-g Ï‚„‰ ÌÚ ˙ÈÎ˙‰ ˙‡ ÏÙÓ˜Ï ˘È ¨®ßÂÎÂÔ˙Ó È„È ÏÚ ‰ˆ¯ ˙ÈÎ˙ ˙ÂÙÏ ÏÈÁ˙‰Ï Ì‚ Ô˙È Æ®PROG ‡Â‰ ˙ÈÎ˙‰ Ì˘ ¯˘‡Î© gdb PROGÏ˘ ˜˘ÓÓ‰ Ɖˆ¯‰ ˙ÈÎ˙‰ Ï˘ ÍÈω˙‰ ¯ÙÒÓ ‡Â‰ PID ¯˘‡Î gdb PROG PID ‰„˜ى

∫Ô‰ ÔÎÂ˙Ó ¯˙ÂÈ· ˙ÂÈ˘ÂÓÈ˘‰˘ ¨˙„˜٠¯ÙÒÓ ÏÏÂ΢ ȯÓ–‡Ùχ ˜˘ÓÓ ‡Â‰ gdb

˘ÂÓÈ˘ ‰„˜Ù

˙ÈÎ˙‰ ˙ˆ¯‰ ˙ÏÈÁ˙‰¯ÈˆÚÓ ‰ˆÈ¯ ͢Ӊ˙ÈÎ˙· ‡ ÔÎȉ

‰˙˘Ó Í¯Ú ˙ÒÙ„‰ÌȘ„·˘ ˙ÈÎ˙‰ ˙‚ȯ‰

‰‡ÈˆÈ‰¯ÊÚ

ÌÈ˙Â˙ȇ· „ÁÂÈÓ· ÔÈÈÚÓ ÏÂÙÈË ÆÌÈ˙Â˙ȇ· ÏÙËÏ „ÓÏ Â·˘˙ÈÒÁÈ ·Î¯ÂÓ ÏÈ‚¯˙ Â‰Ê ÆÏÈ‚¯˙‰Æ˙ÈÎ˙· ‚ȯÁ ·ˆÓ ¯¯ÂÚ˙Ó ¯˘‡Î ‰ÙÓ ˙Á˙ ‰ˆÈ¯Ï ˙ÈÎ˙‰ ˙ÒΉ ‡Â‰ Ï‚¯˙ Â˙‡˘

¯ÙÒÓ ˙¯ÈˆÈÏ Ì¯‚Ï ˙ÂÈÂ˘Ú ®∂≠µ© ÏÈ‚¯˙‰ Ï˘ Ô¯Á‡‰ ˜ÏÁ· ˙ÂÎ˙ ˙‡Ȃ˘ ÍÎÏ ·Ï ÂÓÈ˘Ï˘ ‰¯˜Ó· Æ˙‡Ȃ˘Ó ÚÓ‰Ï „ÈÓ˙Ó ¯˙ÂÈ Ï„˙˘‰Ï ˘È ¨˙‡Ê Ï˘· ÆÌÈÎÈω˙ Ï˘ „Â‡Ó ·¯˙‡ Á˙ÙÏ Í˙¯˘Ù‡· ˘È ̇ ƯÁ‡ ÔÂÏÁÓ Ì˙‡ ‚¯‰ ‡‡ ¨ÌÈÎÈω˙ Ï˘ ·¯ ¯ÙÒÓ ˙¯ÈˆÈͯ„ °˘Â˘ÁÏ Í¯Âˆ Ôȇ ͇ ¯‰Ê‰Ï ˘È Æ˙‡Ê ‰˘Ú ‡‡ ¨„·Ï· ͢ÂÓÈ˘·˘ ·˘ÁÓ ÏÚ ÏÈ‚¯˙‰¯ÙÒÓ ÏÚ ÌÒÁ‰ ˙‡ ÔÈ˘‰Ï ‡Â‰ Ò˜ÂÈÏ· ¯ˆÂÂ‰Ï ÌÈÈ¢ڢ ÌÈÎÈω˙‰ ¯ÙÒÓ ˙‡ ÏÈ·‚‰Ï ˙Á‡ÌÈÎÈω˙‰ ¯ÙÒÓ ‡Â‰ X ¯˘‡Î ¨limit maxproc X ‰„˜ى ˙¯ÊÚ· ¯ÂˆÈÏ Ô˙È˘ ÌÈÎÈω˙‰

Æ®shell© ˙ÙËÚÓ‰ ˙Á˙ ˙ÈÓÊ Â· ¯ÂˆÈÏ Ô˙È˘˙·¢˙ Ì‚ ˘È‚‰Ï© Ìȇ·‰ ÌÈ·Ï˘‰ ÈÙ ÏÚ ˙ÈÎ˙‰ ˙‡ Á˙ÙÏ ˘È ¨ÏÈ‚¯˙‰ ˙·ίÂÓ ˙‡ÙÓ

∫®˙ÂÚÈÙÂÓ‰ ˙Âχ˘Ï

Æy ËÏ˜Ï ‰ÎÁÓ continue? ˙ʯÁÓ‰ ˙‡ ‰ÒÈÙ„Ó‰ ex-sig.c Ì˘· ˙ÈÎ˙ ·Â˙ΠƱ˙Ï·˜ ¯Á‡Ï Ɖ·Â˘˙Ï ˙ÂÎÁÏ ·Â˘ ‰Ï‡˘‰ ˙‡ ÒÈÙ„‰Ï ˘È ¯Á‡ ËϘ ÏÎ Ï˘ ‰¯˜Ó·∞ ˙·Â˙η ‰˘ÏÎ Í¯Ú ˙Ó˘‰ ‡È‰ ‰Ï˘ ‰‡·‰ ‰ÏÂÚÙ‰ ƉÎÈ˘ÓÓ ˙ÈÎ˙‰ Ȉ¯‰ ËϘ‰Æpointer==NULL ¯˘‡Î *pointer=1 ‰„˜ى È„È ÏÚ Ï˘ÓÏ© ‰ÙÂÓÓ ‰È‡ ÌÏÂÚÏ˘

±¥

Page 15: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

˙¯ÊÚ· ¯Á‡ ÔÂÏÁÓ SIGTERM ‰Ï ÁÏ˘ ËÏ˜Ï ‰ÎÁÓ ‡È‰ ¯˘‡Î ˙ÈÎ˙‰ ˙‡ ÏÚÙ‰ Æ≤‚¯Â‰ ˙Â˙ȇ‰ Æps aux ‰„˜ى ˙¯ÊÚ· ˙ÂÏ‚Ï Ô˙È ÍÈω˙‰ ¯ÙÒÓ ˙‡ Ækill ‰„˜ى

ÆÍÈω˙‰ ˙‡

¨˙Â˙ȇ‰ ‚ÂÒ ˙‡ ˜Â„·Ï ‰Îȯˆ ‰¯‚˘‰ ÆÌÈ˙Â˙ȇ· ÏÂÙÈË ˙¯‚˘ ˙ÈÎÂ˙Ï ÛÒ‰ ˙ÚÎ Æ≥„˜ ÛÒ‰ ˙È˘‡¯‰ ˙ÈÎ˙· ÆI will survive ÒÈÙ„‰Ï ËÂ˘Ù SIGTERM Ï˘ ‰¯˜Ó·ÂÈ„È ÏÚ ÍÈω˙‰ ˙‡ ‚¯‰Ï ˙ÚÎ ‰Ò ÆSIGTERM ˙Â˙È‡Ï Âʉ ÏÂÙÈˉ ˙¯‚˘ ˙‡ Ì˘Â¯˘

ø‰¯Â˜ ‰Ó Ækill ‰„˜ى

ø˙ÚÎ ‰¯Â˜ ‰Ó Æ®SIGKILL © kill -9 ‰„˜ٷ ˘ÂÓÈ˘ È„È ÏÚ ÍÈω˙‰ ˙‡ ‚¯‰Ï ‰Ò Æ¥øSIGTERM≠· ÂÓÎ ‰Ê ‰¯˜Ó· ÏÙËÏ Ô˙È Ì‡‰

‰¯‚˘Ï ÛÒ‰ SIGSEGV≠· ÏÂÙÈË ¯Â·Ú Ì‚ ÏÂÙÈˉ ˙¯‚˘ ˙‡ Ì˘Â¯˘ „˜ ÛÒ‰ ˙ÚΠƵ‰ÚÈ‚Ó ˙ÈÎÂ˙‰ ¯˘‡Î ‰¯Â˜ ‰Ó Æ‰Ê ˙Â˙ȇ ¯Â·Ú Ì‚ ˙ʯÁÓ‰ ‰˙‡ ˙‡ ÒÈÙ„˙˘ ‰˜È„·

ø∞ ˙·Â˙ÎÏ ‰Ó˘‰Ï

˙Ï·˜Ó ˙ÈÎ˙‰ ¯˘‡Î gdb ˙‡ ÏÈÚÙ˙˘ ÍÎ ÏÂÙÈˉ ˙¯‚˘ ˙‚‰˙‰ ˙‡ ‰˘ ˙ÚÎ Æ∂ÍÈω˙‰ ¯ÙÒÓ ˙‡ ¯¯·Ï ‰Îȯˆ ‰¯‚˘‰ ‰ÊÎ ‰¯˜Ó· Æ®SIGSEGV© Û„ ‚ȯÁ ÏÚ ˙Â˙ȇ‰‰Ê ˘„Á ÍÈω˙ ¯ÂˆÈÏ ¨®unistd.h≠· ˙¯„‚ÂÓ© getpid≠Ï ‰‡È¯˜ ˙ÂÚˆÓ‡·‰„˜ى ˙‡ execv ˙¯ÊÚ· ÏÈÚÙ‰Ï Íȯˆ ®Ô·‰© ˘„Á‰ ÍÈω˙‰ Æfork ˙ÂÚˆÓ‡·ÆÍÈω˙‰ ¯ÙÒÓ ÚÈÙÂ‰Ï Íȯˆ PID ̘ӷ ¯˘‡Î /usr/bin/gdb ex-sig PID

Æsleep ˙¯ÊÚ· ÂÈχ ¯·Á˙È ‰ÙÓ‰˘ ˙ÂÎÁÏ ËÂ˘Ù Íȯˆ ȯ˜Ӊ ÍÈω˙‰

‰Ó ø‰ÙÓ‰ ‰Èχ ¯·Á˙Ó ˙ÈÎ˙· ‰„˜ ‰Êȇ· ÆÛ„ ‚ȯÁÏ ‰Ï ̯‚ ˙ÈÎ˙‰ ˙‡ ÏÚÙ‰ Æ∑ø˙ÈÎ˙‰ ˙ˆÈ¯ ˙‡ ÍÈ˘Ó‰Ï ‰ÙÓ‰Ó ÌÈ˘˜·Ó ¯˘‡Î ‰¯Â˜

±µ

Page 16: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Notes for Teachers: Jobs and Process Groups

͇ ÌÈÓ„ ÌÈÂ‚Ó È˘· ˘ÂÓÈ˘ ÍÂ˙ ˙ÂÈÏÂȈ˜ÂÙ ‰˙‡ ˙‡ ÌÈ˘˘ÓÓ Ìȇ·‰ ÌÈÏÈ‚¯˙‰ È˘¨‰˘„Á ÌÈÎÈω˙ ˙¯Â·Á ÍÂ˙· ˙ÈÎÂ˙ ıȯ‰Ï ‡È‰ ‰¯ËÓ‰ ÌÈÏÈ‚¯˙‰ È˘· ÆWin32 Ï˘ ÌÈ„¯ÙÈ˘‰Â ®jobs© ˙„·ڷ ˘Ó˙˘Ó „Á‡ ÏÈ‚¯˙ Ɖ¯Â·Á·˘ ÌÈÎÈω˙‰ ÏÎ ˙‡ ‚¯‰Ï ÔÎÓ ¯Á‡Ï¯˙ÂÈ ‰Ó„ È˘‰ ÂÏȇ ¨¯˙ÂÈ ÈÏÏÎ Ô¢‡¯‰ Ô‚Ӊ Æ®process groups© ÌÈÎÈω˙ ˙¯·Á·Ì‰È˘ ˙‡ ‡Ï ÌÈÏÈ‚¯˙‰ È˘Ó „Á‡ ¯˙Âȉ ÏÎÏ ÏÈË‰Ï È‡„Î ‰¯˜Ó Ïη Æposix Ï˘ ˜˘ÓÓÏ

Æ„Á‡ ү˜·Í¯Â‡ ÌÚ ÌÈ·¯ ÌÈÎÈω˙ ¯ˆÈÈÏ ‰„ȘÙ˙˘ ˙˜ÙÂÒÓ ¯ÊÚ ˙ÈÎÂ˙· ÌÈ˘Ó˙˘Ó ÌÈÏÈ‚¯˙‰Âʉ ¯ÊÚ‰ ˙ÈÎÂ˙ ˙ˆ¯‰ ÆÌ‰Ï˘ ˙¯˙Ù‰ ˙‡ ˜Â„·Ï ÂÏÎÂÈ ÌÈ„ÈÓÏ˙‰˘ ˙Ó ÏÚ ¨¯ˆ˜ ÌÈÈÁÆÌÈ˘„Á ÌÈÎÈω˙ ¯ÂˆÈÏ ˙ÏÂÎÈ È‡· ‡Ë·˙Ó˘ ¨‰ÏÚÙ‰‰ ˙ίÚÓ· ÌÈ·‡˘Ó ¯ÒÂÁÏ Ì¯‚Ï ‰ÏÂÏÚ‡Ï Æ®‰ÚÙÂ˙· ˙ÂÒ˙‰Ï Ì‰Ï ıÈÏÓ‰Ï È‡„Î È˙Ú„Ï Ï·‡© ÍÎ ÏÚ ÌÈ„ÈÓÏ˙‰ ˙‡ Ú„ÈÈÏ È‡„Î˙Â¯È˘ ˙˜ÒÙ‰Ï Ì¯‚Ï ÏÂÏÚ ÌÈ·‡˘Ó‰ ¯ÒÂÁ ·˘ ˙¯˘ ÏÚ Âʉ ¯ÊÚ‰ ˙ÈÎÂ˙ ˙‡ ıȯ‰Ï È„Î

ƉӈÚØÂÓˆÚ ‰Ø„ÈÓÏ˙Ï Ë¯Ù ÌȯÁ‡ ÌÈ˘Ó˙˘ÓϨÌÈÎÈω˙‰ ˙‡ ‚¯‰Ï ÔÓʉ ÚÈ‚‰˘ ÔÓÒÏ ˙Ó ÏÚ ®event© Ú¯ȇ· ÌÈ˘Ó˙˘Ó ÌÈÏÈ‚¯˙‰‡Ï ÌÈËÂÁ Ô¯ÎÈÒ È‡˘Â· ÌÈÏÈ‚¯˙‰ ˙Ïˉ ¯Á‡Ï ÂÏω ÌÈÏÈ‚¯˙‰ ˙‡ ÏÈË‰Ï È‡„Î ÔÎÏÂ

Æ̉ÈÙÏ

±∂

Page 17: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Managing Jobs

˙ÂÏÂÚÙ Úˆ·Ï ÌÈÎÈω˙ Ï˘ ˙ˆ·˜ ˘¯ÂÙÓ ÔÙ‡· ‚ˆÈÈÏ ÏÏΠͯ„· ˙¯˘Ù‡Ó ‰ÏÚÙ‰ ˙ÂίÚÓprocess© ÌÈÎÈω˙ ˙¯Â·Á ˙‡¯˜˘ ¨ÂÊÎ ‰ˆÂ·˜ Æ˙Á‡ ˙·· ‰ˆÂ·˜ Ï˘ ÌÈÎÈω˙‰ ÏÎ ÏÚÍÈω˙ ‰Èχ ÍÈÈ˘Ï Ô˙È ¨˙ίÚÓ ˙‡È¯˜ È„È ÏÚ ˘¯ÂÙÓ ÔÙ‡· ˙¯ˆÂ ®job© ‰„Â·Ú Â‡ ®groupÍÈÈÂ˘Ó ˘„Á‰ ÍÈω˙‰ ¨ÛÒ ÍÈω˙ ¯ˆÂÈ ÂÊÎ ‰¯Â·ÁÏ ÍÈÈ¢Ӣ ÍÈω˙ ¯˘‡Î ÆÌÈÎÈω˙ ‡̯‚ ‰Ê‰ Ô‚Ӊ Æ®˙¯Á‡ ‰¯Â·ÁÏ ˙¢¯ÂÙÓ Â˙‡ ÌÈÎÈÈ˘Ó Ì‡ ‡Ï‡© ‰¯Â·ÁÏ ˙ÈËÓ¡̇ Ì‚ ¨ÌȯÁ‡ ÌÈÎÈω˙ ÌȯˆÂÈ ÌÈÎÈω˙ ·˘ ¨ÌÏ˘ ÌÈÎÈω˙ ıÚ ‰¯Â·ÁÏ ÍÈÈ˘Ï Ô˙È˘ ÍÎϨ®shells© ˙ÙËÚÓ ˙ÂÈÎÂ˙ Æ˘¯ÂÙÓ ÔÙ‡· ‰¯Â·ÁÏ ÌÓˆÚ ˙‡ ÌÈÎÈÈ˘Ó Ìȇ ÂÏω ÌÈÎÈω˙‰˙ÈÎÂ˙ Ï˘ ‰˙ÏÂÚÙ ˙‡ ˜ÈÒÙ‰Ï ˘Ó˙˘ÓÏ ¯˘Ù‡Ï ˙Ó ÏÚ ‰Ê‰ Ô‚ӷ ˙¢Ó˙˘Ó ¨Ï˘Óωȇ ˙ÙËÚÓ‰˘ ¨ÌÈÎÈω˙ Ï˘ Ï„‚ ¯ÙÒÓ ‰¯ˆÈ Âʉ ˙ÈÎÂ˙‰ ̇ Ì‚ ¨˙ÙËÚÓ‰ ͯ„ ıȯ‰˘

ÆÌÈÓÈȘ Ìȇ ¯·Î Èχ Ì˙‡ ¯ˆÈ˘ ÌÈÎÈω˙‰ ‡ ÍÈω˙‰˘Â ¨ÌÓÂÈ˜Ï ÏÏÎ ˙Ú„ÂÓprocess© ÌÈÎÈω˙ ˙¯Â·Á ‡¯˜ „Á‡ ‚ÂÒ ÆÌÈÎÈω˙ ˙¯·Á Ï˘ ÌÈ‚ÂÒ È˘ ˘È ˙ÂÂÏÁ·˜Â˙È È„È ÏÚ ÂÊÎ ‰¯Â·Á ÌȯˆÂÈ ÆÒ˜ÂÈÏ ҘÈÂÈ· ÌÈÎÈω˙ ˙¯·ÁÏ ‰Ó„ ‰Ê‰ ‚ÂÒ‰ Æ®group‰Èχ ÍÈÈ˘ ˜˙¢ ÍÈω˙‰˘ ‰˘„Á ‰¯Â·Á ¯ˆÂÈ ‰Ê ª‰Èχ ÍÈÈ˘ ‡Â‰˘ ‰¯Â·Á‰Ó ÍÈω˙ Ï˘ÏÎ ¯˘‡Î ˜¯ · ˘Ó˙˘‰Ï Ô˙È˘ ÔÂÈÎÓ ¨È„ÓÏ Ï·‚ÂÓ ‰Ê‰ ‚ÂÒ‰ ÆÍÈω˙‰ ¯ÙÒÓ ‡Â‰ ‰Ó˘˘ÂÏÏΠͯ„· ˙¯˘ ˙ÂÎÂ˙ ˙ÂÈÙ¯‚ ˙ÂÈÎÂ˙˘ ÔÂÈÎÓ Æ˙Á‡ ‰ÏÂÒÂ˜Ï Ìȯ·ÂÁÓ ‰¯Â·Á· ÌÈÎÈω˙‰

Ɖʉ ‚ÂÒ· ˘Ó˙˘‰Ï ¯˘Ù‡ „ÈÓ˙ ‡Ï ¨‰ÏÂÒÂ˜Ï ˙¯·ÂÁÓ ÔÈ‡Æ‰Ê ÏÈ‚¯˙· ˘Ó˙˘ · ¨ÈÏÏÎ ¯˙ÂÈ ‡Â‰ ¨®job© ‰„Â·Ú ‡¯˜ È˘‰ ‚ÂÒ‰

∫‰„·ÚÏ ÍÈω˙ ÍÂÈ˘

HANDLE CreateJobObject (LPSECU-RITY_ATTRIBUTES sa,

LPCTSTR name);BOOL AssignProcessToJobObject(HANDLE job,

HANDLE process);

ÌÚ ®Ï„ÁÓ‰ ˙¯È¯· ¯Â·Ú NULL© ˙ÂÂ˙ ˙‡˘¯‰ ÌÚ ‰„Â·Ú ˙¯ˆÂÈ ‰Â˘‡¯‰ ˙ίÚÓ‰ ˙‡È¯˜ÍÈÈÂ˘Ó Âȇ ÍÈω˙ Ì¢ ¨˙¯ˆÂ ‰„·ډ ¯˘‡Î Ɖ„·ÚÏ ‰‰ÊÓ ‰¯ÈÊÁÓ ‰‡È¯˜‰ ÆÔÂ˙ Ì˘¯˘‡Î ¨‰„·ÚÏ ÍÈω˙ ˙ÎÈÈ˘Ó ‰È˘‰ ˙ίÚÓ‰ ˙‡È¯˜ Æ®‰˙‡ ¯ˆÈ˘ ÍÈω˙‰ ‡Ï Ì‚© ‰ÈÏ‡È„È ÏÚ ‡Ï ‰„·ډ Ì˘ È„È ÏÚ ‡Ï ¨ÁÂ˙Ù ‰‰ÊÓ È„È ÏÚ ÌÈÈÈÂˆÓ ‰„·ډ Ì‚Â ÍÈω˙‰ Ì‚

ÆÍÈω˙‰ ¯ÙÒÓÌÈÎÈÈÂ˘Ó ÂÈ‰È Âȇˆ‡ˆ ÏÎ Ì‚Â ÍÈω˙‰˘ ‚‡„Ï ÏÏΠͯ„· ‡È‰ ÍÂÈ˘‰ ˙¯ËÓ ¨¯ÂÓ‡ÎÍÈω˙‰ ¨Â˙‡ ÌÈÎÈÈ˘Ó ¯ˆÂ ‡Â‰˘ ¯Á‡Ï ÏÈ‚¯ ÔÙ‡· ÍÈω˙ ÌȯˆÂÈ Ì‡ Ìχ Ɖ„·ÚϯÁ‡Ï ¨‰ÊÎ ‰¯˜Ó· Ɖ„·ÚÏ Â˙‡ ÍÈÈ˘Ï ˜ÈÙÒ˘ ÈÙÏ ÌÈÙÒ ÌÈÎÈω˙ ¯ÂˆÈÏ ÏÂÏÚ ˘„Á‰ÚÂÓÏ ˙Ó ÏÚ Æ‰„·ÚÏ ÂÎÈÈÂ˘È ‡Ï ¯ˆÈ ¯·Î˘ Ìȇˆ‡ˆ‰ Ï·‡ ¨ÍÈÈÂ˘Ó ‰È‰È ÌÓ‡ ‡Â‰ ÂÎÂÈ˘Ê‡ ˜¯Â ¨‰„·ÚÏ Â˙‡ ÍÈÈ˘Ï ¨ıÂ¯Ï ÏÎÂÈ ‡Ï˘ ÍÎ ˘„Á‰ ÍÈω˙‰ ˙‡ ¯ÂˆÈÏ Íȯˆ ¨‰ÊÎ ·ˆÓÌÚ Â˙‡ ¯ÂˆÈÏ ˘È ¨Â˙¯ÈˆÈ ÌÚ „ÈÈÓ ıÂ¯Ï ÏÈÁ˙È ‡Ï ÍÈω˙‰˘ È„Î ÆıÂ¯Ï ÏÈÁ˙‰Ï ÂÏ ¯È˙‰Ï‡Â¯˜Ï ˘È ¨‰„·ÚÏ ÂÎÂÈ˘ ¯Á‡Ï ıÂ¯Ï ÏÈÁ˙‰Ï ÂÏ ¯˘Ù‡Ï È„Î ÆCREATE_SUSPENDED Ï‚„‰

˙ίÚÓ‰ ˙‡È¯˜Ï

DWORD ResumeThread(HANDLE thread);

‰¯ÈÊÁÓ Âʉ ‰‡È¯˜‰ ÔÂÏ˘Î Ï˘ ‰¯˜Ó· ÆÍÈω˙‰ Ï˘ È˘‡¯‰ ËÂÁ‰ Ï˘ ‰‰ÊÓ ‡Â‰˘ ËÓ‚¯‡ ÌÚ˙·ÈÒ‰ ¯ÙÒÓ ˙‡ ‰ÈË˜Ó Âʉ ‰‡È¯˜‰ ª˙·ÈÒ ¯ÙÒÓ ÏÏ‚· ‰ˆÈ¯Ó ‰Ú˘ÂÓ ˙ÂÈ‰Ï ÏÂÎÈ ËÂÁ© Æ-1

®ÆıÂ¯Ï ÍÈ˘Ó‰Ï ˙ÚÎ ÏÂÎÈ ËÂÁ‰˘ ‰¯˜Ó· 0 ¨Â¯˙¢ ˙·ÈÒ‰ ¯ÙÒÓ ˙‡ ‰¯ÈÊÁÓ „Á‡·

17

Page 18: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

∫‰ÓÏ˘ ‰„Â·Ú Ï˘ ‰ˆÈ¯ ˙˜ÒÙ‰

BOOL TerminateJobObject(HANDLE job,UINT exit_code);

‰Ï˘ ‰‰ÊÓ‰˘ ‰„·ÚÏ ÌÈÎÈÈ¢Ӣ ÌÈÎÈω˙‰ ÏÎ Ï˘ Ì˙ÏÂÚÙ ˙‡ ‰˜ÈÒÙÓ ‰‡È¯˜‰‰‡È¯˜‰Ó ‡ CreateJobObject ‰‡È¯˜‰Ó Ï·˜˙‰˘ ‰‰ÊÓ‰ ‡Â‰ ‰‰ÊÓ‰ ÆÔÂ˙¯ÊÁÂÈ˘ ͯډ ‡Â‰ È˘‰ ËÓ‚¯‡‰ Æ‰Ó˘ ÈÙ ÏÚ ‰„Â·Ú ˙Á˙ÂÙ˘ OpenJobObject

Ɖ„·ڷ˘ ÌÈÎÈω˙‰ ÏÎÓ

ÌÈÏÈÚÙÓ ¯˘‡Î ÆÌÈί„ È˙˘· ÏÈÚÙ‰Ï Ô˙È˘ ex-job Ì˘· ˙ÈÎÂ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰˙¯Â˘˘ ˘„Á ÍÈω˙ ˙¯ˆÂÈ ‰˘„Á ‰„Â·Ú ˙¯ˆÂÈ ‡È‰ ¨¯˙ÂÈ Â‡ ÌÈËÓ‚¯‡ È˘ ÌÚ ‰˙‡¨˘„Á‰ ÍÈω˙‰ ˙‡˙¯ˆÂÈ ‡È‰˘ ÈÙÏ Æex-job Ï˘ ÍÏÈ‡Â È˘‰ ÌÈËÓ‚¯‡‰ ‡È‰ ÂÏ˘ ‰„˜ىÏÁ˙‡Ӣ ex-job Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰ ‡Â‰ ÂÓ˘˘ È„È ÒÂÙȇ ‚ÂÒÓ Ú¯ȇ ˙¯ˆÂÈ ‡È‰Ú¯ȇ‰ ¯˘‡Î ¨ÚÂ¯È‡Ï ‰ÎÁÓ ˙ÈÎÂ˙‰ ¨ıÂ¯Ï ÏÈÁ˙‰ ˘„Á‰ ÍÈω˙‰˘ ¯Á‡Ï ÆÔÓÂÒÓ ‡Ï ·ˆÓÏ

Ɖ„·ÚÏ ÌÈÎÈÈ¢Ӣ ÌÈÎÈω˙‰ ÏÎ ˙ÏÂÚÙ ˙‡ ‰˜ÈÒÙÓ ‡È‰ ¨ÔÓÂÒӇ‰ ÂÓ˘˘ Ú¯ȇ‰ ˙‡ ˙Á˙ÂÙ ‡È‰ ¨„·Ï· „Á‡ ËÓ‚¯‡ ÌÚ ˙ÈÎÂ˙‰ ˙‡ ÌÈÏÈÚÙÓ ¯˘‡Î¯ÂÓ‡˘ ‰Ó ¨Â˙‡ ˙ÓÒÓ OpenEvent ˙ίÚÓ‰ ˙‡È¯˜ ˙ÂÚˆÓ‡· ‰Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰

ÆıÂ¯Ï ˜ÈÒÙ‰Ï ÔÎÏ Ì„Â˜ Èχ ÂÏÁ˙‰˘ ‰„·ÚÏ Ì¯‚Ï∫˙ÈÎ˙‰ Ï˘ ‰Ó‚Â„Ï ‰ÏÚÙ‰ ‰‰

↓ the name of the eventc:\> ex-job job-event netstat.exe -e 5

↑ the command line to run in the new job

‰„˜ى ˙‡ ¯Á‡ ÔÂÏÁ ÍÂ˙Ó ÌÈÏÈÚÙÓ ¨‰„·ډ ˙ÏÂÚÙ ˙‡ ˜ÈÒÙ‰Ï È„Î

c:\> ex-job job-event

ÆÏÈ‚¯˙‰ ÌÚ „ÁÈ ˙˜ÙÂÒÓ˘ CloneProcess ˙ÈÎÂ˙· ¢Ó˙˘‰ ¨˙ÈÎÂ˙‰ ˙‡ ˜Â„·Ï È„ÎÈ˘ ˙¯ˆÂÈ ˙ÈÎÂ˙‰ Æ˙ÂÈ˘· ÔÓÊ Í˘Ó ¨„Á‡ ËÓ‚¯‡ ÌÚ Âʉ ˙ÈÎÂ˙‰ ˙‡ ıȯ‰Ï ÍȯˆÏ˘ ‡Â‰ ˘ه‰ ¨ÔÓÊ Í¯Â‡Ï Æ‰˙ÏÂÚÙ ˙‡ ˙ÓÈÈÒÓ Ê‡Â ÔÂ˙‰ ÔÓʉ Í˘Ó Ï˘ ˘¯Ù‰· ÌÈÎÈω˙ÆÌÈÓÈȘ Ìȇ ¯·Î Ì˙‡ ¯ˆÈ˘ ÌÈÎÈω˙‰ ¯˘‡Î ¨ÌÈÎÈω˙ Ï˘ ÈχȈÂÙÒ˜‡ ¯ÙÒÓ ˙¯ÈˆÈÏ‰Ó ˙¯ÊÚ· Ì˙ÏÂÚÙ ˙‡ ÌÈÈÒÏ ÂÒ ˙ÂÓÈ˘Ó‰ Ï‰Ó ˙¯ÊÚ· ÂÏω ÌÈÎÈω˙‰ ˙¯ÈˆÈ· ÂÙˆ˙¯ÊÚ· Ì˙ÏÂÚÙ ˙‡ ÌÈÈÒÏ ÂÒ ˙ÚÎ Æ®¯ˆ˜ ÌÈÎÈω˙ ˙¯ÈˆÈ ÔÈ· ÔÓʉ Í˘Ó Ì‡ Ϙ ‡Ï© ˙ÂÓÈ˘Ó‰¨È„Ó ÌÈ·Â¯Ó ÌÈÎÈω˙ ˙¯ÈˆÈÓ ÚÓ‰Ï ˙Ó ÏÚ Æ¯˙ÂÈ Ï˜ ˙ÂÈ‰Ï ¯ÂÓ‡ ‰Ê ªÌ˙·˙΢ ˙ÈÎÂ˙‰Æ¯˙ÂÈ Â‡ ˙ÂÈ˘ ±∞≠Î Ï˘ ÔÓÊ Í˘Ó ÌÚ ‰ÏÈÁ˙· ˙ÂÁÙÏ CloseProcess ˙‡ ıȯ‰Ï ȇ„Î

±∏

Page 19: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Managing Process Groups

˙ÂÏÂÚÙ Úˆ·Ï ÌÈÎÈω˙ Ï˘ ˙ˆ·˜ ˘¯ÂÙÓ ÔÙ‡· ‚ˆÈÈÏ ÏÏΠͯ„· ˙¯˘Ù‡Ó ‰ÏÚÙ‰ ˙ÂίÚÓprocess© ÌÈÎÈω˙ ˙¯Â·Á ˙‡¯˜˘ ¨ÂÊÎ ‰ˆÂ·˜ Æ˙Á‡ ˙·· ‰ˆÂ·˜ Ï˘ ÌÈÎÈω˙‰ ÏÎ ÏÚÍÈω˙ ‰Èχ ÍÈÈ˘Ï Ô˙È ¨˙ίÚÓ ˙‡È¯˜ È„È ÏÚ ˘¯ÂÙÓ ÔÙ‡· ˙¯ˆÂ ®job© ‰„Â·Ú Â‡ ®groupÍÈÈÂ˘Ó ˘„Á‰ ÍÈω˙‰ ¨ÛÒ ÍÈω˙ ¯ˆÂÈ ÂÊÎ ‰¯Â·ÁÏ ÍÈÈ¢Ӣ ÍÈω˙ ¯˘‡Î ÆÌÈÎÈω˙ ‡̯‚ ‰Ê‰ Ô‚Ӊ Æ®˙¯Á‡ ‰¯Â·ÁÏ ˙¢¯ÂÙÓ Â˙‡ ÌÈÎÈÈ˘Ó Ì‡ ‡Ï‡© ‰¯Â·ÁÏ ˙ÈËÓ¡̇ Ì‚ ¨ÌȯÁ‡ ÌÈÎÈω˙ ÌȯˆÂÈ ÌÈÎÈω˙ ·˘ ¨ÌÏ˘ ÌÈÎÈω˙ ıÚ ‰¯Â·ÁÏ ÍÈÈ˘Ï Ô˙È˘ ÍÎϨ®shells© ˙ÙËÚÓ ˙ÂÈÎÂ˙ Æ˘¯ÂÙÓ ÔÙ‡· ‰¯Â·ÁÏ ÌÓˆÚ ˙‡ ÌÈÎÈÈ˘Ó Ìȇ ÂÏω ÌÈÎÈω˙‰˙ÈÎÂ˙ Ï˘ ‰˙ÏÂÚÙ ˙‡ ˜ÈÒÙ‰Ï ˘Ó˙˘ÓÏ ¯˘Ù‡Ï ˙Ó ÏÚ ‰Ê‰ Ô‚ӷ ˙¢Ó˙˘Ó ¨Ï˘Óωȇ ˙ÙËÚÓ‰˘ ¨ÌÈÎÈω˙ Ï˘ Ï„‚ ¯ÙÒÓ ‰¯ˆÈ Âʉ ˙ÈÎÂ˙‰ ̇ Ì‚ ¨˙ÙËÚÓ‰ ͯ„ ıȯ‰˘

ÆÌÈÓÈȘ Ìȇ ¯·Î Èχ Ì˙‡ ¯ˆÈ˘ ÌÈÎÈω˙‰ ‡ ÍÈω˙‰˘Â ¨ÌÓÂÈ˜Ï ÏÏÎ ˙Ú„ÂÓprocess© ÌÈÎÈω˙ ˙¯Â·Á ‡¯˜ „Á‡ ‚ÂÒ ÆÌÈÎÈω˙ ˙¯·Á Ï˘ ÌÈ‚ÂÒ È˘ ˘È ˙ÂÂÏÁ·˜Â˙È È„È ÏÚ ÂÊÎ ‰¯Â·Á ÌȯˆÂÈ ÆÒ˜ÂÈÏ ҘÈÂÈ· ÌÈÎÈω˙ ˙¯·ÁÏ ‰Ó„ ‰Ê‰ ‚ÂÒ‰ Æ®group‰Èχ ÍÈÈ˘ ˜˙¢ ÍÈω˙‰˘ ‰˘„Á ‰¯Â·Á ¯ˆÂÈ ‰Ê ª‰Èχ ÍÈÈ˘ ‡Â‰˘ ‰¯Â·Á‰Ó ÍÈω˙ Ï˘ÏÎ ¯˘‡Î ˜¯ · ˘Ó˙˘‰Ï Ô˙È˘ ÔÂÈÎÓ ¨È„ÓÏ Ï·‚ÂÓ ‰Ê‰ ‚ÂÒ‰ ÆÍÈω˙‰ ¯ÙÒÓ ‡Â‰ ‰Ó˘˘ÂÏÏΠͯ„· ˙¯˘ ˙ÂÎÂ˙ ˙ÂÈÙ¯‚ ˙ÂÈÎÂ˙˘ ÔÂÈÎÓ Æ˙Á‡ ‰ÏÂÒÂ˜Ï Ìȯ·ÂÁÓ ‰¯Â·Á· ÌÈÎÈω˙‰

Ɖʉ ‚ÂÒ· ˘Ó˙˘‰Ï ¯˘Ù‡ „ÈÓ˙ ‡Ï ¨‰ÏÂÒÂ˜Ï ˙¯·ÂÁÓ ÔȇƇ‰˘ ‚ÂÒ ÏÎÓ ÍÈω˙ ÂÈχ ÍÈÈ˘Ï ¯˘Ù‡Â ÈÏÏÎ ¯˙ÂÈ ‡Â‰ ¨®job© ‰„Â·Ú ‡¯˜ È˘‰ ‚ÂÒ‰

ÆÌÈÎÈω˙ ˙¯·Á· ˘Ó˙˘ ‰Ê‰ ÏÈ‚¯˙· Ï·‡

˘„Á ÍÈω˙ ÌȯˆÂÈ ¯˘‡Î ˙¯ˆÂ ‰˘„Á ÌÈÎÈω˙ ˙¯Â·Á ∫‰˘„Á ÌÈÎÈω˙ ˙¯Â·Á ˙¯ÈˆÈÆCreateProcess˙ίÚÓ‰ ˙‡È¯˜Ï CREATE_NEW_PROCESS_GROUPÏ‚„‰ ˙‡ ÌȯȷÚÓ‡χ Âʉ ‰ˆÂ·˜Ï ˙ÈËÓ¡ ÂÎÈÂ˘È ¨Ì‰Ï˘ Ìȇˆ‡ˆ‰Â ¨¯ÂˆÈÈ ˘„Á‰ ÍÈω˙‰˘ ÌÈÎÈω˙‰ ÏÎÌÚ Â¯ˆÈ˘ ÍÈω˙‰ Ï˘ ¯ÙÒÓ‰ ‡Â‰ ÌÈÎÈω˙‰ ˙¯Â·Á Ì˘ ÆÏ‚„‰ Â˙‡ ÌÚ ¯ˆÂÂÈÈ Ì‰Ó „Á‡ ̇Ɖ¯Â·Á‰ ˙‡ ÌÈ·ÈÎ¯Ó˘ ÌÈÎÈω˙‰ ıÚ Ï˘ ˘¯Â˘‰ ¨CREATE_NEW_PROCESS_GROUP Ï‚„‰

∫‰ÓÏ˘ ‰„Â·Ú Ï˘ ‰ˆÈ¯ ˙˜ÒÙ‰

BOOL GenerateConsoleCtrlEvent(DWORD control_event,DWORD process_group);

ÌÈÎÈω˙Ï ˙ӯ‚ ¨CONTROL_BREAK_EVENT‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ Í¯Ú ¯˘‡Î ¨Âʉ ‰‡È¯˜‰‰¯Âˆ ‰˙‡· ·È‚‰Ï process_group ‡Â‰ ‰¯ÙÒÓ˘ ®ÌÏÂÎ ‡Ï Èχ© ÌÈÎÈω˙‰ ˙¯Â·Á Ï˘ÌÈÎÈω˙‰ Ï˘ ‰ˆÈ¯‰ ˙˜ÒÙ‰Ï Ì¯Â‚ ‰Ê ÏÏΠͯ„· Æcontrol-break ˙˘˜‰Ï ÌÈ·È‚Ó Ì‰˘‰˙Â‡Ï Ìȯ·ÂÁӢ ‰¯Â·ÁÏ ÌÈÎÈÈ˘˘ ÌÈÎÈω˙‰ ̉ Â·È‚È˘ ÌÈÎÈω˙‰ Ɖ‡È¯˜Ï ÌÈ·È‚Ó˘ÌÈÎÈω˙ ÆGenerateConsoleCtrlEvent ‰‡È¯˜‰ ˙‡ Úˆ·Ó˘ ÍÈω˙‰ ÂÓÎ ‰ÏÂÒ˜

ÆÂ·È‚È ‡Ï ‰¯Â·Á· ÌȯÁ‡˙‡ ˜¯ ÚÈÙ˘Ó ‡Â‰ Ï·‡ ¨Ô¢‡¯‰ ËÓ‚¯‡‰ ¯Â·Ú ȘÂÁ CONTROL_C_EVENT ͯډ Ì‚

ƉÓÈÏ˘ ÌÈÎÈω˙ ˙¯Â·Á ÏÚ ‡Ï ¨È˘‰ ËÓ‚¯‡· ÔÂ˙ ¯ÙÒÓ˘ „„· ÍÈω˙·È‚˙˘ ‰¯‚˘ Ú·˜Ï Ô˙È SetConsoleCtrlHandler ˙ίÚÓ‰ ˙‡È¯˜ ˙ÂÚˆÓ‡·È·ÈΠ¨˙ίÚÓ‰Ó ˙Ø˘Ó˙˘Ó‰ Ï˘ ‰‡ÈˆÈ ¨‰ÏÂÒ˜‰ Ï˘ ‰¯È‚Ò ÈÚÂ¯È‡Ï Ì‚Â ¨ÂÏω ÌÈÚ¯ȇÏÈ„È ÏÚ ˙ÈÎÂ˙‰Ó ˙¯„ÂÒÓ ‰‡ÈˆÈÏ ‚‡„Ï ‡È‰ ÏÂÙÈˉ ˙¯‚˘ ˙¯ËÓ ÏÏΠͯ„· Æ˙ίÚÓ‰

ÆÌȯÁ‡ ÌÈ·‡˘Ó ¯Â¯Á˘Â ÌÈÈÓÊ ÌȈ·˜ ˙˜ÈÁÓ ÂÓÎ ˙ÂÏÂÚÙ

ÌÈÏÈÚÙÓ ¯˘‡Î ÆÌÈί„ È˙˘· ÏÈÚÙ‰Ï Ô˙È˘ ex-job Ì˘· ˙ÈÎÂ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰‡Â‰ ‰Ï˘ ˘¯Â˘‰˘ ‰˘„Á ÌÈÎÈω˙ ˙¯Â·Á ˙¯ˆÂÈ ‡È‰ ¨¯˙ÂÈ Â‡ ÌÈËÓ‚¯‡ È˘ ÌÚ ‰˙‡

±π

Page 20: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

‡È‰˘ ÈÙÏ Æex-pgroup Ï˘ ÍÏÈ‡Â È˘‰ ÌÈËÓ‚¯‡‰ ‡È‰ ÂÏ˘ ‰„˜ى ˙¯Â˘˘ ˘„Á ÍÈω˙Ô¢‡¯‰ ËÓ‚¯‡‰ ‡Â‰ ÂÓ˘˘ È„È ÒÂÙȇ ‚ÂÒÓ Ú¯ȇ ˙¯ˆÂÈ ‡È‰ ¨˘„Á‰ ÍÈω˙‰ ˙‡ ˙¯ˆÂÈ˙ÈÎÂ˙‰ ¨ıÂ¯Ï ÏÈÁ˙‰ ˘„Á‰ ÍÈω˙‰˘ ¯Á‡Ï ÆÔÓÂÒÓ ‡Ï ·ˆÓÏ ÏÁ˙‡Ӣ ex-pgroup Ï˘ÌÈÎÈÈ¢Ӣ ÌÈÎÈω˙‰ ÏÎ ˙ÏÂÚÙ ˙‡ ‰˜ÈÒÙÓ ‡È‰ ¨ÔÓÂÒÓ Ú¯ȇ‰ ¯˘‡Î ¨ÚÂ¯È‡Ï ‰ÎÁÓ

Ɖ¯Â·Áχ‰ ÂÓ˘˘ Ú¯ȇ‰ ˙‡ ˙Á˙ÂÙ ‡È‰ ¨„·Ï· „Á‡ ËÓ‚¯‡ ÌÚ ˙ÈÎÂ˙‰ ˙‡ ÌÈÏÈÚÙÓ ¯˘‡Î¯ÂÓ‡˘ ‰Ó ¨Â˙‡ ˙ÓÒÓ OpenEvent ˙ίÚÓ‰ ˙‡È¯˜ ˙ÂÚˆÓ‡· ‰Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰

ÆıÂ¯Ï ˜ÈÒÙ‰Ï ÔÎÏ Ì„Â˜ Èχ ÂÏÁ˙‰˘ ÌÈÎÈω˙‰ ˙¯Â·ÁÏ Ì¯‚Ï∫˙ÈÎ˙‰ Ï˘ ‰Ó‚Â„Ï ‰ÏÚÙ‰ ‰‰

↓ the name of the eventc:\> ex-pgroup job-event netstat.exe -e 5

↑ the command line to run in the new job

‰„˜ى ˙‡ ¯Á‡ ÔÂÏÁ ÍÂ˙Ó ÌÈÏÈÚÙÓ ¨‰„·ډ ˙ÏÂÚÙ ˙‡ ˜ÈÒÙ‰Ï È„Î

c:\> ex-pgroup job-event

ÆÏÈ‚¯˙‰ ÌÚ „ÁÈ ˙˜ÙÂÒÓ˘ CloneProcess ˙ÈÎÂ˙· ¢Ó˙˘‰ ¨˙ÈÎÂ˙‰ ˙‡ ˜Â„·Ï È„ÎÈ˘ ˙¯ˆÂÈ ˙ÈÎÂ˙‰ Æ˙ÂÈ˘· ÔÓÊ Í˘Ó ¨„Á‡ ËÓ‚¯‡ ÌÚ Âʉ ˙ÈÎÂ˙‰ ˙‡ ıȯ‰Ï ÍȯˆÏ˘ ‡Â‰ ˘ه‰ ¨ÔÓÊ Í¯Â‡Ï Æ‰˙ÏÂÚÙ ˙‡ ˙ÓÈÈÒÓ Ê‡Â ÔÂ˙‰ ÔÓʉ Í˘Ó Ï˘ ˘¯Ù‰· ÌÈÎÈω˙ÆÌÈÓÈȘ Ìȇ ¯·Î Ì˙‡ ¯ˆÈ˘ ÌÈÎÈω˙‰ ¯˘‡Î ¨ÌÈÎÈω˙ Ï˘ ÈχȈÂÙÒ˜‡ ¯ÙÒÓ ˙¯ÈˆÈÏ‰Ó ˙¯ÊÚ· Ì˙ÏÂÚÙ ˙‡ ÌÈÈÒÏ ÂÒ ˙ÂÓÈ˘Ó‰ Ï‰Ó ˙¯ÊÚ· ÂÏω ÌÈÎÈω˙‰ ˙¯ÈˆÈ· ÂÙˆ˙¯ÊÚ· Ì˙ÏÂÚÙ ˙‡ ÌÈÈÒÏ ÂÒ ˙ÚÎ Æ®¯ˆ˜ ÌÈÎÈω˙ ˙¯ÈˆÈ ÔÈ· ÔÓʉ Í˘Ó Ì‡ Ϙ ‡Ï© ˙ÂÓÈ˘Ó‰¨È„Ó ÌÈ·Â¯Ó ÌÈÎÈω˙ ˙¯ÈˆÈÓ ÚÓ‰Ï ˙Ó ÏÚ Æ¯˙ÂÈ Ï˜ ˙ÂÈ‰Ï ¯ÂÓ‡ ‰Ê ªÌ˙·˙΢ ˙ÈÎÂ˙‰Æ¯˙ÂÈ Â‡ ˙ÂÈ˘ ±∞≠Î Ï˘ ÔÓÊ Í˘Ó ÌÚ ‰ÏÈÁ˙· ˙ÂÁÙÏ CloseProcess ˙‡ ıȯ‰Ï ȇ„Î

≤∞

Page 21: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Mapping Files to Memory

ÔÈ· ÌÈÂ˙ ¯È·Ú‰Ï ˙Ó ÏÚ ˙‡Ê ˙ÏÂÎÈ· ˘Ó˙˘‰Ï Ô¯ÎÈÊÏ ÌȈ·˜ ˙ÂÙÓÏ „ÓÏ ‰Ê ÏÈ‚¯˙·Æ˙ÈÓÊ Â· ˙ˆ¯˘ ˙ÂÈÎÂ˙ È˙˘

ÌˆÚ ÌȯˆÂÈ Ô¢‡¯· ÆÌÈ·Ï˘ È˘· ˙ÂÂÏÁ· Úˆ·˙Ó Ô¯ÎÈÊÏ ı·Â˜ ÈÂÙÈÓ ∫Ô¯ÎÈÊÏ ı·Â˜ ÈÂÙÈÓ‡© ı·Â˜· ÌÈÈÂÒÓ ¯ÂÊȇ ÌÈÙÓÓ È˘·Â ¨Ô¯ÎÈÊÏ ı·Â˜· ÌÈ¢ ÌȘÏÁ ˙ÂÙÓÏ ¯˘Ù‡Ó˘ ¨ÈÂÙÈÓ

ÆÔ¯ÎÈÊÏ ®ı·Â˜‰ ÏÎ ˙‡

HANDLE CreateFileMapping(HANDLE file,LPSECURITY_ATTRIBUTES sa,DWORD protection,DWORD max_size_high,DWORD max_size_lowLPCTSTR mapping_name);

ͯډ ‡ ¨ÔÎÏ Ì„Â˜ ‰Á˙Ù ˙ÈÎÂ˙‰˘ ı·Â˜ Ï˘ ‰‰ÊÓ ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ı·Â˜ ‡Ï ˜ÒÈ„· Û„ل‰ ¯ÂÊÈ‡Ó ˜ÏÁ Ô¯ÎÈÊÏ ˙ÂÙÓÏ ÌÈ˘˜·Ó̇ INVALID_HANDLE_VALUENULL ‡ ¨ÂÓˆÚ ÈÂÙÈÓ‰ ̈ÚÏ ˙˙Ï ÌÈÈÈÂÚÓ Â‡˘ ˙‡˘¯‰ ‚ˆÈÈÓ È˘‰ ËÓ‚¯‡‰ ÆÌÈÈÂÒÓı·Â˜‰ ÍÂ˙Ó ‡Â¯˜Ï ‰ˆ¯ ̇‰ ÔÈÈˆÓ È˘ÈÏ˘‰ ËÓ‚¯‡‰ Æ˙‡˘¯‰ ÔÈÈˆÏ ÌÈÈÈÂÚÓ Ôȇ Ì‡È„È ÏÚ ˙‚ˆÂÈÓ ÂÏω ˙Âȯ˘Ù‡‰ Æ·Â˙ÎÏ Ì‚Â ‡Â¯˜Ï Ì‚ ‡ ¨ÂÎÂ˙Ï ·Â˙ÎÏ ¨Ô¯ÎÈÊÏ ‰ÙÂÓӉ̂ Ô˙È ÆPAGE_READWRITE≠ ¨PAGE_WRITEONLY ¨PAGE_READONLYÌÈÈÏ·ÓÈÒ ÌÈίډÈ˯٠˜˙ÂÚ ¯ÂˆÈÏ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ‰¯ÂÓ˘ ¨PAGE_WRITECOPY ÈÏ·ÓÈÒ‰ ͯډ ˙‡ ¯È·Ú‰ÏÌȇ·‰ ÌÈËÓ‚¯‡‰ È˘ ÆÈÂÙÈÓ‰ ÌˆÚ Í¯„ ‰˘Ó ˙ÈÎÂ˙‰˘ ı·Â˜· ÌÈÙ„ Ï˘ Û„ل‰ ¯ÂÊȇ·˙¯·ÚÂÓ˘ ¨˙ÂÈ·ÈÒ ∂¥≠· ‚ˆÂÈÓ ÈÏÓÈÒ˜Ó‰ τ‚‰ ÆÈÂÙÈÓ‰ Ï˘ ÈÏÓÈÒ˜Ó‰ τ‚‰ ˙‡ ÌÈÈȈӇ‰ ÈÏÓÈÒ˜Ó‰ τ‚‰˘ ˙ÈÈˆÓ ∞ τ‚‰ ˙¯·Ú‰ Æ„Á‡ ÏÎ ˙ÂÈ·ÈÒ ≥≤ Ï˘ ÌÈËÓ‚¯‡ È˘·Ô¯Á‡‰ ËÓ‚¯‡‰ Ʈۄل‰ ¯ÂÊȇ ˙‡ ÌÈÙÓÓ Ì‡ ∞ τ‚‰ ˙‡ ¯È·Ú‰Ï ¯ÂÒ‡© ı·Â˜‰ τ‚ Ë¢Ù

Æ˙ÂÈÎÂ˙ ‰ÓÎÓ Â· ˘Ó˙˘‰Ï ÌÈÈÈÂÚÓ Â‡ ̇ ¨ÈÂÙÈÓ‰ ̈ÚÏ ÌȘÈÚÓ Â‡˘ Ì˘ ‡Â‰˙·˘Á ‰È‡ ÂÊ© ÂÈχ ‰‰ÊÓ ‰¯ÈÊÁÓ ‰‡È¯˜‰ ¨¯·Î ÌÈȘ ÔÂ˙‰ Ì˘· ÈÂÙÈÓ ÌˆÚ Ì‡Í¯Ú‰ ˙‡ ‰¯ÈÊÁÓ GetLastError() ‰Èˆ˜ÂÙÏ ‰‡È¯˜ ‰ÊÎ ‰¯˜Ó· Ï·‡ ¨®‰‡È‚˘

ÆERROR_ALREADY_EXISTSÈÂÙÈÓ ÌˆÚÏ ‰‰ÊÓ Ï·˜Ï Ô˙È ÆCloseHandle ˙¯ÊÚ· ¨ÏÈ‚¯Î ¨Ìȯ¯Á˘Ó ÈÂÙÈÓ‰ ÌˆÚ ˙‡

ÆOpenFileMapping ‰Èˆ˜ÂÙ‰ ˙¯ÊÚ· ÌÈȘƉ‡·‰ ‰Èˆ˜ÂÙÏ ‰‡È¯˜ È„È ÏÚ ÌȯˆÂÈ ÂÓˆÚ ÈÂÙÈÓ‰ ˙‡

void* MapViewOfFile(HANDLE file_mapping,DWORD access,DWORD file_offset_high,DWORD file_offset_lowSIZE_T view_size);

BOOL UnmapViewOfFile(void* address);

¨OpenFileMappin ‡ CreateFileMapping È„È ÏÚ ¯ÊÁ‰˘ ‰‰ÊÓ ˙Ï·˜Ó ‰‡È¯˜‰ËÓ‚¯‡‰ Æı·Â˜‰ ˙‡ ‰'˙ŸtÈ!Ó ‰ÏÚÙ‰‰ ˙ίÚÓ ÂÈχ˘ Ô¯ÎÊ· ¯ÂÊÈ‡Ï ÚÈ·ˆÓ ‰¯ÈÊÁÓ‰Ó„ ËÓ‚¯‡‰© ‰ÙÂÓÓ‰ ı·Â˜Ï ̉· ÌÈÈÈÂÚÓ Â‡˘ ‰˘È‚‰ È‚ÂÒ ˙‡ ¯‡˙Ó È˘‰∫®ÌȯÁ‡ ÌÈÈÏ·ÓÈÒ ÌÈίڷ ˘Ó˙˘Ó Ï·‡ ¨ÈÂÙÈÓ‰ ÌˆÚ ˙¯ÈˆÈ· protection ËÓ‚¯‡Ï

≤±

Page 22: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ÆFILE_MAP_COPY≠ ¨FILE_MAP_ALL_ACCESS ¨FILE_MAP_WRITE ¨FILE_MAP_READËÓ‚¯‡‰Â ¨Ô¯ÎÊÏ ‰ÙÂÓÈ˘ ı·Â˜· Ô¢‡¯‰ ˙È·‰ ¨ËÒȉ‰ ˙‡ ÌÈÈÈˆÓ Ìȇ·‰ ÌÈËÓ‚¯‡‰ È˘

Æı·Â˜‰ ÏÎ ˙‡ ˙ÂÙÓÏ ÌÈÈÈÂÚÓ Ì‡ ∞ ‡ ¨‰ÙÂÓÈ˘ Áˢ‰ τ‚ ˙‡ Ô¯Á‡‰˙Ó ÏÚ Æ‰ÏÚÙ‰‰ ˙ίÚÓ Ï˘ Û„‰ τ‚ Ï˘ ‰ÏÂÙÎ ˙ÂÈ‰Ï ·ÈÈÁ ı·Â˜‰ ˙ÏÈÁ˙Ó ËÒȉ‰¨„„· ËÓ‚¯‡ ˙Ï·˜Ó˘ GetSystemInfo ‰¯‚˘Ï ‰ÏÈÁ˙ ‡Â¯˜Ï ˘È ¨Û„‰ τ‚ ˙‡ ¯¯·ÏÛ„‰ τ‚ ˙‡ ¯‡˙Ó ÂÏ˘ dwPageSize ‰„˘‰˘ ¨SYSTEM_INFO ÒÂÙÈËÓ ‰·Ó Ï˘ ˙·Â˙Î

ÆÌÈ˙··ÌÈÂ˙ ˙˜˙Ú‰ ˙ÂÁÙ© ÌÈÏ„‚ ÌȈ·˜Ï ‰˘È‚Ï ‰Ë¢Ù ‰ÏÈÚÈ ‰ËÈ˘ ‡È‰ ÌȈ·˜ ÈÂÙÈÓÔÈ· ÌÈÂ˙ ˙¯·Ú‰ ˙¯˘Ù‡Ó ‡È‰Â ®‰ÏÈ‚¯ ‰·È˙Π‰‡È¯˜ ¯˘‡Ó ‰ÏÚÙ‰‰ ˙ίÚÓ ÍÂ˙·

Æ· ÌÈ˘Ó˙˘Ó ÌÈÎÈω˙ ¯ÙÒÓ˘ „Á‡ ÈÂÙÈÓ ÌˆÚ ˙¯ÈˆÈ È„È ÏÚ ÌÈÎÈω˙

˙„„· ˙‡ Ï˘ ËÓ‚¯‡ ÌÚ ıȯ‰Ï Ô˙È˘ win32mmap Ì˘· ˙ÈÎÂ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰ËÓ‚¯‡ ÌÚ ÌÚÙ ÏÎ ¨®ÌÈ¢ ˙ÂÂÏÁ·© ÌÈÈÓÚÙ ˙ÈÎÂ˙‰ ˙‡ ıÈ¯Ó ˘Ó˙˘Ó‰ ¯˘‡Î Æw ‡ rÌÚ ı¯Â‰˘ ˜˙Âډ w ËÓ‚¯‡‰ ÌÚ ‰ˆ¯Â‰˘ ˙ÈÎÂ˙Ï ËϘΠËÒ˜Ë „ÈϘ‰Ï ÏÂÎÈ ‡Â‰ ¨¯Á‡¯·ÚÂÈ „Ϙ‰˘ ËҘˉ Ɖ„Ϙ‰ ‰ÓÏ˘ ‰¯Â˘˘ ÌÚÙ Ïη ËÏÙÎ ËҘˉ ˙‡ ÒÈÙ„È r ËÓ‚¯‡‰È˙˘Ï ̯‚ ˘Ó˙˘Ó‰ ÆÛ„ل‰ ¯ÂÊȇ Ï˘ ÈÂÙÈÓ· ˘ÂÓÈ˘ È„È ÏÚ ËÏÙ‰ ÍÈω˙Ï ËϘ‰ ÍÈω˙Ó

Ɖ„˜· ÏÈÁ˙Ó˘ ËÒ˜Ë Ï˘ ‰„Ϙ‰ È„È ÏÚ ¯ÂˆÚÏ ˙ÂÈÎÂ˙‰Ô¢‡¯‰ Â˙‰ ÆÔ¯ÎÊÏ Û„ل‰ ¯ÂÊȇ· Áˢ ÈÂÙÈÓ È„È ÏÚ Ô‰ÈÈ· ˙¯˘˜˙Ó ˙ÂÈÎÂ˙‰˙¯·Ú‰Ï ˘Ó˘Ó Áˢ‰ ¯‡˘Â ¨˙ÂÈÎÂ˙‰ ÔÈ· ˙ÂËÂ˘Ù ˙„˜٠˙¯·Ú‰Ï ˘Ó˘Ó ‰ÙÂÓÓ‰ ¯ÂÊȇ·˘Ó˙˘Ó‰˘ ÔÈÈˆÓ ‰„˜ ͯډ ∫‡È‰ ‰ÙÂÓÓ‰ ¯ÂÊȇ· Ô¢‡¯‰ Â˙‰ Ï˘ ˙ÂÚÓ˘Ó‰ ÆËҘˉ‡¯˜ ‡Ï ÔÈÈ„Ú˘ ‰ÙÂÓÓ‰ ¯ÂÊȇ· ˙ʯÁÓ ·˙Î ËϘ‰ ÍÈω˙˘ ÔÈÈˆÓ R ͯډ ¨¯ÂˆÚÏ ˘˜È·ËϘ‰ ÍÈω˙˘ ˙ÂʯÁÓ‰ ÏÎ ˙‡ ÒÈÙ„‰ ËÏÙ‰ ÍÈω˙˘ ÔÈÈˆÓ N ͯډ ¨ËÏÙ‰ ÍÈω˙ È„È ÏÚ¯·„· ˙Âڄ‰ ˙¯·Ú‰Ï ‰Ê‰ Ô‚Ӊ Ɖ¯ÈˆÚ ˙„˜ÙÏ Â‡ ˙ÂÙÒ ˙ÂʯÁÓÏ ÍÈ˙ÓÓ ‡Â‰Â ¨ÁÏ˘˘ÂÓÈÓ ¯˘Ù‡Ó Âȇ ÏÈÚÈ Âȇ ˙ÈÎÂ˙‰ ˙¯ÈˆÚÏ ‰·È˙Î؉‡È¯˜Ï Û˙¢Ӊ Ô¯ÎÈʉ ˙ÂÎÂÓÌÈÂΠÌÈÏÈÚÈ ÌÈÂ‚Ó „ÓÏ Í˘Ó‰· ª‰Ê‰ ÏÈ‚¯˙‰ Í¯ÂˆÏ ˜ÈÙÒÓ ‡Â‰ Ï·‡ ¨ÔÈËÂÏÁÏ ÔÂÎ

ÆÌÈÎÈω˙ ÔÈ· ˙¯Â˘˜˙ÏÁÈ‰Ï ¯˙ÂÓ Æscanf ˙¯ÊÚ· ËÂÏ˜Ï ˘È ËϘ‰ ˙‡Â printf ˙¯ÊÚ· ÒÈÙ„‰Ï ˘È ËÏÙ‰ ˙‡ÛÂÒ ˙‡ ÔÓÒÓ˘ 0≠‰ ÏÏÂÎ ÌÈÂ˙ ∏± „Ú Ï˘ ˙ʯÁÓ ¯ÓÂÏΩ ÌÈÂ˙ ∏∞–Ï Ï·‚ÂÓ ËϘ ˙¯Â˘ ͯ‡˘

Æ®˙ʯÁÓ‰WinObj ˙ÈÎÂ˙‰ ˙¯ÊÚ· ÆÌÎÏ˘ ˘Ó˙˘Ó‰ Ì˘· ÏÈÁ˙‰Ï ·ÈÈÁ ÈÂÙÈÓ‰ ̈ډ Ì˘˙Á˙ ÈÂÙÈÓ‰ ÌˆÚ ˙‡ ˙‡¯Ï ¯˘Ù‡ www.sysinternals.com ¯˙‡‰Ó „È¯Â‰Ï Ô˙È˘

ÆBaseNamedObject

˙‡ ̄˜ ÌÈÏÈÁ˙Ó ¯˘‡Î Ì‚ ˙„·ÂÚ ‡È‰ ̇‰ Ɖ˙‡ ÂÒ Æ˙ÈÎ˙‰ ˙‡ È·˙ÈÎØ·Â˙Î Æ±È˘· „·ÚÏ ‰Îȯˆ ‡È‰© ËÏÙ‰ ÍÈω˙ ˙‡ ̄˜ ÌÈÏÈÁ˙Ó ¯˘‡Î Ì‚Â ËÏÙ‰ ÍÈω˙

ø®Ìȯ˜Ó‰

È„È ÏÚ ˙Á‡ ÌÚÙ ˜¯ ‰¯·Ú‰˘ ˙ʯÁÓ ÌÈÈÓÚÙ ÒÈÙ„˙ ËÏÙ‰ ˙ÈÎÂ˙˘ ÔÎ˙È Ì‡‰ Æ≤ƘÓÏ ˘È ø˙ʯÁÓ ÌÚ ‚Ï„˙ ËÏÙ‰ ˙ÈÎÂ˙˘ ÔÎ˙È Ì‡‰ øËϘ‰ ˙ÈÎÂ˙

ËÓ‚¯‡‰ ¨„Á‡ ËÓ‚¯‡Ó ¯˙ÂÈ ˙Ï·˜Ó ‡È‰ ̇˘ ÍÎ ˙ÈÎÂ˙‰ ˙‡ ·ÈÁ¯‰Ï ˘È ˙ÚÎ Æ≥˙ÈÎÂ˙˘ ˙ÚÎ ÔÎ˙È Ì‡‰ ÆÛ„ل‰ ¯ÂÊȇ ̘ӷ ¨ÈÂÙÈÓÏ ˘Ó˘È˘ ı·Â˜ Ì˘ ‡Â‰ È˘‰¯˘‡Î ÔÎ˙È ‰Ê ̇‰ ø˘Ó˙˘Ó‰ È„È ÏÚ ÏÏÎ ‰„Ϙ‰ ‡Ï˘ ˙ʯÁÓ ÒÈÙ„˙ ËÏÙ‰

ƘÓÏ ˘È øÛ„ل‰ ¯ÂÊȇ· ÌÈ˘Ó˙˘Ó

¯ÂÊȇ· ˙˘Ó˙˘Ó ˙ÈÎÂ˙‰˘ ‡„ÂÏ ˘È Æ≥ ÛÈÚÒ Ï˘ ‰·Á¯‰‰ ¯Á‡Ï ˙ÈÎÂ˙‰ ˙‡ ˘È‚‰Ï ˘ÈÆ„Á‡ ËÓ‚¯‡ ˜¯ ‰Ï ÌȯȷÚÓ Ì‡ Û„ل‰

≤≤

Page 23: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Using Multiple Heaps

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

≤≥

Page 24: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Explicit Management of Physical Memory

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

≤¥

Page 25: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Programming with Threads

˘ÓÓÏ ÌÎÈÏÚ Æ˙ÈÓʖ· ÌÈËÂÁ ¯ÙÒÓ· ˙˘Ó˙˘Ó˘ ˙ÂÂÏÁ ˙ÈÎÂ˙ ˘ÓÓÏ „ÓÏ ‰Ê ÏÈ‚¯˙·ÌÂÈÒÏ ÔÈ˙Ó‰Ï ÈÏ· ËÏÙ ËϘ Úˆ·Ï ˙‡¯Â˜‰ ˙ÈÎÂ˙Ï ¯˘Ù‡Ï ˙ÂÎȯˆ˘ ¨˙¯‚˘ ˙ÈȯÙÒ‡Ï Â‡ ͇ ¨‰ÊÎ ˜˘ÓÓ ‰˘ÚÓÏ ˙¢ÓÓÓ ˙ÂÏÈ‚¯‰ ËÏÙØËϘ‰ ˙¯‚˘ ˙ÂÂÏÁ· Æ˙ÂÏÂÚÙ‰‡ÏÏ ËÏÙØËÏ˜Ï Ô‚Ӊ ÆÌÈÚ¯ȇ ¨ÌÈÏÂÚÓ ¨ÌÈËÂÁ· ˘ÂÓÈ˘ Ï‚¯˙Ï ˙Ó ÏÚ ¨Â· ˘Ó˙˘ÌÈËÂÁ· ˘ÂÓÈ˘‰Ó ¯˙ÂÈ ÏÈÚÈ ˙ÂÏÈ‚¯‰ ˙‡ȯ˜‰ ͯ„ ˙˜ÙÒÓ ˙ÂÂÏÁ ˙ÂίÚÓ˘ ‰˙Ó‰

ÆÏÈ‚¯˙·

∫˘„Á ËÂÁ ˙¯ÈˆÈ

typedefDWORD WINAPI (LPTHREAD_START_ROUTINE*)(void* argu-

ment);HANDLE CreateThread(

LPCTSTR security_attributes,SIZE_T stack_size,LPTHREAD_START_ROUTINE function,LPVOID function_argument,DWORD creation_flags,LPDWORD thread_id);

˙Ï·˜Ó‰ ¨funcion ‰¯‚˘‰ ˙‡ ıÈ¯Ó ˘„Á‰ ËÂÁ‰ Æ˘„Á ËÂÁ ˙¯ˆÂÈ Âʉ ˙ίÚÓ‰ ˙‡È¯˜Ï˘ ÈÚÈ·¯‰ ËÓ‚¯‡‰ ÆÈÏÈÏ˘–ȇ ÌÏ˘ ‰¯ÈÊÁÓ „ÈÁÈ ËÓ‚¯‡Î ®void*© ÒÂÙÈË ‡ÏÏ ÚÈ·ˆÓ¨HANDLE ‰¯ÈÊÁÓ ‰¯‚˘‰ Æfuncion≠Ï ËÓ‚¯‡Î ¯·ÚÂÈ˘ ÚÈ·ˆÓ‰ ‡Â‰ ˙ίÚÓ‰ ˙‡È¯˜

Æ‰Ï˘Î ˙ίÚÓ‰ ˙‡È¯˜ ̇ NULL ‡ ¨¯ˆÂ˘ ˘„Á‰ ËÂÁ‰ ˙‡ ‚ˆÈÈÓ˘ ‰‰ÊÓÏÈ‚¯˙·—˘„Á‰ ËÂÁ‰ ¯Â·Ú ˙‡˘¯‰ ÔÈÈˆÏ ¯˘Ù‡Ó ˙ίÚÓ‰ ˙‡È¯˜ Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰Í¯Ú‰Â ¨ÌÈ˙·· ˘„Á‰ ËÂÁ‰ Ï˘ ˙ÈÒÁÓ‰ τ‚ ˙‡ ÔÈÈˆÓ È˘‰ ËÓ‚¯‡‰ ÆNULL ¯È·Ú‰Ï ˘È¯˘Ù‡Ó È˘ÈÓÁ‰ ËÓ‚¯‡‰ ÆÈË„ËÒ Ï„Â‚· ˙ÈÒÁÓ ˙ˆ˜‰Ï ‰ÏÚÙ‰‰ ˙ίÚÓÏ ‰¯ÂÓ 0‰¯ÂÓ CREATE_SUSPENED Ú·˜‰ ∫˘„Á‰ ËÂÁ‰ ˙¯ÈˆÈ ÏÚ ˜ÈÈÂ„Ó ¯˙ÂÈ ÔÙ‡· ËÂÏ˘ÏÚ·˜‰Â ¨‰Ú˘ÂÓ ‡Â‰˘Î ¯ˆÂÂÈ ‡Ï‡ ¨„ÈÈÓ ıÂ¯Ï ÏÈÁ˙È ‡Ï ˘„Á‰ ËÂÁ‰˘ ‰ÏÚÙ‰‰ ˙ίÚÓÏ˙ίÚÓÏ ‰¯ÂÓ ®ÍÏȇ XP ˙ÂÂÏÁ· ˜¯© STACK_SIZE_PARAM_IS_A_RESERVATION‡Ï‡˘ „Ú ˙ÂÈÊÈÙ ˙¯‚ÒÓ ˙ˆ˜‰Ï ‡Ï ͇ ¨˙ÈÒÁÓ‰ ¯Â·Ú ˙·Â˙Î ·Á¯Ó ˙ˆ˜‰Ï ‰ÏÚÙ‰‰ÏÈÁ˙È˘ ˙Ó ÏÚ ResumeThread(HANDLE)≠Ï ‡Â¯˜Ï ˘È ¨‰Ú˘ÂÓ ËÂÁ ÌȯˆÂÈ Ì‡ Æ˙Â˘Â¯„˙ίÚÓÏ ¯˘Ù‡Ó Ô¯Á‡‰ ËÓ‚¯‡‰ ÆÛ˜Â˙· Ìȇ ÂÏω ÌÈÏ‚„‰ È˘˘ ‰¯ÂÓ 0 ͯډ ÆıÂ¯Ï¯È·Ú‰Ï Ô˙È ¨‰Ê‰ ‰‰ÊÓ· ͯˆ Ôȇ ÏÏΠͯ„· Æ˘„Á‰ ËÂÁÏ È¯ÙÒÓ ‰‰ÊÓ ¯ÈÊÁ‰Ï ‰ÏÚÙ‰‰˙‡ ˜¯ ‡Ï‡ ¨È¯ÙÒÓ‰ ‰‰ÊÓ‰ ˙‡ ¯ÈÊÁ‰Ï ‡Ï˘ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ‰¯ÂÓ˘ ¨NULL ͯډ ˙‡

ÆHANDLE≠‰

˙Âӯ‚˘ ˙ίÚÓ ˙‡ȯ˜ ˘È ˙ÂÂÏÁ ˙ÂίÚÓ· ÆÌÈÏÂÚÓ ˙ÏÈÚ ®¯·„ ÏÎÏ ËÚÓΩ ‰˙Ó‰¨˙ÂÂÏÁ· Æ„ÂÚ ¨ÚÂ¯È‡Ï ¨ÏÂÚÓ ˙ÏÈÚÏ ¨ËÂÁ ˙ÏÂÚÙ ÌÂÈÒ Ô‚Π¨‡Â‰˘ÏÎ ÚÂ¯È‡Ï ˙ÂÎÁÏ ˙ÈÎÂ˙ÏÍÈω˙ ‡ ËÂÁ ÆÔÓÂÒÓ ‡Ï ‡ ®signaled© ÔÓÂÒÓ ∫ÌÈ·ˆÓ È˘Ó „Á‡· ‡ˆÓ‰Ï ÏÂÎÈ ÌˆÚ ÏÎ˙‡ ÌÈÓÈÈÒÓ Ì‰ ¯˘‡Î ÌÈÓÂÒÓ ¨„È˙Ú· ıÂ¯Ï ÌÈÈÂ˘Ú Â‡ ÌȈ¯ ̉˘ ÔÓÊ ÏÎ ÌÈÓÂÒÓ Ìȇ®event© Ú¯ȇ ÆÈ˘ÙÂÁ ‡Â‰ ¯˘‡Î ÔÓÂÒÓ Âȇ ÏÂÚ ‡Â‰ ¯˘‡Î ÔÓÂÒÓ ®mutex© ÏÂÚÓ ÆÌ˙ÏÂÚÙÆ‰Ê ÔÂÓÈÒÏ Ë¯Ù ˙ÂÚÓ˘Ó ÂÏ Ôȇ ¨ÂÏω ÌÈ·ˆÓ‰ È˘Ó „Á‡· ‡ˆÓ‰Ï ÏÂÎÈ˘ ÌˆÚ ËÂ˘Ù ‡Â‰‡È‰ ÔÓÂÒÓ ·ˆÓÏ ÚÈ‚È ÌˆÚ˘ ÔÈ˙Ó‰Ï ˙Ó ÏÚ ‰· ˘Ó˙˘˘ ˙ίÚÓ‰ ˙‡È¯˜ÏÚÈ ÏÂÚÓ˘ ‡ ¨Â˙ÏÂÚÙ ˙‡ ÌÈÈÒÈ ËÂÁ˘ ‰È˙ÓÓ ‰‡È¯˜‰ ÆWaitForSingleObject

≤µ

Page 26: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

˙¯ÊÂÁ ‰‡È¯˜‰ ¨ÏÂÚÓ ‡Â‰ ̈ډ ̇ ÆÔÓÂÒÈ ÌÈÈÂÒÓ ®event© Ú¯ȇ˘ ‡ ¨‡¯Â˜‰ ËÂÁ‰ È„È ÏÚƯÁ‡ ËÂÁ È„È ÏÚ ÏÂÚ ÏÂÚÓ‰ ̇ ‰È˙ÓÓ ¨‡¯Â˜‰ ËÂÁ‰ È„È ÏÚ ÏÚ ‡Â‰ ¯˘‡Î ˜¯ ‰Áψ‰·

Ælock(m)≠Ï ÔÈËÂÏÁÏ ‰Ï˜˘ Âʉ ˙ίÚÓ‰ ˙‡È¯˜ ÌÈÏÂÚÓ ÏÚ ¨¯ÓÂÏÎ

DWORD WaitForSingleObject(HANDLE object,DWORD timeout_in_milliseconds);

ËÓ‚¯‡‰Â ¨Â· ¯Â˘˜‰ ÚÂ¯È‡Ï ˙ÂÎÁÏ ÌÈ˘˜·Ó˘ ̈ډ Ï˘ ‰‰ÊÓ‰ ‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰‰˙Ó‰‰ ̇ INFINITE Ú·˜‰ ‡ ¨‰È˘ ˙ÂÈÙχ· ÈÏÓÈÒ˜Ó‰ ‰˙Ó‰‰ ÔÓÊ ‡Â‰ È˘‰‰˙Ó‰‰ ̇ WAIT_OBJECT_0 ∫ÌÈÎ¯Ú ‰˘ÂÏ˘Ó „Á‡ ‰¯ÈÊÁÓ ‰¯‚˘‰ ÆÔÓÊ· ˙Ï·‚ÂÓ ‰È‡ÈÏÓÈÒ˜Ó‰ ÔÓʉ ˜¯Ù˘ ÏÏ‚· ‰ÓÈÈ˙Ò‰ ‰˙Ó‰‰ ̇ WAIT_TIMEOUT ¨‰Áψ‰· ‰ÓÈÈ˙҉̇ ¨®mutex© ÏÂÚÓ ‡Â‰ ̈ډ ¯˘‡Î ˜¯ ¯ÊÁÂÓ˘ ¨WAIT_ABANDONED ‡ ¨¯·Ú ÂÈȈ˘¯¯Á˘Ï ÈÏ· Â˙ÏÂÚÙ ˙‡ ÌÈÈÒ ÏÂÚÓ‰ ˙‡ ÏÚ˘ ̄˜‰ ËÂÁ‰˘ ÌÂ˘Ó ‰Áψ‰· ÏÚ ÏÂÚÓ‰

ÆÏÂÚÓ‰ Ï˘ ‰˘ÈË ‡¯˜ ‰ÊÎ ·ˆÓ ÆÏÂÚÓ‰ ˙‡

∫®mutex© ÏÂÚÓ ˙¯ÈˆÈ

HANDLE CreateMutex(LPSECURITY_ATTRIBUTES security_attributes,BOOL initial_ownership,LPCTSTR name);

˙‡˘¯‰Â ®È˘ÈÏ˘‰ ËÓ‚¯‡‰© ÈÏ·ÂÏ‚ Ì˘ ÂÏ ˙˙Ï Ô˙È˘ ¨˘„Á ÏÂÚÓ ˙¯ˆÂÈ ‰‡È¯˜‰È„È ÏÚ È˙ÏÁ˙‰ ÔÙ‡· ÏÂÚ˘ ÏÂÚÓ ¯ÂˆÈÏ ¯˘Ù‡Ó È˘‰ ËÓ‚¯‡‰ ƮԢ‡¯‰ ËÓ‚¯‡‰©¯¯Á˘Ï ˘È ‰‰ÊÓ‰ ˙‡ Æ˙Ï˘Î ‡È‰ ̇ NULL ‡ ‰‰ÊÓ ‰¯ÈÊÁÓ ‰‡È¯˜‰ ƯˆÂȉ ËÂÁ‰¯˘‡Î ÏÂÚÓ‰ ˙‡ ˙¯¯Á˘Ó ‰ÏÚÙ‰‰ ˙ίÚÓ ÆCloseHandle(HANDLE) ‰‡È¯˜‰ ˙¯ÊÚ·‰‡È¯˜‰ ˙¯ÊÚ· Úˆ·˙Ó ÏÂÚ ÏÂÚÓ ¯Â¯Á˘ ÆÂÈχ ÌÈÒÁÈÈ˙Ó˘ ®handles© ÌȉÊÓ Ìȯ‡˘ ‡Ï

ÆReleaseMutex(HANDLE)

∫Ì·ˆÓ ÈÂÈ˘Â ®event© Ú¯ȇ ˙¯ÈˆÈ

HANDLE CreateEvent(LPSECURITY_ATTRIBUTES security_attributes,BOOL manual_reset,BOOL initially_signaled,LPCTSTR name);

ËÓ‚¯‡‰© ˙‡˘¯‰Â ®ÈÚÈ·¯‰ ËÓ‚¯‡‰© ÈÏ·ÂÏ‚ Ì˘ ÂÏ ˙˙Ï Ô˙È˘ ¨˘„Á Ú¯ȇ ˙¯ˆÂÈ ‰‡È¯˜‰ËÓ‚¯‡‰ ÆÔÓÂÒÓ ÂÏ˘ È˙ÏÁ˙‰‰ ·ˆÓ‰˘ ÏÂÚÓ ¯ÂˆÈÏ ¯˘Ù‡Ó È˘ÈÏ˘‰ ËÓ‚¯‡‰ ƮԢ‡¯‰Â‡ ‰‰ÊÓ ‰¯ÈÊÁÓ ‰‡È¯˜‰ ÆÈËÓ¡ ÒÂÙȇ ‡ È„È ÒÂÙȇ ‚ÂÒÓ ‡Â‰ Ú¯ȇ‰ ̇‰ ÔÈÈˆÓ È˘‰ÆCloseHandle(HANDLE) ‰‡È¯˜‰ ˙¯ÊÚ· ¯¯Á˘Ï ˘È ‰‰ÊÓ‰ ˙‡ Æ˙Ï˘Î ‡È‰ ̇ NULLÆÂÈχ ÌÈÒÁÈÈ˙Ó˘ ®handles© ÌȉÊÓ Ìȯ‡˘ ‡Ï ¯˘‡Î ÏÂÚÓ‰ ˙‡ ˙¯¯Á˘Ó ‰ÏÚÙ‰‰ ˙ίÚÓ¯˘‡Î ÔÓÂÒÓ˘ Ú¯ȇ ÆÌÏÂÎ ˙‡ ¯ÈÚÓ ÂÏ ‰È˙ÓÓ˘ ÌÈËÂÁ ˙ˆÂ·˜ ˘È ¯˘‡Î ÔÓÂÒÓ˘ Ú¯ȇ

ÆÏÏÎ ÂÈ˙ÓÈ ‡Ï ‰˙Ó‰ ˙¯‚˘Ï ‡¯˜È˘ ÌÈËÂÁ ¨ÔÓÂÒÓ ¯‡˘ ÌÈÈ˙ÓÓ Ôȇ‰‡È¯˜‰ ˙¯ÊÚ· ÌÈÓÒÓ ®ÈËÓÂËÂ‡Â È„È ÒÂÙȇ© ÌÈÚ¯ȇ‰ È‚ÂÒ È˘ ˙‡‰‡È¯˜‰ ˙¯ÊÚ· ÔÓÂÒÓ ‡Ï ·ˆÓÏ ÌȯȷÚÓ È„È ÒÂÙȇ ‚ÂÒÓ Ú¯ȇ ÆSetEvent(HANDLE)˙¯ÊÚ· ÔÓÂÒÓ ‡Ï ·ˆÓÏ ÈËÓˇ ÒÂÙȇ ‚ÂÒÓ Ú¯ȇ Ì‚ ¯È·Ú‰Ï Ô˙È ÆResetEvent(HANDLE)

≤∂

Page 27: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

¯˙ÂÈ Â‡ „Á‡ ËÂÁ ¯ÈÚÓ ‡Â‰˘ Ú‚¯· ÔÓÂÒÓ ‡Ï ·ˆÓÏ ¯·ÂÚ Ì‚ ‰ÊÎ Ú¯ȇ Ï·‡ ¨ResetEventƉ˙Ó‰Ó

‡ÏÏ ËÏÙ ËϘ ˙ÂÏÂÚÙ Úˆ·Ï ˙ÈÎ˙Ï ˙¯˘Ù‡Ó‰ ˙¯‚˘ ˙ȯÙÒ ˘ÓÓ ‰Ê ÏÈ‚¯˙·È¯ÎÈÒ‡ Úˆȷ ‡¯˜ ÂÊÎ ‰¯Âˆ· ˙ÂÏÂÚÙ Úˆȷ ÆPOSIX threads≠· ˘ÂÓÈ˘ ÍÂ˙ ¨ÔÈ˙Ó‰Ï˙ÈÎ˙‰ Ï˘ ‰·È˙Π‡ ‰‡È¯˜ ˙ÏÂÚÙ˘ ÍÎ ¨ÌÈËÂÁ· ˘Ó˙˘˙ ‰È¯ÙÒ‰ Ænonblocking ‡read ˙ίÚÓ‰ ˙‡ȯ˜ ÚÂˆÈ·Ï ÔÈ˙ÓÈ˘ ‡Â‰ ËÂÁ‰ ƉȯÙÒ‰ Ï˘ ËÂÁ È„È ÏÚ ‰˘ÚÓÏ Úˆ·˙˙

Æ˙¯Á‡ ˙ÂÏÂÚÙ· ÍÈ˘Ó‰Ï ÏÎÂ˙ ˙ÈÎ˙‰˘ „ÂÚ· write ‡

∫˙¯‚˘ µ ˙· ‰È¯ÙÒ ˘ÓÓÏ ÌÎÈÏÚ Æ˘ÓÓÏ ÌÎÈÏÚ˘ ‰È¯ÙÒÏ ˜˘ÓÓ‰

int nb_init (int min_threads, int max_threads);int nb_finalize();int nb_read (HAN-DLE f, void* buf, SIZE_T count, DWORD offset);int nb_write (HAN-DLE f, void* buf, SIZE_T count, DWORD offset);int nb_wait (int request);

‰È¯ÙÒ‰ Ï˘ ÌÈÂ˙‰ È·Ó ˙‡ ˙ÏÁ˙‡Ó ‰¯‚˘‰ ƉȯÙÒ‰ ˙‡ ˙ÏÁ˙‡Ó nb_init ‰¯‚˘‰‡Â‰ Ô¢‡¯‰ ¯ËÓ¯Ù‰ ƉȯÙÒ‰ ˙ÂÁÂ˜Ï ¯Â·Ú ËÏÙØËϘ‰ ˙ÂÏÂÚÙ ˙‡ ÂÚˆ·È˘ ÌÈËÂÁ ˙¯ˆÂȉȯÙÒÏ ¯˙ÂÓ˘ ÈÏÓÈÒ˜Ó‰ ¯ÙÒÓ‰ ‡Â‰ È˘‰ ¯Ëӯى ¨ÏÂÁ˙ȇ‰ ÔÓÊ· ¯ˆÂÂÈ˘ ÌÈËÂÁ‰ ¯ÙÒÓ˙¯¯Á˘Ó ‰È¯ÙÒ‰ Ï˘ ÌÈËÂÁ‰ ÏÎ Ï˘ ¯„ÂÒÓ ÌÂÈÒÏ ˙‚‡Â„ nb_finalize ‰¯‚˘‰ ƯˆÈÏÏ˘ ‰¯˜Ó· 0 ¯ÈÊÁ‰Ï ˙ÂÎȯˆ Âχ‰ ˙¯‚˘‰ ƮԯÎÈÊ Ô‚Ω ‰È¯ÙÒ‰ È„È ÏÚ Âˆ˜Â‰˘ ÌÈ·‡˘Ó

ÆÏ˘Î Ï˘ ‰¯˜Ó· -1≠ ‰Áψ‰˙¯ÊÂÁ ˙ÂÏÂÚÙ‰ Ɖ·È˙Π‡ ‰‡È¯˜ ˙ÂÏÂÚÙ ˙ÂÏÈÁ˙Ó nb_write≠ nb_read ˙¯‚˘‰ı·Â˜ Ï˘ ‰‰ÊÓ ‰È¯ÙÒÏ ˙¯ȷÚÓ ˙¯‚˘‰ Æ͢Ӊ· Â˯ÂÙÈ˘ Ìȯ˜ÓÏ Ë¯Ù ¨‰˙Ó‰ ‡ÏÏ „ÈÈÓ¯ÙÒÓ ¨ÌÈÂ˙‰ ¯·ÚÂÈ ÂÓÓ Â‡ ÂÈχ˘ ıˆÂÁÏ ÚÈ·ˆÓ ¨‰ÏÂÚÙ‰ ˙‡ Úˆ·Ï ˘È ÂÈÏÚ˘ ÁÂ˙ÙÆÌÈÂ˙‰ ˙‡ ¯È·Ú‰Ï ˘È ÂÈχ ‡ ÂÓÓ˘ ı·Â˜‰ ˙ÏÈÁ˙Ó Ì˜ÈӉ ¨¯È·Ú‰Ï ˘È˘ ÌÈ˙·‰‰¯˜Ó· ƉÏÂÚÙ‰ ÌÂÈÒÏ ÔÈ˙Ó‰Ï ˙ÈÎ˙Ï ¯˘Ù‡Ó˘ ÈÏÈÏ˘–ȇ ‰ÏÂÚÙ ‰‰ÊÓ ˙¯ÈÊÁÓ ˙¯‚˘‰

Æ-1 ˙¯ÈÊÁÓ ˙¯‚˘‰ ‰‡È‚˘ Ï˘‰ÏÂÚÙ ‰‰ÊÓ ‡Â‰ ¯ËÓ¯Ù‰ Ɖ·È˙Π‡ ‰‡È¯˜ ˙ÏÂÚÙ ÌÂÈÒÏ ‰È˙ÓÓ nb_wait ‰¯‚˘‰

Ænb_write ‡ nb_read È„È ÏÚ ¯ÊÁ‰˘‰È¯ÙÒ‰ Ï˘ ÌÈËÂÁ‰ Ɖ˘˜·‰ È˯٠˙‡ ¯Â˙Ï ‰ÒÈÎÓ nb_write ‡ nb_read≠Ï ‰‡È¯˜˙Ó ÏÚ ˙¢˜· ≤µ≠Ï ¯Â˙‰ τ‚ ˙‡ ÏÈ·‚‰Ï ¯˙ÂÓ ÆÔ˙‡ ÌÈÚˆ·Ó ¯Â˙‰Ó ˙¢˜· ÌȇȈÂÓ¯Â˙‰˘ ‰Ï‚Ó˘ nb_write ‡ nb_read≠Ï ‰‡È¯˜ Æͯ‡˙Ó Íω˘ ¯Â˙· Ô¯ÎÈÊ Ê·ʷ ÚÂÓÏ„ÈÈÓ Â¯ÊÁÈ ‡Ï ‰Ï‡ ˙‡ȯ˜ ·˘ „ÈÁȉ ‰¯˜Ó‰ ‰Ê ƯÂ˙· ÌÂ˜Ó ‰Ù˙Ó˘ „Ú ‰È˙ÓÓ ‡ÏÓ

Æ˙ÈÎ˙ÏÆ≤µ≠Ï ¯ÂˆÈÏ ‰ÏÂÎÈ ‰È¯ÙÒ‰˘ ÌÈËÂÁ‰ ¯ÙÒÓ ˙‡ ÏÈ·‚‰Ï Ì‚ ¯˙ÂÓ

tst-win32aio.c Ì˘· ‰˜È„· ˙ÈÎ˙ ˙¯ÊÚ· ‰È¯ÙÒ‰ ˙‡ ˜Â„·Ï ÌÎÈÏÚ Æ‰˜È„·‰ ˙ÈÎ˙ÌȄ·ÈÚ ÂÈÏÚ ˙Úˆ·Ó ÔÎÓ ¯Á‡Ï Ï„‚ ı·Â˜ ˙¯ˆÂÈ ˙ÈÎÂ˙‰ ƯÙÒ‰ Ï˘ ¯˙‡‰Ó „È¯Â‰Ï Ô˙È˘Ï˘ ÌȘÂÏ··© ˜ÒÈ„Ï Â˙·È˙Πı·Â˜‰ ÏÎ Ï˘ ‰‡È¯˜ ‡Â‰ Ô¢‡¯‰ ·Ï˘‰ ÆÌÈ·Ï˘ ‰˘ÂÏ˘· ÌÈ¢Â˙·È˙Π¨Ô¯ÎÈÊ· ˜ÂÏ·· ®˙ÈÒÁÈ „·Î© ÏÂÙÈË ¨ı·Â˜· ˜ÂÏ· ÏÎ ˙‡È¯˜ ‡Â‰ È˘‰ ·Ï˘‰ Æ®µ±≤ KB

‰·È˙Ή ‰‡È¯˜‰ ˙Á˜ÂÏ˘ ÔÓʉ ˙‡ ÍÈ¯Ú‰Ï Ìȯ˘Ù‡Ó „ÁÈ ÌÈ¢¯‰ ÌÈ·Ï˘‰ È˘ Æ˘„ÁÓÌȘÂÏ·‰ ˙‡ ˙‡¯Â˜ ˙ÈÎ˙‰ È˘ÈÏ˘‰ ·Ï˘· ƘÂÏ· Ïη ÏÂÙÈˉ Á˜ÂÏ˘ ÔÓʉ ˙‡Â ı·Â˜‰Ó‰¯ÊÁ Â˙‡ ˙·˙ÂΠ¨Ô¯ÎÈÊ· ˜ÂÏ· Ïη ˙ÏÙËÓ ¨ÌÎÏ˘ ‰È¯ÙÒ· ˘ÂÓÈ˘ ÍÂ˙ ˙ȯÎÈÒ‡ ‰¯Âˆ·Ï˘ ‰‡È¯˜ ∫ÏÈ·˜Ó· ˙ÂÏÂÚÙ ˘ÂÏ˘ ˙¢Á¯˙Ó ‰Ê ·Ï˘· ‰Èˆ¯Ëȇ Ïη Æ˙ȯÎÈÒ‡ ‰¯Âˆ·

≤∑

Page 28: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Úˆ·˙Ó ˙ÈÎ˙· ·Ï˘ ÏÎ Ǣ˜‰ ˜ÂÏ·‰ Ï˘ ‰·È˙Π¨ÈÁΉ ˜ÂÏ·· ÏÂÙÈË ¨ı·Â˜· ‡·‰ ˜ÂÏ·‰Ï˘ ˙ÂÏÚ‰ ˙‡ ˙˜ÈÈÂ„Ó ‰¯Âˆ· ÍÈ¯Ú‰Ï ‰È‰È Ô˙È˘ ˙Ó ÏÚ ¨ÒÙ„ÂÓ ÂÏ˘ ‰ˆÈ¯‰ ÔÓÊ ÌÈÈÓÚÙ

Æ·Ï˘ ÏÎÔÓÊ ˙‡ ÔÓÒ Æı·Â˜· ÏÂÙÈˉ Ï˘ ‰ˆÈ¯‰ ÔÓÊ ˙‡ „ȯÂÈ È¯ÎÈÒ‡ „·ÈÚ˘ ÌÈÙˆÓ Â‡·Ï˘‰ Ï˘ ‰ˆÈ¯‰ ÔÓÊ ˙‡ ¨t1≠· ®ı·Â˜Ï ·˙ÂΠ‡¯Â˜ ˜¯˘© Ô¢‡¯‰ ·Ï˘‰ Ï˘ ÚˆÂÓÓ‰ ‰ˆÈ¯‰˘¯„‰ ‰ˆÈ¯‰ ÔÓÊ˘ ÌÈÎȯÚÓ Â‡ Æt3≠· È˘ÈÏ˘‰ ·Ï˘‰ Ï˘ ‰ˆÈ¯‰ ÔÓÊ ˙‡Â ¨t2≠· È˘‰ÌÈÈ˜Ï ÔÎÏ ·ÈÈÁ È˘ÈÏ˘‰ ·Ï˘‰ Ï˘ ‰ˆÈ¯‰ ÔÓÊ Æt2 − t1 ‡Â‰ Ô¯ÎÈÊ· ÌȘÂÏ·· ÏÂÙÈËÏ‰È‰È ‰ˆÈ¯‰ ÔÓÊ ÏÚÂÙ· ͇ ¨t3 ≈ max{t2 − t1, t1}≠˘ ÌÈÂÂ˜Ó Â‡ Æt3 ≥ max{t2 − t1, t1}

‰¯Âˆ· ËÏÙØËϘ ÚˆÈ·Ï ˙„‰ „¯È ‰ˆÈ¯‰ ÔÓÊ˘ Ú„ t3 < t2 ·˘ ‰¯˜Ó Ïη Ư˙ÂÈ Ï„‚ ·Â¯Ï·Â˘ÈÁ‰ ͇ ¨„¯ÂÈ Ô· ‰ˆÈ¯‰ ÔÓÊ˘ ‰‡¯Ó˘ ¨‰˜È„·‰ ˙ÈÎ˙ Ï˘ ÈÈÈÙ‡ ËÏÙ ‰‰ Æ˙ȯÎÈÒ‡

∫ËÏÙØËÏ˜Ï ÔÈËÂÏÁÏ ÏÈ·˜Ó· Úˆ·˙Ó ‡Ï Ô¯ÎÈÊ·

Y:\Courses\os\exercises\win32aio\Release>win32aio 100 1 4 start-ing worker 0temporary file name=<c:\temp\delete.me>writing 100 MB (200 ios of 512 KB each)Time = 6.109 secondsSyncronous Processing, no computationTime = 14.718 secondsSyncronous Processing,no computation Time = 14.546 secondsSyncronous Processing, with computation (x1024)flag = 1 (ignore)Time = 27.499 secondsSyncronous Processing, with computation (x1024)flag = 1 (ignore)Time = 27.718 secondsAsyncronous Processing, with computation (x1024)creating another worker (1)creating another worker (2)flag = 1 (ignore)Time = 15.046 secondsAsyncronous Processing, with computation (x1024)flag = 1 (ignore)Time = 14.968 secondsNB Worker <0>: stoppingNB Worker <2>: stoppingNB Worker <1>: stopping

t3 > Ï·‡ t3 < t2 ¯ÓÂÏÎ Æ®˙ÂÈ˘·© t3 ≈ 15.0≠ ¨t2 ≈ 25.5 ¨t1 ≈ 14.5 Ô‡ÎÆmax{t2 − t1, t1}

ÌÈËÂÁ‰ ¯ÙÒÓ MB≠· ı·Â˜‰ τ‚ ∫ÌÈȯÙÒÓ ÌÈËÓ‚¯‡ ‰˘ÂÏ˘ ˙Ï·˜Ó ‰˜È„·‰ ˙ÈÎ˙ÆÈÏÓÈҘӉ ÈÏÓÈÈÓ‰

˙ίÚÓ‰ ˙‡ȯ˜ Ï˘ È˘ÈÓÁ‰ ËÓ‚¯‡‰ Æ·Â˙ÎÏ Â‡ ‡Â¯˜Ï Íȯˆ ÂÓÓ˘ ı·Â˜· ̘Ӊ ÔÂȈ∫Ìȯ·È‡ ‰˘ÈÓÁ ÏÈÎÓ˘ OVERLAPPED ÒÂÙÈËÓ ‰·ÓÏ ÚÈ·ˆÓ‡Â‰ WriteFile≠ ReadFile

28

Page 29: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

typedef struct {ULONG_PTR Internal, InternalHigh;DWORD Offset, OffsetHigh;HANDLE event;

} OVERLAPPED;

˙ÂÈ·ÈÒ‰ ≥≤ ˙‡ ÌÈÈÈˆÓ Ìȇ·‰ ˙„˘‰ È˘ Æ˙ίÚÓ‰ ˘ÂÓÈ˘Ï Ì‰ ÌÈ¢‡¯‰ ˙„˘‰ È˘Ï„Â‚· ÌȈ·˜Ï ˙˘‚Ï ¯˘Ù‡˙ ÂÏ˘ ‰È¯ÙÒ‰ Æ̘ÈÓ‰ Ï˘ ˙ÂÂÈÏÚ‰ ˙ÂÈ·ÈÒ‰ ≥≤≠ ˙ÂÂ˙Á˙‰Ï˘ ‰‰ÊÓ ¯È·Ú‰Ï ¯˘Ù‡Ó Ô¯Á‡‰ ‰„˘‰ Æ∞ „ÈÓ˙ ÂÈ‰È ˙ÂÂÈÏÚ‰ ˙ÂÈ·ÈÒ‰˘ ÍÎ ¨„·Ï· ¥ GB

‡Ï ‡—ÌÈÈ˙Ò˙ ËÏÙ‰ ‡ ËϘ‰ ˙ÏÂÚÙ ¯˘‡Î ÔÓÂÒÓ ·ˆÓÏ ¯È·Ú˙ ‰ÏÚÙ‰‰ ˙ίÚÓ˘ Ú¯ȇÆ0 ͯڷ ‰Ê‰ ‰„˘‰ ˙‡ ‡ÏÓÏ ˘È ÔÎÏ ‰Ê‰ Ô‚ӷ ˘Ó˙˘

‰·ÓÏ ÚÈ·ˆÓ ˙¯·Ú‰ ÆFILE_FLAG_OVERLAPPED Ï‚„‰ ÈÏ· ı·Â˜‰ ˙‡ Á˙٠‡ڈ·˙‰Ï ËÏÙØËϘ‰ ˙ÏÂÚÙÏ ˙ӯ‚ ‰Ê‰ Ï‚„‰ ‡ÏÏ Á˙Ù˘ ı·Â˜ ÏÚ ‰ÏÂÚÙ· OVERLAPPED¯˘‡Î ˜¯ ‡¯Â˜‰ ËÂÁÏ Â¯ÊÁÈ ËÏÙØËϘ‰ ˙‡ȯ˜Â ¨Offset≠‰ ˙„˘· ÔÈȈӉ ̘ÈÓ‰Ó͇ ¨ÌÈËÂÁ· ˘ÂÓÈ˘ ‡ÏÏ ‰˙Ó‰ ‡ÏÏ ËÏÙØËϘ Úˆ·Ï ¯˘Ù‡Ó ‰Ê‰ Ï‚„‰ ÆÂÓÈÈ˙ÒÈ ˙ÂÏÂÚÙ‰

Æ‰Ê Ô‚ӷ ˘Ó˙˘ ‡Ï ‡ ¯ÂÓ‡Î

Ɖ¯„‚‰˘ ÈÙΉȯÙÒ‰˙‡˙˘ÓÓÓ‰ex-aio-win32.cÌ˘·‰È¯ÙÒ È·˙ÈÎØ·Â˙Î ÆÏÈ‚¯˙‰ÌÈËÂÁ‰ ¯ÙÒÓ ˙‡ ¯ÂˆÈÏ ˘È ‰ËÂ˘Ù‰ ˘ÂÓÈÓ‰ ˙Ó¯· Æ˙ÂÓ¯ È˙˘· ÏÈ‚¯˙‰ ˙‡ ˘ÓÓÏ Ô˙ÈÏ˘ ‰Ê ¯ÙÒÓ ¯ÂˆÈÏ ˘È ‰‡ÏÓ‰ ˘ÂÓÈÓ‰ ˙Ó¯· Æ‡Ï Â˙ nb_init≠Ï ‰‡È¯˜· ÔÈÂˆÓ˘ ÈÏÓÈÈÓ‰ÌÈÓÈȘ‰ ÌÈËÂÁ‰ Ï΢ ‰Ï‚Ó˘ nb_write ‡ nb_read≠Ï ‰‡È¯˜ ͇ ¨nb_init≠· ÌÈËÂÁ¯ÂˆÈÏ ˙·ÈÈÁ ¨nb_init≠Ï ‰‡È¯˜· ÔÈȈ˘ ÈÏÓÈÒ˜Ó‰ ¯ÙÒÓ‰Ó ÌÈËÂÁ ˙ÂÁ٠¯ˆÂ˘Â ÌȘÂÒÚ

ÆËÏÙØËϘ Úˆ·È˘ ÛÒ ËÂÁ˙‡ ÒÈÎ‰Ï ˙ÂÎȯˆ nb_write≠ nb_read ˙‡ȯ˜‰ Æ˙¢˜· ¯Â˙ ˘ÓÓÏ ˘È ‰¯˜Ó ÏηÍ¢ÓÏ ÌÈÎȯˆ ‰È¯ÙÒ‰ Ï˘ ËÏÙØËϘ‰ ÈËÂÁ Ƙȯ Âȇ˘ ͯˆ‰ ˙„ÈÓ· ˙˙‡Ï ¯Â˙Ï ‰˘˜·‰Ô˙È˘ ¨Í¯Âˆ‰ ˙„ÈÓ· ¨˙˙Â‡Ï ˘È ¯Â˙‰Ó ‰˘˜· ˙‡ˆÂÓ ¯˘‡Î ÆÔ˙‡ Úˆ·Ï ¯Â˙‰Ó ˙¢˜·

ƯÂ˙Ï ˙ÂÙÒ ˙¢˜· ÒÈΉω˙Ó‰ ÔÓÊ ˘˜·Ï ˙·ÈÈÁ ˙Â˙Ó‰‰ ÏΠƇ‰˘ ͯˆ ÌÂ˘Ï ®polling© ‰ÓÈ‚„ Úˆ·Ï ¯ÂÒ‡

ÆÈÙÂÒȇϷ˜Ï ˙Ó ÏÚ ÆÏ„‚ ı·Â˜ ÏÚ ÌÎÏ˘ ‰È¯ÙÒ· ˘ÂÓÈ˘ ÍÂ˙ ‰˜È„·‰ ˙ÈÎ˙ ˙‡ ıȯ‰Ï ˘ÈÔ·˙‰Ï ȇ„Î ÆDebug≠‰ ˙Ò¯‚ ˙‡ ‡Ï Release≠‰ ˙Ò¯‚ ˙‡ ˆȯ‰ ¨˙ÂÈ˙ÂÚÓ˘Ó ˙‡ˆÂ˙¨˙¯ډ· ¨˙˘‚ÂÓ‰ ˙ÈÎ˙Ï Û¯ˆÏ ˘È Æ˙ÈÎÂ˙‰ ˙ˆÈ¯ ÔÓÊ· ˙ÂÓÈ˘Ó‰ ωӷ ÌÈÚˆȷ‰ ÈÈÂÂÈÁ·ÌÂÓÈÒ˜Ó ÌÂÓÈÈÓ ÌÚ „Á‡ ËÂÁ Ï˘ ÌÂÓÈÒ˜Ó ÌÂÓÈÈÓ ÌÚ ˙ˆ¯‰· ˙ÈÎ˙‰ Ï˘ ËÏÙ‰ ˙‡Ï˘ ÌÂÓÈÈÓ ÌÚ ËÏÙ Ì‚ ˘È‚‰Ï ˘È ¨‰‡ÏÓ‰ ˘ÂÓÈÓ‰ ˙Ó¯ ˙‡ ˘È‚Ó˘ ÈÓÏ ÆÌÈËÂÁ ‰Ú·¯‡ Ï˘

Ɖڷ¯‡ Ï˘ ÌÂÓÈÒ˜Ó ÌÈËÂÁ ÒÙ‡

≤π

Page 30: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Reading Directories and Classifying Files

ÆÒ˜ÈÂÈ ҘÂÈÏ· ÌȈ·˜ Ï˘ ˙ÂÂÎ˙ ÔÂÁ·Ï ÌÈÎȯ„Ó ÔÎÂ˙ ‡Â¯˜Ï „ÓÏ ‰Ê ÏÈ‚¯˙·

∫Íȯ„Ó ÔÎÂ˙ ˙‡È¯˜

#include <windows.h>...HANDLE FindFirstFile(LPCTSTR pattern,

WIN32_FIND_DATA* find_data);BOOL FindNextFile (HANDLE search_handle,

WIN32_FIND_DATA* find_data);BOOL FindClose (HANDLE search_handle);

Ô¢‡¯‰ ËÓ‚¯‡‰ ÆÍȯ„Ó· ÌȈ·˜ Ï˘ ˘ÂÙÈÁ ‰ÏÈÁ˙Ó FindFirstFile ˙ίÚÓ‰ ˙‡È¯˜Ì˘ Ï˘ ˙ÈÏÈÁ˙ ˙ÏÏÂÎ ˙È·˙‰ Æ˘ÙÁÏ ÌÈ˘˜·Ó˘ ÌȈ·˜ ˙ÂÓ˘ Ï˘ ˙È·˙ ‡Â‰ ‰Ï˘Ï˘ÓÏ ÂÓÎ ¨?≠ * ÌÈÂ˙‰ ˙‡ ÏÈÎ‰Ï ‰ÏÂÎÈ˘ ¨ı·Â˜ Ì˘ Ï˘ ˙È·˙ Ï˘ ˙ÓÂÈÒ Íȯ„Ó˙ʯÁÓ‰˘ ÍÎÏ ·Ï ÌÈ˘Ï ˘È Æ\\home-fs\stoledo\* ‡ ¨C:\* ¨C:\Temp\*.doc¨Íȯ„Ó Ì˘ Ì˘· ˙ÓÈÈ˙ÒÓ ‡È‰ ̇˘ ÍÎ ¨ÌȈ·˜ ˙ÂÓ˘ Ï˘ ˙È·˙· ÌÈÈ˙Ò‰Ï „ÈÓ˙ ‰ÎȯˆÌȈ·˜‰ ÏÎ ˙‡ ‡Ï ¨ÂÓˆÚ Íȯ„Ó‰ ¨„Á‡ ı·Â˜ ˜¯Â ͇ ‡ˆÓÈ ˘ÂÙÈÁ‰ ¨\* Ô‚Π˙ÓÂÈÒ ‡ÏÏËÓ‚¯‡‰ ÆȘÂÁ Âȇ ¨C: ÂÓÎ ¨ÔÂÎ ˙‡ Ï˘ ˘¯Â˘‰ Íȯ„Ó ÏÚ ÚÈ·ˆÓ˘ ËÓ‚¯‡ ÆÍȯ„Ó·˘‰¯ÈÊÁÓ ‰¯‚˘‰ Æ˘ÂÙÈÁ· ‡ˆÓ˘ „Á‡ ı·Â˜ ˙„‡ Ú„ÈÓ ¯ÊÁÂÓ Â·˘ ‰·ÓÏ ÚÈ·ˆÓ ‡Â‰ È˘‰‰‰ÊÓ‰ ˙‡ ¯Â‚ÒÏ ˘È ¨˘ÂÙÈÁ· ‡ˆÓ˘ ÌȈ·˜‰ ¯‡˘ ˙„‡ Ú„ÈÓ Ï·˜Ï Ô˙È Â˙¯ÊÚ·˘ ‰‰ÊÓ

ÆFindClose≠Ï ‰‡È¯˜ È„È ÏÚ ‰Ê‰Æ˘ÂÙÈÁ‰ ȇ˙Ï ‰ÂÚ˘ ÛÒ ı·Â˜ ÏÚ Ú„ÈÓ ‰¯ÈÊÁÓ FindNextFile ˙ίÚÓ‰ ˙‡È¯˜ÚÈ·ˆÓ ‡Â‰ È˘‰Â ¨FindFirstFile È„È ÏÚ ¯ÊÁ‰˘ ‰‰ÊÓ‰ ‡Â‰ ‰Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰ÏÎ ˙‡ÈˆÓÏ ¨‰‡ÏÂÏ· ÏÏΠͯ„· Ìȇ¯Â˜ Âʉ ‰‡È¯˜Ï Æı·Â˜‰ ˙„‡ Ú„ÈÓ ÏÈÎÈ˘ ‰·ÓÏ˙Ï˘Î ‰‡È¯˜‰ ¨˘ÂÙÈÁ‰ ȇ˙Ï ÌÈÂÚ˘ ÌȈ·˜ ¯˙ÂÈ Ôȇ ¯˘‡Î Æ˘ÂÙÈÁÏ ÌÈÂÚ˘ ÌȈ·˜‰

ÆERROR_NO_MORE_FILES ‡Â‰ GetLastError()≠Ó ¯ÊÁÂÈ˘ ͯډÂ

‚ÂÒÓ ‰·Ó· Íȯ„Ó Â‡ ı·Â˜ ˙„‡ Ú„ÈÓ ¯ÈÊÁÓ ÌȈ·˜‰ ˘ÂÙÈÁ ÔÂ‚Ó ∫ı·Â˜ ˙„‡ Ú„ÈÓ¨WIN32_FIND_DATA

typedef struct {DWORD dwFileAttributes;FILETIME ftCreationTime;FILETIME ftLastAccessTime;FILETIME ftLastWriteTime;DWORD nFileSizeHigh;DWORD nFileSizeLow;DWORD dwReserved0;DWORD dwReserved1;TCHAR cFileName[MAX_PATH];TCHAR cAlternateFileName[14];

} WIN32_FIND_DATA;

≥∞

Page 31: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Ï˘ ˙¯Á‡ ‰ÂÎ˙ ‚ˆÈÈÓ Ì‰Ó „Á‡ Ï΢ ÌÈËÈ· ¯ÙÒÓ ÏÈÎÓ Ô¢‡¯‰ ‰„˘‰≠ ¨FILE_ATTRIBUTE_DIRECTORY ¨FILE_ATTRIBUTE_READONLY Ô‚Π¨ı·Â˜‰Æ®Í˘Ó‰· „ÂÓÚ ‰Â¯Á‡‰ ‰ÂÎ˙‰ ˙ÂÚÓ˘Ó ÏÚ© ¨FILE_ATTRIBUTE_REPARSE_POINT·˘Â ¨‰Â¯Á‡Ï ÂÈχ ¢‚ ·˘ ¨¯ˆÂ ı·Â˜‰ ·˘ ÔÓʉ ˙‡ ÌÈ‚ˆÈÈÓ Ìȇ·‰ ˙„˘‰ ˙˘ÂÏ˘Âȇ Ú„ÈÓ‰˘ ÍÎ ¨ı·Â˜ Ï˘ ÂÏω ˙„˘‰ ˙‡ ˙Â˘Ï ‰ÏÂÎÈ ˙ÈÎÂ˙ Ɖ¯Á‡Ï ÂÈχ ·˙ήßıÈȯ‚· ˙È˯„ËÒ‰ ‰Ú˘‰ ¨UTC≠·© ‡È¯˜ ‚ˆÈÈ ÂÏω ˙„˘‰Ó ıÏÁÏ Ô˙È ÆÔÈÓ‡ Á¯Î‰·ı·Â˜‰ τ‚ ˙‡ ÌÈ‚ˆÈÈÓ Ìȇ·‰ ˙„˘‰ È˘ ÆFileTimeToSystemTime ‰¯‚˘‰ ˙¯ÊÚ·Ï„‚ ˙ÂÈ‰Ï ÏÂÎÈ ı·Â˜‰ τ‚˘ ·Ï ÌÈ˘Ï ˘È Æ„Á‡ ÏÎ ˙ÂÈ·ÈÒ ≥≤ Ï˘ ˙„˘ È˘ ˙¯ÊÚ· ÌÈ˙··‰˙¯ÊÚ·˘ ‰ÁÒ‰ Æint ÂÓÎ ¨˙ÂÈ·ÈÒ ≥≤ Ï˘ ‰˙˘Ó ˙¯ÊÚ· Â˙‡ ‚ˆÈÈÏ ‰È‰È Ô˙È˘ È„ÎÓ(nFileSizeHigh * (MAXDWORD+1)) + ‡È‰ ı·Â˜ Ï„Â‚Ï ÂÏω ˙„˘‰ È˘ ˙‡ ÌȯÈÓÓÌÈ˙˘Ó· ˘ÂÓÈ˘ È„È ÏÚ Ï˘ÓÏ© ‰Ê‰ ·Â˘ÈÁ· ‰˘ÈÏ‚Ó ¯‰ÊÈ‰Ï ˘È ¨¯ÂӇΠÆnFileSizeLoẇ ¨cFileName ‰„˘· ÚÈÙÂÓ ‡ˆÓ˘ ı·Â˜‰ Ì˘ Æ®ULARGE_INTEGER ‡ double ‚ÂÒÓÚÈÙÂÈ ‡Â‰ ¨®‰È¯Á‡ ¯˙Âȉ ÏÎÏ ≥ „ÂÚ ‰„˜‰ ÈÙÏ ¯˙Âȉ ÏÎÏ ÌÈÂ˙ ∏© ¯ˆ˜ Û„¯ Ì˘ ÂÏ ˘È

ÆÔ¯Á‡‰ ‰„˘·

ÌÈÙÒ ÌÈÚÈ·ˆÓ ˙¯ÈˆÈ· ˙ÎÓÂ˙ NTFS ÌȈ·˜‰ ˙ίÚÓ ∫ÌȈ·˜Ï ÌÈÚÈ·ˆÓ ˙¯ÈÙÒ ˙¯ÈˆÈı·Â˜Ï Û„¯ Ì˘ ¯ÂˆÈÏ Ô˙È Æı·Â˜Ï ÌÈÙ„¯ ˙ÂÓ˘ ‰˘ÚÓÏ Ì‰ ÂÏω ÌÈÚÈ·ˆÓ‰ ÆÌȈ·˜ÏÚÈ·ˆÓ ÏÎ ¯È·Ú‰Ï Ô˙È ¨®ÌȈ·˜ ˙ίÚÓ ‰˙‡· ˜¯ Ï·‡© Ô΢ ı·Â˜‰ ·˘ ‰ÊÓ ¯Á‡ Íȯ„Ó·¯„Ò Ôȇ ı·Â˜ Ï˘ ÌÈÙ„¯‰ ˙ÂÓ˘‰ ÔÈ· ÆÈÂÏ˙ È˙Ï· ÔÙ‡· ¨ÂÓ˘ ˙‡ ˙Â˘Ï Â‡ ¨Ì˜ÓÏ Ì˜ÓÓ¯Ó˘È‰Ï ÍÈ˘ÓÈ ı·Â˜‰ Ï·‡ ı·Â˜‰ Ï˘ ȯ˜Ӊ ÚÈ·ˆÓ‰ ˙‡ ˜ÂÁÓÏ Ï˘ÓÏ Ô˙È ∫˙¯Èη Ï˘fsutil create‰„˜ى ˙¯ÊÚ· ‰ÊÎ ÚÈ·ˆÓ ¯ÂˆÈÏ Ô˙È ÆÛ„¯‰ Ì˘‰ ˙Á˙ ÌȈ·˜‰ ˙ίÚÓ·˙Ó ÏÚ ÌÈÙÒ ÌÈËÓ‚¯‡ ÈÏ· ‰„˜ى ˙‡ ˆȯ‰ Æ®cmd.exe© ˙ÙËÚÓ ˙ÎÂ˙· hardlink˙ίÚÓ‰ ˙‡È¯˜ ˙¯ÊÚ· ÚÈ·ˆÓ ¯ÂˆÈÏ Ì‚ Ô˙È ÆÌÈÙÒ‰ ÌÈËÓ‚¯‡‰ ˙ÂÚÓ˘Ó Ï˘ ˯ÈÙ Ï·˜Ï·Á¯Ó· ÌÈÏ‚ÚÓ Â¯ˆÂÂÈ ‡Ï˘ ˙Ó ÏÚ ¨Íȯ„ÓÏ ÚÈ·ˆÓ ¯ÂˆÈÏ Ô˙È ‡Ï ÆCreateHardLink

Æ˙ÂÓ˘‰Æ‰‡·‰ ˙ίÚÓ‰ ˙‡È¯˜ ˙ÂÚˆÓ‡· ¯¯·Ï Ô˙È ı·Â˜Ï ÌÈÚÈ·ˆÓ‰ ¯ÙÒÓ ˙‡

#include <windows.h>...BOOL GetFileInformationByHandle(HANDLE hFile,

BY_HANDLE_FILE_INFORMATION* finfo);

Â˙·Â˙΢ ‰·Ó· ı·Â˜‰ ˙„‡ Ú„ÈÓ ‰¯ÈÊÁÓ ÁÂ˙Ù ı·Â˜ Ï˘ ‰‰ÊÓ ˙Ï·˜Ó ‰‡È¯˜‰‰„˘Ï ˯٠¨WIN32_FIND_DATA≠Ï È„ÓÏ ‰Ó„ ‰Ê‰ ‰·Ó‰ ÆÈ˘‰ ËÓ‚¯‡· ˙¯·ÚÂÓÌȯ˘Ù‡Ó˘ ÌȯÁ‡ ˙„˘ ‰˘ÂÏ˘Â ¨ı·Â˜Ï ÌÈÚÈ·ˆÓ‰ ¯ÙÒÓ ˙‡ ¯‡˙Ó˘ nNumberOfLinksÌÈÚÈ·ˆÓ ͯ„ Èχ ¨ı·Â˜ Â˙Â‡Ï ÌÈÒÁÈÈ˙Ó ®HANDLE’s© ÌÈÁÂ˙Ù ÌȉÊÓ È˘ ̇‰ ˙ڄϨFindNextFile≠ FindFirstFile È„È ÏÚ ¯ÊÁÂÓ˘ Ú„ÈÓÏ „‚ȷ˘ ·Ï ÌÈ˘Ï ˘È© ÆÌȯÁ‡Æ®ı·Â˜‰ ˙‡ ÁÂ˙ÙÏ Íȯˆ ‰Ê‰ Ú„ÈÓ‰ ˙‡ Ï·˜Ï ˙Ó ÏÚ ¨ı·Â˜‰ ˙‡ ÁÂ˙ÙÏ Í¯Âˆ Ôȇ Â¯Â·Ú˘

Ï˘ ÌÈ‚ÂÒ È˘ „ÂÚ· ˙ÎÓÂ˙ NTFS ÌȈ·˜‰ ˙ίÚÓ ∫‰·ˆ‰ ˙„˜ ÌÈÈÏ·ÓÈÒ ÌÈÚÈ·ˆÓ¯˘Ù‡Ó ‰Ê‰ Ô‚Ӊ Æreparse point ‡¯˜˘ Ô‚ӷ ÌÈ˘Ó˙˘Ó ÂÏω ÌÈ‚ÂÒ‰ È˘ ÆÌÈÚÈ·ˆÓÆÌÈÈÂÒÓ Íȯ„Ó Â‡ ı·Â˜ ˙Á˙ÂÙ ˙ÈÎÂ˙˘ ÔÓÊ· ‰ÏÚÙ‰‰ ˙ίÚÓ Ï˘ ˙‚‰˙‰‰ ˙‡ ˙¢ÏÚ„ÈÓ‰ ‚ÂÒ ˙‡ ˙ÏÏÂÎ ‰Ó¢¯‰ ÆÍȯ„ÓÏ Â‡ ı·Â˜Ï Ú„ÈÓ ˙Ó¢¯ ˙ÙÒ‰ È„È ÏÚ ÏÚÂÙ Ô‚Ӊ¨ÛÒ‰ Ú„ÈÓ‰ ˙‡ ‰‰ÊÓ ‰ÏÚÙ‰‰ ˙ίÚÓ ¨Íȯ„ÓØı·Â˜‰ ˙‡ ÌÈÁ˙ÂÙ ¯˘‡Î ÆÂÓˆÚ Ú„ÈÓ‰ ˙‡ÂÔ‚Ӊ ÆÚ„ÈÓ ‚ÂÒ Â˙Â‡Ï ˙„ÁÂÈÓ ‡È‰˘ ‰ÏÚÙ‰‰ ˙ίÚÓ Ï˘ ‰¯‚˘ ‰ÏÈÚÙÓ ¨Â‚ÂÒ ˙‡ ˙¯¯·Ó¯È„ ̉· ˘ÂÓÈ˘‰˘ ÌȈ·˜ Ï˘ ‰¯·Ú‰ Ô‚Π˙ÂÏÂÎÈ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ÛÈÒÂ‰Ï ¯˘Ù‡Ó ‰Ê‰

ÆÏ˘ÓÏ ¨ÌÈÙÏ˘ ÌȘÒÈ„ ‡ ˙ÂËϘ Ï˘ ˙ÂÈË·¯ ˙ÂȯÙÒÏ

≥±

Page 32: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

‰·ˆ‰ ˙„˜ Æreparse points Ï˘ ÌÈ‚ÂÒ È˘ ̉ ‰·ˆ‰ ˙„˜ ÌÈÈÏ·ÓÈÒ ÌÈÚÈ·ˆÓ¯˘Ù‡Ó ‰Ê Æ®˜È¯Â ÌÈȘ ˙ÂÈ‰Ï Íȯˆ˘© Íȯ„Ó È·‚ ÏÚ ‰ÓÏ˘ ÌȈ·˜ ˙ίÚÓ ·Èˆ‰Ï ˙¯˘Ù‡Ó˙‡ ͯ„ ‡Ï ¨˙¯Á‡ ÌȈ·˜ ˙ίÚÓÓÓ ˜ÏÁ ‰˙Èȉ ‡È‰ ÂÏȇΠÌȈ·˜‰ ˙ίÚÓ· ˘Ó˙˘‰Ï

ÆÔ˙‡ ¯È҉Ϡ‰·ˆ‰ ˙„˜ ¯ÂˆÈÏ ˙¯˘Ù‡Ó mountvol ˙ÈÎÂ˙‰ ÆÔÂÎ

reparse point≠‰˘ ‰Ó„ Íȯ„Ó ‡Â‰ ®junction© ˙Óˆ ˙ÂÂÏÁ· ‡¯˜˘ ¨ÈÏ·ÓÈÒ ÚÈ·ˆÓ„‚ȷ ÆÍȯ„ÓÏ Û„¯ Ì˘ ‰˘ÚÓÏ Â‰Ê Æ¯Á‡ Íȯ„Ó ÁÂ˙ÙÏ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ˙ӯ‚ ÂÏ˘ÆÂÈχ ÈÏ·ÓÈÒ ÚÈ·ˆÓ ÔÈ·Â ÂÓˆÚ Íȯ„Ó‰ ÔÈ· ˙¯Èη ÒÁÈ ˘È ¨Ì„˜ ¯‡È˙˘ ÌÈÚÈ·ˆÓÏËÂ˘Ù ÈÏ·ÓÈÒ‰ ÚÈ·ˆÓ‰ ‰ÊÎ ‰¯˜Ó· ÆÈÏ·ÓÈÒ ÚÈ·ˆÓ ÂÈχ ˘È˘ Íȯ„Ó ˜ÂÁÓÏ Ô˙È ¨Ï˘ÓϘÂÁÓÏ ¯ÂˆÈÏ ¯˘Ù‡Ó˘ ÈÏÎ ‰ÏÚÙ‰‰ ˙ίÚÓ ÌÚ „ÁÈ· ˙˜ÙÒÓ ‡Ï ËÙÂÒ¯˜ÈÓ Æ„Â·ÚÏ ˜ÈÒÙÈÔ˙È˘ junction ˙ÈÎÂ˙‰ ˙¯ÊÚ· ‰Ï‡Î ÌÈÚÈ·ˆÓ Ï‰Ï Ô˙È Ï·‡ ¨ÌÈÈÏ·ÓÈÒ ÌÈÚÈ·ˆÓresource≠‰Ó ˜ÏÁ ‡È‰˘ linkd ˙ÈÎÂ˙‰ ˙¯ÊÚ·Â www.sysinternals.com≠Ó ÌÈÁ „ȯ‰Ï˙ίÚÓ ¨‰˘ÚÓÏ Æ®˙ÂÂÏÁ ÌÚ „ÁÈ ˙ˆÙÂÓ ‰È‡˘ ËÙÂÒ¯˜ÈÓ Ï˘ ‰ÎÂ˙ ˙ÏÈ·Á© ˙ÂÂÏÁ Ï˘ kitÏ˘ ‚ÂÒ Â˙‡ ˜ÂÈ„· Âȉ ÂÏȇΠÌÈÈÏ·ÓÈÒ ÌÈÚÈ·ˆÓÏ ‰·ˆ‰ ˙Â„Â˜Ï ˙ÒÁÈÈ˙Ó ‰ÏÚÙ‰‰

Æreparse point

Ú„ÈÓ‰ ˙‡ ‡Â¯˜Ï ˙Ó ÏÚ Íȯ„ÓÏ ÍÈÈ˘˘ reparse point≠‰ ‚ÂÒ ˙‡ ‡ÂˆÓÏ ˙Ó ÏÚÌÈÚÈ·ˆÓ ‰·ˆ‰ ˙„˜ ‡ ÈÏ·ÓÈÒ ÚÈ·ˆÓ˘ ÌȈ·˜‰ ˙ίÚÓ Â‡ Íȯ„Ó‰ Ì˘ ÏÏÂΩ · ¯ÂÓ˘˘

Ɖ‡·‰ ˙ίÚÓ‰ ˙‡È¯˜· ˘Ó˙˘‰Ï Íȯˆ ¨®Ì‰Èχ

#define _WIN32_WINNT 0x0500 /* Windows 2000 and up */#include <windows.h>#include <winioctl.h>#define FSCTL_GET_REPARSE_POINT 0x000900a8typedef struct {

DWORD ReparseTag;WORD ReparseDataLength;WORD Reserved;union {

struct {WORD SubstituteNameOffset;WORD SubstituteNameLength;WORD PrintNameOffset;WORD PrintNameLength;

WCHAR PathBuffer[1]; /* can be larger! */} MountPointReparseBuffer;struct {

BYTE DataBuffer[1];} GenericReparseBuffer;

};} MY_REPARSE_DATA_BUFFER...

32

Page 33: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

BOOL DeviceIoControl(HANDLE file,DWORD command,void* input_buffer,DWORD input_buffer_size,void* output_buffer,DWORD output_buffer_size,DWORD* bytes_returned,OVERLAPPED* overlapped);

‰„˜ى ˙‡Â ÁÂ˙Ù ı·Â˜ Ï˘ ‰‰ÊÓ ¯È·Ú‰Ï Íȯˆ Âʉ ˙ίÚÓ‰ ˙‡È¯˜Ïreparse ˙¯ÈˆÈ Ï˘ÓÏ ÂÓÎ ¨ÌÈ˘ÂÓÈ˘ ‰·¯‰ Âʉ ‰‡È¯˜Ï ÆFSCTL_GET_REPARSE_POINTÈÚÈ·¯‰Â NULL ˙ÂÈ‰Ï ÏÂÎÈ È˘ÈÏ˘‰ ËÓ‚¯‡‰˘ ÍÎ ¨ÛÒ ËϘ· ͯˆ Ôȇ ¨ÂÏ˘ ˘ÂÓÈ˘· Æpointreparse point≠·˘ Ú„ÈÓ‰ ·˙ÎÈ Â·˘ Ô¯ÎÈÊ Áˢ ¯È·Ú‰Ï ˘È Ìȇ·‰ ÌÈËÓ‚¯‡‰ È˘· Æ0ƘÈÙÒÓ Ï„Â‚· Ô¯ÎÊ Áˢ ¯È·Ú‰Ï ˘È ÔÎÏ ¨ÌÈ˙· ±∂≥∏¥≠Ï ÚÈ‚‰Ï ÈÂ˘Ú Ú„ÈÓ‰ ͯ‡ ÆÂί‡ ˙‡ÂReparseTag‰„˘‰ ʇ ¨‰·ˆ‰ ˙„˜ ‡ ÈÏ·ÓÈÒ ÚÈ·ˆÓ ‚ÂÒÓ ‡Â‰ reparse point≠‰ ̇˙„˘‰ ‰ÊÎ ‰¯˜Ó· ÆIO_REPARSE_TAG_MOUNT_POINT ͯډ ˙‡ ÏÈÎÈ ¯ÊÁÂÈ˘ ‰·Ó·Ï˘ ͯ‡‰Â ̘ÈÓ‰ ˙‡ ÂÏÈÎÈ SubsituteNameLength≠ SubstituteNameOffset‰Ó Ï˘ Ì˘‰ Æ®PathBuffer ‰„˘‰ ˙ÏÈÁ˙Ó ÌÈ˙·· ËÒȉΠ̘ÈÓ© ‰Ó„‰ Íȯ„Ó· ·ˆÂÓ˘ ‰Ó˙ÂÁÙÏ Â‡ Unicode ˙ÈÎÂ˙ ‡È‰ ˙ÈÎÂ˙‰˘ ‡„ÂÂÏ ˘È ÔÎÏ ¨Unicode≠· „ÈÓ˙ „„Â˜Ó ·ˆÂÓ˘

ÆÌȇ˙Ó ÔÙ‡· Âʉ ˙ʯÁÓÏ ÌÈÒÁÈÈ˙Ó˘˙‡È¯˜Ï ÂÏ˘ ‰‰ÊÓ· ˙‡ ¯È·Ú‰Ï ‰ÂÂÎ ÍÂ˙Ó Íȯ„Ó Â‡ ı·Â˜ ÌÈÁ˙ÂÙ ¯˘‡ÎÌÈÏ‚„‰ ÌÚ ı·Â˜‰ ˙‡ ÁÂ˙ÙÏ ˘È ¨reparse point ‡Â¯˜Ï ˙Ó ÏÚ Âʉ ˙ίÚӉ̯‚ ‰Ê FILE_FLAG_BACKUP_SEMANTICS≠ FILE_FLAG_OPEN_REPARSE_POINT‰Ó Ï˘ ‰ÁÈ˙ÙÏ ‡Ï ¨ÂÈχ „ÓˆÂÓ reparse point≠‰˘ ‰Ó„‰ Íȯ„Ó Â‡ ı·Â˜ Ï˘ ‰ÁÈ˙ÙÏ

ÆÂÈÏÚ ÌÈÚÈ·ˆÓ˘MY_REPARSE_DATA_BUFFER Ï˘Â FSCTL_GET_REPARSE_POINT Ï˘ ˙¯„‚‰‰

ÆÔ˙‡ ‰¯È„‚Ó „ÈÓ˙ ‡Ï Win32 Ï˘ ÁÂ˙ÈÙ‰ ˙·È·Ò˘ ÌÂ˘Ó ˙ˆÂÁ

ÆÏÈ‚¯˙‰

Æ˙ÏÚÂÙ ‡È‰˘ ‡„ ÌȯÂËÈϘ˙‰ ÔÂÎ ¯Â·Ú ‰·ˆ‰ ˙„˜ ¯ˆ Ʊ

®hard link© c ÚÈ·ˆÓ Íȯ„Ó‰ Ï˘ ‰¯Â‰· ¯ˆ ˙ÚÎ Æb≠ a ¨ÌȈ·˜ È˘ · f Íȯ„Ó Â¯ˆ Æ≤øc≠Ó ÔÈÓÊ ÔÈÈ„Ú ÂÎÂ˙ ̇‰ Æf≠Ó b ˙‡ ˜ÁÓ b≠Ï

¯ÂˆÈÏ ÌÈÏÂÎÈ Ì˙‡ ̇‰ Æf≠· gj ÈÏ·ÓÈÒ ÚÈ·ˆÓ ÂÈχ ¯ˆÂ f Ï˘ ‰¯Â‰· g Íȯ„Ó Â¯ˆ Æ≥ÌÈÎȯ„Ó· ÌȈ·˜· ÌÈ˘ÙÁÓ ¯˘‡Î ‰¯Â˜ ‰Ó øg ÍÂ˙· f≠Ï fj ÚÈ·ˆÓ ˙¯ÈˆÈ È„È ÏÚ Ï‚ÚÓ˙‡ ÌȘÁÂÓ Ì‡ ‰¯Â˜ ‰Ó ø®‰ÏÚÙ‰‰ ˙ίÚÓ Ï˘ ÌȈ·˜‰ ˘ÂÙÈÁ ˙ÎÂ˙ ͯ„© ÂÏω

øg Íȯ„Ó‰

‰„˜ÙÏ ‰Ó„· ¨Íȯ„Ó ÔÎÂ˙ ÏÚ ˙Á„Ӣ ex-win32dir Ì˘· ˙ÈÎÂ˙ ·˙Î ˙ÚΠƥ¨ı·Â˜Ï ‰Â¯Á‡‰ ‰·È˙Ή ÔÓÊ ˙‡ ÒÈÙ„‰Ï ‰Îȯˆ ˙ÈÎÂ˙‰ ¨Íȯ„Ó· ı·Â˜ ÏÎ ¯Â·Ú Ædir¨Âτ‚ ˙‡ ¨ı·Â˜‰ χ ÌÈÚÈ·ˆÓ‰ ¯ÙÒÓ ˙‡ ¨‡Ï ‡ Íȯ„Ó ‡Â‰ ı·Â˜‰ ̇‰ ÔÈÈˆÓ˘ Â˙¨ÈÏ·ÓÈÒ ÚÈ·ˆÓ ‡ ‰·ˆ‰ ˙„˜ ‡Â‰ ̇ ¨Í¯‡‰ ÂÓ˘ ˙‡ ¨®˘È ̇© ¯ˆ˜‰ ÂÓ˘ ˙‡˘È Ɖ˘Â¯„‰ ˙ÈÎÂ˙‰ ˙ÏÂÚÙ ˙‡ ÌÈ‚„Ó Í˘Ó‰· ËÏÙ‰ ÆÂÈÏÚ ÚÈ·ˆÓ ‡Â‰˘ ‰Ó ˙‡ÏÚ ÌÎÏ˘ ˙ÈÎÂ˙‰ Ï˘ ËÏÙ ¨˙Âӄ˜‰ ˙Âχ˘‰ ÏÚ ˙·¢˙Ï ˙ÈÎÂ˙Ï ÛÒ· ¨˘È‚‰ÏÚÈ·ˆÓ ÌÚ Íȯ„Ó ¨‰·ˆ‰ ˙„˜ ÌÚ Íȯ„Ó ¨„Á‡ ÚÈ·ˆÓÓ ¯˙ÂÈ ÌÚ ı·Â˜ · ˘È˘ Íȯ„Ó

Æ®Íȯ„Ó Â˙‡ ‰È‰È ‰Ê˘ ¯˙ÂÓ© ÈÏ·ÓÈÒ

33

Page 34: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

C:\os\exercises\ex-win32dir\Debug> ex-win32dir .22/04/2004 09:00 D (1) 0 .22/04/2004 09:00 D (1) 0 ..21/04/2004 12:09 - (1) 17 THI-SIS~1 This is a long name21/04/2004 12:09 - (2) 17 b21/04/2004 12:09 D (1) 0 gj -> ..\g...

34

Page 35: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

A Tiny Web Browser

Æ®HTTP ˙¯˘Ó ¨˜ÂÈ„ ¯˙ÈÏ© ˯Ëȇ ˙¯˘Ó ı·Â˜ ‰„ȯÂÓ˘ ˙ÈÎ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓÌÈلل ÔÈ· ¯˘˜Ï ˙Ó ÏÚ „ÁÂÈÓ· ÔÎÂ˙˘ ÌȈ·˜ ˙¯·Ú‰Ï ϘÂ˯٠‡Â‰ HTTP ϘÂ˯ÙÌÈÂ˙ ˙¯·Ú‰Ï ϘÂ˯٠¨TCP ‚ÂÒÓ ‰¯·Ú‰ ϘÂ˯ٷ ˘Ó˙˘Ó ϘÂ˯ى Æ˯Ëȇ È˙¯˘Ï

ÆÔÈӇ ¯Â„Ò ¯˘˜·

¨˙¯Â˘˜˙‰ ˙·È·Ò ˙‡ ¯Â‚ÒÏ ÏÁ˙‡Ï Íȯˆ ˙ÂÂÏÁ· ∫˙¯Â˘˜˙‰ ˙·È·Ò ÏÂÁ˙ȇ

#include <winsock2.h>...int WSAStartup(WORD version, WSAData* data);int WSACleanup();int WSAGetLastError();

‰¯ÈÊÁÓ ˙È˘ÈÏ˘‰Â ¨‰˙‡ ˙¯‚ÂÒ ‰ÈÈ˘‰ ¨˙¯Â˘˜˙‰ ˙·È·Ò ˙‡ ˙ÏÁ˙‡Ó ‰Â˘‡¯‰ ‰¯‚˘‰·˙¯‡˙Ó˘ ˙ʯÁÓ Ï·˜Ï ˙Ó ÏÚ FormatMessage≠Ï ¯È·Ú‰Ï Ô˙È˘ ¨Ô¯Á‡‰ ‰‡È‚˘‰ „˜ ˙‡Ï˘ ‰Ò¯‚‰˘ ‡„ÂÂÏ ˙ÈÎÂ˙Ï ¯˘Ù‡Ó ÏÂÁ˙ȇ‰ ˙¯‚˘ Ï˘ Ô¢‡¯‰ ËÓ‚¯‡‰ Ɖ‡È‚˘‰ ˙‡˘È ÁÂ˙ÈÙ‰ ˙·È·Ò· ¨ÔÎ ÂÓÎ ÆMAKEWORD(2,2) ͯڷ ¢Ó˙˘‰ ª˙ÈÎ„Ú ˙¯Â˘˜˙‰ ˙¯‚˘project properties > ˙Á˙ ÏÏΠͯ„·© wsock32.lib ‰È¯ÙÒ· ˙˘Ó˙˘Ó ˙ÈÎÂ˙‰˘ ‡„ÂÂÏ

Ʈlinker > input > additional dependencies

∫˙¯Â˘˜˙ Ú˜˘ ˙¯È‚Ò ˙¯ÈˆÈ

#include <winsock2.h>...SOCKET socket(int domain, int type, int protocol);int closesocket(SOCKET s);

˙ÂÂÏÁ·© -1 ‡ ®ÌȈ·˜ ¯Â·Ú open ÂÓΩ ȯÙÒÓ ‰‰ÊÓ ‰¯ÈÊÁÓ ژ˘ ˙¯ˆÂÈ socket ‰‡È¯˜‰ßß˙¯Â˘˜˙‰ ÌÏÂÚßß ˙‡ ÔÈÈˆÓ Ô¢‡¯‰ ¯ËÓ¯Ù‰ ÆÏ˘Î Ï˘ ‰¯˜Ó· ®INVALID_SOCKET Ú·˜‰ÈÏ·ÓÈÒ‰ Ú·˜‰ ˙ÂÈ‰Ï Íȯˆ ÂÎ¯Ú Ë¯Ëȇ‰ ÈϘÂ˯ٷ ˙¯Â˘˜˙Ï ÆÚ˜˘‰ ÏÚÙÈ Â·˘ÔÈӇ ¯Â„Ò ¯˘˜ ˙¯ÈˆÈÏ Æ˜ÙÒÈ Ú˜˘‰˘ ˙Â¯È˘‰ ‚ÂÒ ˙‡ ÔÈÈˆÓ È˘‰ ËÓ‚¯‡‰ ÆAF_INET˙‡ ÔÈÈˆÓ È˘ÈÏ˘‰ ËÓ‚¯‡‰ ÆSOCK_STREAM ͯډ ˙‡ ¯È·Ú‰Ï ˘È ®TCP ¯˘˜ Ï˘ÓÏ©˙¯ÊÚ· Ìȯ‚ÂÒ Ú˜˘‰ ˙‡ ÆIPPROTO_TCP ˙ÂÈ‰Ï Íȯˆ ÂÎ¯Ú TCP ¯Â·Ú ÈÙȈÈÙÒ‰ ϘÂ˯ى

Æclosesocket ‡ close ˙ίÚÓ‰ ˙‡È¯˜

∫˙¯˘Ï ÌÈȘ Ú˜˘ ¯Â·ÈÁ

#include <winsock2.h>...int connect(SOCKET sockfd,

struct sockaddr* server_name,int server_name_len);

‡Â‰ Ô¢‡¯‰ ËÓ‚¯‡‰ Æ-1 ͯډ ¯ÊÁÂÓ Ï˘Î Ï˘ ‰¯˜Ó· Æ˙¯˘Ï Ú˜˘‰ ˙‡ ˙¯·ÁÓ ‰‡È¯˜‰˙¯˘‰ ˙‡ ÌÈÈÈˆÓ È˘ÈÏ˘‰Â È˘‰ ÌÈËÓ‚¯‡‰ Æsocket≠Ï ‰‡È¯˜· ¯ÊÁ‰˘ Ú˜˘‰ ¯ÙÒÓ

≥µ

Page 36: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ËÓ‚¯‡‰ Æ͢Ӊ· ¯·ÒÂÓ ‰Ê ‰·Ó ÏÂÁ˙ȇ Æ®port© ¯Ú˘‰ ¯ÙÒÓ ˙‡Â ¯·Á˙‰Ï ÌȈ¯ ÂÈχ˘ÆÈ˘‰ ËÓ‚¯‡‰ ͯ‡ ˙‡ ÔÈÈˆÓ È˘ÈÏ˘‰

read≠· ˘ÂÓÈ˘ È„È ÏÚ ÂÈχ ÂÓÓ ·Â˙ÎÏ ‡Â¯˜Ï Ô˙È ˙¯˘Ï ¯·ÂÁÓ Ú˜˘‰˘ Ú‚¯Ó˙Ó ÏÚ Æ®ÁÂ˙Ù ı·Â˜ Ï˘ ‰‰ÊÓÎ ˘Ó˘Ó Ú˜˘‰ ¯ÙÒÓ© ¯ÂȈ ‡ ı·Â˜Ó ÂÓÎ ˘ÓÓ write≠Â

ÆÁÂ˙Ù ı·Â˜Ï ÂÓÎ ˘ÓÓ ¨close≠Ï ‡Â¯˜Ï ˘È ¯˘˜‰ ˙‡ ¯Â‚ÒÏ

ÏÂÎÈ˘ ÈÙ¯ÂÓÈÏÂÙ ‰·Ó ‡Â‰ struct sockaddr ‚ÂÒÓ ‰·Ó‰ Æ˯Ëȇ ˙·Â˙Î ˙ÈÈ·˙·Â˙Î ‚ˆÈÈÓ˘ ‰·Ó· ˘Ó˙˘‰Ï ˘È ÂÏ˘ ‰¯˜Ó· Æ˙¢ ÌÈϘÂ˯٠˙ÂÁÙ˘Ó· ˙·Â˙Î ‚ˆÈÏcasting Úˆ·Ï ˘È© struct sockaddr_in ‡Â‰ ‰Ê‰ ÈÙȈÈÙÒ‰ ‰·Ó‰ Ì˘ Æ˯Ëȇ˙‡ ÔÈȈӉ ¨sin_family ®±© ∫˙„˘ ‰˘ÂÏ˘ ‰Ê ‰·Ó· Æ®connect≠Ï ‰‡È¯˜‰ ÔÓÊ· ÚÈ·ˆÓϨsin_port ®≤© ¨AF_INET Ú·˜‰ ˙‡ ÏÈÎ‰Ï ÂÈÏÚ Ë¯Ëȇ ˙·Â˙Î Ï˘ ‰¯˜Ó·Â ˙·Â˙Ή ‚ÂÒ¨network order≠· Ìȯ„ÂÒÓ ÌÈ˙·‰ ¯˘‡Î ¯Ú˘‰ ¯ÙÒÓ ˙‡ ÏÈÎ‰Ï Íȯˆ˘ ¨short ÒÂÙÈËÓ‡ gethostbyname ‰‡È¯˜‰Ó ˙¯ÊÁÂÓ ‡È‰˘ ÈÙÎ ¨˙¯˘‰ ˙·Â˙Î ˙‡ ÏÈÎÓ˘ sin_addr ®≥©¯Â„ÈÒÓ short ÒÂÙÈËÓ ‰˙˘Ó ‰¯ÈÓÓ htons ‰¯‚˘‰ Æ͢Ӊ· ¯‡Â˙È˘ gethostbyaddr

Æ®netinet/in.h≠· ˙¯„‚ÂÓ© ˙˘¯‰ Ï˘ ȇ˜‰ ¯Â„ÈÒÏ ·˘ÁÓ‰ Ï˘ ÌÈ˙·˙¯ÊÚ· ·˘ÁÏ ˘È ®È˘ÈÏ˘‰ ËÓ‚¯‡‰© connect≠Ï ‰‡È¯˜· ‰·Ó‰ ͯ‡ ËÓ‚¯‡ ˙‡

Æstruct sockaddr_in Ï˘ sizeof

#include <winsock2.h>...struct hostent* gethostbyname(const char *name);struct hostent* gethost-byaddr(const char *addr, int len, int type);

Ô˙È˘ ‰˙˘ÓÏ ÂÏ˘ IP≠‰ ˙·Â˙Î ˙‡ ‡ ˙¯˘ Ì˘ ‰ÏÈÎÓ‰ ˙ʯÁÓ ˙ÂÎÙ‰ ÂÏω ˙¯‚˘‰ È˙˘È˙˘ Æstruct sockaddr_in ‚ÂÒÓ ‰·Ó· sin_addr ‰„˘Ï ®memcpy ˙¯ÊÚ·© ˜È˙Ú‰Ï˙ÈÎÂ˙Ï ¯È·Ú‰ ˘Ó˙˘Ó‰ ̇‰ ˙ÂÏ‚Ï ˙ÂÒÏ Ì˜ӷ Æ˙ÂÏ˘Î Ô‰ ̇ NULL ˙¯ÈÊÁÓ ˙¯‚˘‰Æ‰È˘Ï ‡Â¯˜Ï ˙Ï˘Î ‡È‰ ̇ ‰Â˘‡¯‰ ‰¯‚˘Ï ‡Â¯˜Ï ËÂ˘Ù Ï·Â˜Ó ˙·Â˙Π‡ ˙¯˘ Ì˘È˘‰ ËÓ‚¯‡‰ Æ˙¯˘ ˙·Â˙Π‡ Ì˘ ‰ÏÈÎÓ‰ ˙ʯÁÓ‰ ‡Â‰ ˙¯‚˘‰ È˙˘· Ô¢‡¯‰ ËÓ‚¯‡‰ÆAF_INET ˙ÂÈ‰Ï Íȯˆ È˘ÈÏ˘‰ ËÓ‚¯‡‰Â ˙ʯÁÓ‰ ͯ‡ ËÂ˘Ù ‡Â‰ gethostbyaddr≠ϯÙÒÓ ˘È ˙¯˘Ï ̇ ˙·Â˙Î ¯ÙÒÓ Â‡© ˙·Â˙Î ÏÈÎÓ˘ ‰·Ó Ï˘ ˙·Â˙Î ˙¯ÈÊÁÓ ˙¯‚˘‰˜È˙Ú‰Ï ˘È˘ ÌÈ˙·‰ ¯ÙÒÓ ‡Â‰ ͯ‡‰ Æh_length ‰„˘· ÔÈÈÂˆÓ ˙·Â˙Î ÏΠͯ‡ Æ®˙·Â˙Ή„˘· ˙¯ÂÓ˘ ÔÓˆÚ ˙·Â˙Ή Æstruct sockaddr_in ‚ÂÒÓ ‰·Ó· sin_addr ¯·ÁÏ˙·Â˙η ˘Ó˙˘‰Ï ËÂ˘Ù Ô˙È Æh_addr_list ÂÓ˘˘ ˙·Â˙ÎÏ ÌÈÚÈ·ˆÓ Ï˘ ͯÚÓ ‡Â‰˘

ÆͯÚÓ· ‰Â˘‡¯‰

ÏÏΠͯ„· ¨˙¯˘Ï TCP ¯˘˜· ¯·Á˙Ó Á˜ω ¨˙¯˘Ó ˙Â¯È˘ Ï·˜Ï ˙Ó ÏÚ ÆHTTP ϘÂ˯Ù˙ÓÒÓ‰ ‰˜È¯ ‰¯Â˘ ԉȯÁ‡Ï ˙Â¯Â˘ ¯ÙÒÓ ‰ÏÈÎÓ ‰˘˜·‰ Ɖ˘˜· ÁÏ¢ ¨∏∞ ¯ÙÒÓ ¯Ú˘Ï‰Â˘‡¯‰ ‰¯Â˘‰ Æ˙Â¯Â˘ ˘ÂÏ˘ ÏÏÂΉ ËÂ˘Ù ‰˘˜· ‰·Ó· ˘Ó˙˘ ‡ Ɖ˘˜·‰ ÛÂÒ ˙‡ı·Â˜‰ ‰‰ÊÓ ÚÈÙÂÈ ¨‰„˜ى ȯÁ‡ ¨‰¯Â˘ ‰˙‡· ÆGET ‰„˜ٷ ˘Ó˙˘ ‡ Ɖ„˜٠‰ÏÈÎÓ‰Ò¯‚· ˘Ó˙˘ ÂÁ‡ ÆÌÈÁ¯· ÌÈ„¯ÙÂÓ ¨Ï˜Â˯ى ‰‰ÊÓ ÂȯÁ‡Ï ¨®URL≠‰© „È¯Â‰Ï ÌȈ¯˘‚˙‰ ˙‡ ÏÈÎ˙ ‰ÈÈ˘‰ ‰¯Â˘‰ ÆHTTP/1.0 ‡Â‰ ÂÏ˘ ‰‰ÊÓ‰˘ HTTP ϘÂËÂ¯Ù Ï˘ ‰Â˘‡¯‰˙ÂÈ‰Ï ·ÈÈÁ ‡Ï ‰Ê Æ˘˜Â·Ó‰ ı·Â˜‰ ˙‡ ˜ÙÒÏ Ï‚ÂÒÓ˘ ˙¯˘ Ì˘ ®Á¯· „¯ÙÂÓ© ÂȯÁ‡Ï Host:˙¯˘‰ ‡Â‰ URL≠· ÔÈȈӉ ˙¯˘‰ ÈΠ̇ ¨URL≠· ÔÈÂˆÓ˘ ˙¯˘‰ ‡ ÂÈχ ¯·Á˙‰˘ ˙¯˘‰ÌÈÈ˙Ò‰Ï ‰Îȯˆ ‰¯Â˘ ÏΠƉ˜È¯ ‰¯Â˘ ‰È‰˙ ˙È˘ÈÏ˘‰ ‰¯Â˘‰ Æ‰Ê ‰„˘Ï ¯˙ÂÈ· ÈÂÈ‚‰‰

∫‡Ó‚Â„Ï ‰˘˜· ‰‰ Æ®„·Ï· \n ‡Ï© \r\n ÌÈÂ˙·

36

Page 37: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

GET http://www.math.tau.ac.il/~sivan/ HTTP/1.0\r\nHost: www.tau.ac.il\r\n\r\n

ı·Â˜‰ ‰È¯Á‡Ï ¨‰˜È¯ ‰¯Â˘ ԉȯÁ‡Ï ı·Â˜‰Â ˙¯˘‰ È·‚Ï Ú„ÈÓ ˙Â¯Â˘ ¯ÙÒÓ· ‰ÂÚ ˙¯˘‰‡È‰˘ ÔÂÂÈÎÓ ¯˙ÂÈ· ‰·Â˘Á‰ ‡È‰ ‰Â˘‡¯‰ Ú„ÈÓ‰ ˙¯Â˘ Ʈτ‚ ı·Â˜ Ï˘ ‰¯˜Ó· Â˙ÏÈÁ˙ ‡©Ì‚ ÁÏ˘ÈÈ˘ ı·Â˜‰ τ‚ ˙‡ ‰ÏÈÎÓ‰ ‰¯Â˘‰ Æ˙Â¯È˘‰ ÔÂÏ˘Î Â‡ ˙Áψ‰Ï ‰Èˆ˜È„ȇ ‰ÏÈÎÓ∫‚ˆ‰˘ ‰˘˜·Ï ‰·Â‚˙Î ÂÏ·˜˙‰˘ ÈÙÎ Ú„ÈÓ‰ ˙Â¯Â˘Ï ˙ÈÈÈÙ‡ ‡Ó‚„ ÔÏ‰Ï Æ‰·Â˘Á ‡È‰

HTTP/1.0 200 OKDate: Tue, 25 Apr 2000 09:50:57 GMTServer: Apache/1.3.3 (Unix)Last-Modified: Wed, 22 Mar 2000 13:44:01 GMTETag: "cf999-1007-38d8ce21"Accept-Ranges: bytesContent-Length: 4103Content-Type: text/html

Æwww.w3.org ¯˙‡· ‡ÂˆÓÏ Ô˙È HTTP ϘÂËÂ¯Ù Ï˘ ‡ÏÓ‰ ÔÂÈÙ‡‰ ˙‡

ÒÈÙ„‰Ï HTTP ˙¯˘Ó ı·Â˜ Ï·˜Ï ‰„ȘÙ˙˘ ex-browse Ì˘· ˙ÈÎ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰˙·Â˙ÎØÌ˘ ∫ÌÈËÓ‚¯‡ ‰Ú·¯‡ ˘È ˙ÈÎ˙Ï ÆËÏÙÏ ˙¯˘‰ ÁÏ¢˘ Ú„ÈÓ‰ ˙Â¯Â˘ ˙‡Â Â˙‡ÆÂÓˆÚ ı·Â˜‰ Ï˘ URL≠‰Â ¨ı·Â˜‰ ˙‡ ˜ÙÒÈ˘ ˙¯˘‰ Ì˘ ¨¯Ú˘ ¯ÙÒÓ ¨¯·Á˙‰Ï ˘È ÂÈχ˘ ˙¯˘

‰„˜ى Ï˘ ‰‡ˆÂ˙ ‡È‰ ‰Ó‚„· ˙¯·Á˙‰‰ ¨Ï˘ÓÏ

% ex-browse www.tau.ac.il 80 \www.math.tau.ac.il \http://www.math.tau.ac.il/~sivan/

˙ÂËÈÒ¯·È‡ ÏÏÂΩ ÌÈ·¯ ˯Ëȇ ȘÙÒ ∫®proxy servers© ÌÈ˘¯eÓ ÌÈ˙¯˘ È·‚Ï ¯ˆ˜ ¯·Ò‰TCP Ȉ¯ڷ ˙Âȯ·Á˙‰ ÌÈÓÒÂÁ ®Ô‚¯‡‰ ÍÂ˙· ÌÈ·˘ÁÓÏ Ë¯Ëȇ È˙Â¯È˘ ˙˜ÙÒÓ˘ ˙¯·ÁÂ„È¯Â‰Ï ˙Ó ÏÚ ÆHTTP≠‰ ˙¯Â·Ú˙ ˙‡ ÏÚÈÏ ˙Ó ÏÚ ˙‡Ê ¨ÌÈȈÈÁ ÌÈ˙¯˘ Ï˘ ∏∞ ¯ÙÒÓ ¯Ú˘Ï®proxy© ‰˘¯eÓ ˙¯˘Ó ÌȈ·˜‰ ˙‡ ˘˜·Ï ˘È ¨‰ÊΠ˯Ëȇ ˜ÙÒÏ ¯·ÂÁÓ ·˘ÁÓ‰ ¯˘‡Î ÌȈ·˜˘˜·Ó ÁÈÏ˘‰ ¨ÏÏΠͯ„· Æ∏∞ ¯Ú˘Ï ÌÈȈÈÁ Ìȯ·ÈÁ ¯ÂˆÈÏ ˙ÏÂÎÈ ˘È ÂÏ ˜¯˘Â ˜ÙÒÏ ÍÈÈ˘˘ÁÂ˜Ï˘ ÔÂÈÎÓ ı·Â˜‰ Ï˘ ÈÎ„Ú ˜˙ÂÚ ÁÈÏ˘Ï ˘È ̇ Ìχ ÆȈÈÁ‰ ˙¯˘‰Ó ı·Â˜‰ ˙‡ ¯·ژÙÒÏ ÍÂÒÁÈ Íη ¨Âψ‡ ¯ÂÓ˘˘ ˜˙ÂÚ‰ ˙‡ ÂÏ ¯ÈÊÁÈ ‡Â‰ ¨‰Â¯Á‡Ï Â˙‡ „ȯ‰ ‰˘ÏίÓÂÏ ÂÏÎÂÈ ‰¯·Á· ‡ ‰ËÈÒ¯·È‡· ‰ÎÈÓ˙‰ È˘‡ ‡ ˯Ëȇ‰ ˜ÙÒ ÆIP ˙¯Â·Ú˙ ˯Ëȇ‰

ƉÊÎ ˙¯˘· ˘ÂÓÈ˘ ‰˘Ú ̇ ¨‰˘¯ÂÓ‰ ˙¯˘‰ Ì˘ ˙‡ ÌÎÏ∫Ï˘ÓÏ ¨¯ÈÚʉ Ôلل‰ ÌÚ ÌÈÈÂÒÈÓ Ë¯Ëȇ È˙¯˘ ˙‚‰˙‰ ÏÚ ˙·¯ „ÂÓÏÏ Ô˙È

ÂÓÎ ¯˙‡ Ï˘ ˙È·‰ Û„ ˙‡ „È¯Â‰Ï ÈÒØ‰Ò ¨‰˘¯ÂÓ ˙¯˘· ˘Ó˙˘Ó ÌÎÏ˘ ˜ÙÒ‰ ̇ Ʊ‰Êȇ· Ɖ˘¯ÂÓ‰ ˙¯˘‰ ͯ„ Ì‚Â www.cnn.com≠Ï ‰¯È˘È ‰˘˜·· www.cnn.com

ø‰¯˜ ˜ÂÈ„· ‰Ó ¨Ï˘Î· Ì˙Ϙ˙ ̇ øÛ„‰ ˙‡ „È¯Â‰Ï ˙Áψ‰ ÌÈί„‰Ó

Æosbook ‰ÏÈÓ‰ ȯÁ‡ ¢/¢ ÈÏ· Ì‚Â ÌÚ ¨Ï˘ÓÏ ¨¯ÙÒ‰ Ï˘ ˯Ëȇ‰ Û„ ˙‡ „È¯Â‰Ï ÂÒ Æ≤Íȇ ø‰‡È‚˘‰ ˙ڄ‰ ‰˙ȉ ‰Ó Ɖ‡È‚˘ ˙ڄ‰ ‰‡¯‰ ÏÎÎ ÂÏ·˜˙ Ìȯ˜Ó‰ „Á‡·

ø®ÂÒ© ÂÊÎ ‰‡È‚˘ ˙ڄ‰ Ï·˜Ó ‡Â‰ ¯˘‡Î ÈÚˆ˜Ó Ôلل ‚‰˙Ó

≥∑

Page 38: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Serving Dynamic Content

˙¯˘‰ ¨¯ÓÂÏÎ ÆÈÓÈ„ ÔÎÂ˙ ˜ÙÒÏ Ï‚ÂÒÓ ‰È‰È˘ ËÂ˘Ù HTTP ˙¯˘ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ‰¯ÊÁ ÁÏ¢ Ôلل‰ ˙˘˜· ÈÙÏ ˙ÂÈÎÂ˙ ıÈ¯Ó ‡Â‰ ‡Ï‡ ¨ÌȈ·˜ Ï˘ ÔÎÂ˙ ÔÙ„Ù„Ï ˜ÙÒÓ Âȇ˙ίÚÓ‰ ˙‡ȯ˜ ˙ÈÁ·Ó ÈΠ̇ ¨˙ÂÂÏÁ ˙ÂίÚÓÏ „ÚÂÈÓ ˙¯˘‰ ÆÔ‰Ï˘ ËÏÙ‰ ˙‡ ÔللÏÏÈ‚¯˙‰ Ɖ¯Âˆ ‰˙‡· ˜ÂÈ„· ˙¢ÓÂÓÓ Ò˜ÈÂÈØÒ˜ÂÈÏ· ˙¯˘ ˙ÂÎÂ˙ ¨˙¯Â˘˜˙Ï ˙Â¯Â˘˜‰

Æ®shell© ˙ÙËÚÓ Ï˘ ˙‚‰˙‰Â ÌȈ·˜ ˙¯˘ Ï˘ ˙‚‰˙‰ ‰˘ÚÓÏ ·Ï˘Ó

‰È‡ ¨Ôلل Ï˘ÓÏ ¨Á˜Ï ˙¯˘ ÔÈ· TCP ¯˘˜ ˙¯ÈˆÈ ÆÌÈÈÂÒÓ ¯Ú˘Ï ˙¯·Á˙‰ ˙¢˜·Ï ‰Ê‡‰connect≠Ï ‰‡È¯˜ È„È ÏÚ ˙¯˘Ï ¯·Á˙Ó Á˜ω˘ Âȇ¯ ̄˜‰ ÏÈ‚¯˙· Æ˙ȯËÓÈÒ ‰ÏÂÚÙÔÈÎ‰Ï Íȯˆ ˙¯˘‰ ¨˙È˘‡¯ Æ˙¯·Á˙‰ ˙¢˜·Ï ÔÈ˙ÓÓ ¨˙‡Ê ˙ÓÂÚÏ ¨˙¯˘‰ ÆÚ˜˘‰ ˙¯ÈˆÈ ¯Á‡Ï

∫Ú˜˘‰ ˙‡

ÆÂ˙¯ÈˆÈ ȯÁ‡ setsockopt≠Ï ‰‡È¯˜ È„È ÏÚ Ú˜˘‰ ˙‚‰˙‰ ˙‡ ˙Â˘Ï ÏÂÎÈ ˙¯˘‰ ƱÔȇ ˙ÂÁ˜ÏÏ ˙ÂÓȇ˙Ó Ú˜˘ ˙‚‰˙‰ Ï˘ Ï„ÁÓ‰ ˙¯ȯ· ͇ ¨˙‡Ê ˙¢ÚÏ ‰·ÂÁ Ôȇ¯ÊÂÁ ˘ÂÓÈ˘ ˙¢¯‰Ï ®±© ∫̉ ˙Â˘Ï Èˆ¯˘ ÌȯËÓ¯Ù È˘ ÆÌÈ˙¯˘Ï ˜ÂÈ„· ˙ÂÓȇ˙ÓÏÈ‚¯˙· ƉÁψ‰· ÂÁÏ˘ ÌÈÂ˙‰ Ï΢ „Ú ÈÁ ¯˘˜‰ ˙‡ ¯È‡˘‰Ï ®≤© ¨¯Ú˘ ȯÙÒÓ· È„ÈÈÓ

ÆÚ˜˘‰ ˙‚‰˙‰ ˙‡ ˙Â˘Ï Í¯Âˆ Ôȇ ‰Ê

Æbind≠Ï ‰‡È¯˜ È„È ÏÚ ˙ÂÁ˜ω ¯·Á˙È ÂÈχ˘ ÌÈÂÒÓ Ì˘Ï Ú˜˘‰ ˙‡ ¯˘Â˜ ˙¯˘‰ Æ≤˙ÂÏÂÎÈ ˙¯˘Ï© ˙¯·Á˙‰ ˙¢˜· Ï·˜Ï ÌȈ¯ ‰Î¯„˘ IP≠‰ ˙·Â˙Î ÏÏΠͯ„· ‡Â‰ Ì˘‰˙¯˘‰ ÏÚ ÌÎÒÂÓ˘ ¯Ú˘ ¯ÙÒÓ ®˙˘¯Ï Ìȯ·ÈÁ ¯ÙÒÓ ÂÏ ˘È ̇ ˙·Â˙Î ¯ÙÒÓ ˙ÂȉÏÁÂ˜Ï Ì‚© ÆÏ„ÁÓ ˙¯È¯·Î ∏∞ ¯ÙÒÓ ¯Ú˘· ÌÈ˘Ó˙˘Ó HTTP È˙¯˘ Æ„Á‡Î ˙ÂÁÂ˜Ï‰Â¯Â·Ú ÌÈÂÒÓ ¯Ú˘ ¯ÙÒÓ ‰ˆÂ¯ ‡Â‰ ̇ connect≠Ï ‰‡È¯˜‰ ÈÙÏ bind≠Ï ‡Â¯˜Ï ÏÂÎÈ

Æ®ÌÈÂÒÓ ¯Ú˘ ¯ÙÒÓÏ ÁÂ˜Ï Ï˘ Ú˜˘ ˙‡ ¯Â˘˜Ï ‰·ÈÒ Ôȇ ÏÏΠͯ„· ͇ ¨¯˘˜‰

‰‡È¯˜ È„È ÏÚ ¯·Á˙‰Ï ÌÈ˘˜·Ó˘ ˙ÂÁÂ˜Ï Ï˘ ¯Â˙ ˙¯ÈˆÈ· ˆ¯ ÏÚ ÚÈ„ÂÓ ˙¯˘‰ Æ≥¯˙ÂÈ Ì‡ ÆÈÏÓÈÒ˜Ó‰ ¯Â˙‰ ͯ‡ ˙ÂÈ‰Ï Íȯˆ ‰Ó ÚÈ„ÂÓ ˙¯˘‰ ‰‡È¯˜· Ælisten≠ÏÏÂÏÚ ˙¯˘‰ ̉·˘ ÌÈ·ˆÓÏ È˘ÂÓÈ˘ ͯ‡ ¯Â˙ ƉÁ„È˙ Ì˙˘˜· ¨¯·Á˙‰Ï ÌÈÒÓ ˙ÂÁ˜Ï

Æ˙¢˜·‰ ˙Ï·˜ ·ˆ˜· ÌÈˆÂ¯Ú ÁÂ˙ÙÏ Ï‚ÂÒÓ Âȇ˘ È„Î „Ú ÒÂÓÚ ˙ÂȉÏ

‰ÒÈÎÓ ÂÊ ‰‡È¯˜ Æaccept≠Ï ‰‡È¯˜ È„È ÏÚ ˙Úˆ·˙Ó ‰ÓˆÚ ‰˙Ó‰‰ ¨Ú˜˘‰ ˙Ή ¯Á‡Ï‰ÒÓ ÁÂ˜Ï˘ „Ú ‰˙Ó‰Ï ®accept≠Ï ‡¯˜˘ ÍÈω˙‰ ‡ ËÂÁ‰ ˙‡ ˙ÂÁÙÏ Â‡© ˙¯˘‰ ˙‡‰Ê ‰‰ÊÓ Æ˘„Á Ú˜˘ ‰‰ÊÓ ‰¯ÈÊÁÓ ˙¯ÊÂÁ ‰‡È¯˜‰ ¨¯·Á˙‰Ï ‰ÒÓ ÁÂ˜Ï ¯˘‡Î Ư·Á˙‰ÏÌÂÈÒ· Æwrite≠ read ˙¯ÊÚ· ÂÓÓ ·Â˙ÎÏ ‡Â¯˜Ï Ô˙È ˙¯Â˘˜˙Ï ÁÂ˙Ù‰ ¯˘˜‰ ˙‡ ‚ˆÈÈÓaccept Âڈȷ ÂÈÏÚ˘ ȯ˜Ӊ Ú˜˘‰ Æclose ˙¯ÊÚ· Â˙‡ ¯Â‚ÒÏ ˘È ¨‰Ê‰ ¯˘˜· ˘ÂÓÈ˘‰˙ÂÁ˜ÏÏ ¯˘Ù‡Ï ˙Ó ÏÚ accept≠Ï ·Â˘ Â˙ȇ ‡Â¯˜Ï Ô˙È Á˜ω ÌÚ ˙¯Â˘˜˙Ï ˘Ó˘Ó Âȇ‰‡ÏÂÏ· accept ÚÂˆÈ·Ï „Á‡ ÍÈω˙ØËÂÁ· ÌÈ˘Ó˙˘Ó ÌÈ˙¯˘ ¨ÏÏΠͯ„· Ư·Á˙‰Ï ÌÈÙÒÂ

ÆÁ˜ÏÏ Á˙Ù˘ ¯˘˜ Ïη ÏÂÙÈËÏ „¯Ù ÍÈω˙ØËÂÁ·Â ¨˙ÈÙÂÒȇ

#include <winsock2.h>...int bind (SOCKET socket, struct sock-addr* my_addr, int addr_len);int listen(SOCKET socket, int queue_size);int accept(SOCKET socket, struct sock-addr* client_addr, int* addr_len);

≥∏

Page 39: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ÂÈχ˘ ¯Ú˘‰ ¯ÙÒÓ ÏÏÂÎ ˙¯˘‰ ˙·Â˙Î ˙‡ ÌÈÈÈˆÓ bind≠Ï È˘ÈÏ˘‰Â È˘‰ ÌÈËÓ‚¯‡‰Ì˘ ˙‡ Æconnect≠Ï ˙·Â˙Î ÌÈ·˘ ÂÓÎ ˜ÂÈ„· ÂÓÎ Ì˙‡ ÌÈ· ÆÚ˜˘‰ ˙‡ ¯Â˘˜Ï ÌȈ¯

Ægethostname≠Ï ‰‡È¯˜ È„È ÏÚ Ï·˜Ï Ô˙È ˙¯˘‰ ı¯ ÂÈÏÚ˘ ·˘ÁÓ‰ÈÙÏ ¯Â˙· ÔÈ˙Ó‰Ï ˙ÂÏÂÎÈ˘ ˙¯·Á˙‰‰ ˙¢˜· ¯ÙÒÓ ‡Â‰ listen≠Ï È˘‰ ËÓ‚¯‡‰

ÆÂÁ„È ˙¢˜·˘ÆÁ˜ω ˙·Â˙Î ˙¯ÊÁ‰Ï ‰¯‚˘‰ ˙‡ ÌÈ˘Ó˘Ó accept≠Ï È˘ÈÏ˘‰Â È˘‰ ÌÈËÓ‚¯‡‰Æ¯˘˜‰ ˙¯ÈˆÈ ¯Á‡Ï getpeername ‰¯‚˘Ï ‰‡È¯˜ È„È ÏÚ Á˜ω ˙·Â˙Î ˙‡ Ï·˜Ï Ì‚ Ô˙È

Æinet_ntoa≠· ˘ÂÓÈ˘ È„È ÏÚ ˙ʯÁÓÏ ˙·Â˙Ή ˙‡ ÍÂÙ‰Ï Ô˙È

„ÈÈÓ ·Â˘ accept≠Ï ‡Â¯˜Ï ˙¯˘· Ȉ¯ ÏÏΠͯ„· ¨¯·Î ¯·Ò‰˘ ÈÙÎ ÆÁ˜ÏÏ ¯˘˜· ÏÂÙÈËÏÂÙÈˉ ˙‡ ¯È‡˘‰Ï ¨ÁÂ˙Ù ¯˘˜Ï ¯·ÂÁÓ˘ Ú˜˘ Ï˘ ‰‰ÊÓ ‰¯ÈÊÁÓ ˙ӄ˜ ‰‡È¯˜˘ ¯Á‡ÏÚÂÓÏ ˙Ó ÏÚ ˙‡Ê ‰¯Âˆ· ÌÈ˙¯˘ ÌÈ· ‡ ÆÌȯÁ‡ ÌÈËÂÁ ‡ ÌÈÎÈω˙Ï ÌÈÁÂ˙Ù‰ Ìȯ˘˜·

Æ·¯ ÔÓÊ Á˜ÂÏ Ì„Â˜ Á˜Ϸ ÏÂÙÈˉ˘ ÌÂ˘Ó ˙¯·Á˙‰Ï ·¯ ÔÓÊ ÔÈ˙ÓÓ ÁÂ˜Ï Â·˘ ·ˆÓ˙¯ˆÂÈ accept≠˘ ÌÚÙ ÏÎ Æ˙ÂÁ˜Ϸ ÏÂÙÈËÏ ‰ËÂ˘Ù ‰È‚˯ËÒ‡· ‰Ê ÏÈ‚¯˙· ˘Ó˙˘ ‡ƯˆÂ˘ ¯˘˜· ÏÙËÈ˘ ˘„Á ÍÈω˙ ¯ÂˆÈÏ ˙Ó ÏÚ CreateProcess≠Ï ‡¯˜È ˙¯˘‰ ¨˘„Á ¯˘˜·Â˘ ‡Â¯˜Ï ®Â· ˘Ó˙˘È ‡Ï ‡Â‰© ¯ˆÂ˘ ˘„Á‰ Ú˜˘‰ ˙‡ ¯Â‚ÒÏ ËÂ˘Ù Íȯˆ ȯ˜Ӊ ÍÈω˙‰˙˘˜·· ÏÙËÏ ¨accept ڈ· ÂÈÏÚ˘ Ú˜˘‰ ˙‡ ¯Â‚ÒÏ Íȯˆ ¯ˆÂ˘ ˘„Á‰ ÍÈω˙‰ Æaccept≠Ï

Æ˙‡ˆÏ ¯˘˜‰ ˙‡ ¯Â‚ÒÏ Ê‡Â ¨ÁÂ˜Ï‰Ó ˙Â¯È˘‰˙˘˜· Ïη ¨‰¯˜È ‰ÏÂÚÙ ¨˘„Á ÍÈω˙ ˙¯ˆÂÈ ‡È‰˘ ÔÂÈÎÓ „ÁÂÈÓ· ‰ÏÈÚÈ ‰È‡ ÂÊ ‰È‚˯ËÒ‡ÏÂÙÈË ¯Á‡Ï ÌÈ˙Ó Ìȇ˘ ÌÈËÂÁ ‡ ÌÈÎÈω˙ Ï˘ ¯‚‡Ó· ˘Ó˙˘‰Ï ‰È‰ ÛÈ„Ú Æ˙¯·Á˙‰

Ɖ˙Ó‰ ‡ÏÏ ËÏÙØËϘ‰ ÏÈ‚¯˙ ÂÈ˘Ú˘ ÈÙÎ ¨„Á‡ Á˜Ϸ‡Ï ˙ÂÈ‰Ï ‰ÏÂÏÚ ÂÊ ‰È‚˯҇ Ì‚ ˙ÈÓʖ· ˙ÂÁÂ˜Ï Ï˘ Ï„‚ ¯ÙÒÓ· ÏÙËÏ Í¯Âˆ ˘È ¯˘‡ÎÈ„È ÏÚ ÂÊ ‰ÈÚ· ÌÚ „„ÂÓ˙‰Ï Ô˙È ÆÌÈËÂÁ ‡ ÌÈÎÈω˙ Ï˘ ÛÂÎ˙ ‚Â˙ÈÓ Ï˘ ¯ÈÁÓ‰ ˙‡ÙÓ ‰ÏÈÚÈ· ÌÈˆÂ¯Ú Ï˘ Ï„‚ ¯ÙÒÓ· ÏÙËÏ „„· ÍÈω˙Ï ˙¯˘Ù‡Ó˘ select ˙ίÚÓ‰ ˙‡È¯˜· ˘ÂÓÈ˘

ÆÂʉ ‰˘È‚‰ ˙‡ ‚ÈˆÓ ‡·‰ ˜¯Ù· ÏÈ‚¯˙‰ Æ˙ÈÓÊÍÈω˙‰ Æ˙Â¯È˘ ˙˘˜· ÏÎ ¯Â·Ú ˘„Á ÍÈω˙ ˙¯ÈˆÈ Ï˘ ‰È‚˯ËÒ‡· ˘Ó˙˘ ‡ ¨¯ÂÓ‡Î˙‡ ÂÓÓ Ï·˜Ï ˙Ó ÏÚ ¨Á˜ÏÏ ÁÂ˙Ù‰ Ú˜˘· ˘Ó˙˘‰Ï Ï‚ÂÒÓ ˙ÂÈ‰Ï Íȯˆ ¯ˆÂ˘ ˘„Á‰Ï˘ ‰‰ÊÓ‰ ˙˘¯Â‰ È„È ÏÚ ÂÏ ˜ÈÚ‰Ï Íȯˆ Âʉ ˙ÏÂÎȉ ˙‡ Ɖ·Â˘˙‰ ˙‡ ÂÏ ÁÂÏ˘Ï ‰˘˜·‰¨‰‰ÊÓ‰ ˙‡ ÏÙÎ˘Ï ‡È‰ ‰‰ÊÓ‰ ˙‡ ÂÏ ˘È¯Â‰Ï ¯˙ÂÈ· ‰ËÂ˘Ù‰ ͯ„‰ Æ˘„Á‰ ÍÈω˙Ï Ú˜˘‰˘„Á‰ ÍÈω˙‰ ˙¯ÈˆÈ ÔÓÊ· ˘˜·Ï ¨‰˘Â¯È ¯· ‰È‰È ¨Ú˜˘ Â˙Â‡Ï ÒÁÈÈ˙Ó˘ ¨˘„Á‰ ‰‰ÊÓ‰˘ ÍÎ

Æ®‰˘Â¯È ȯ·Î ˘¯ÂÙÓ ÔÙ‡· ¯ˆÂ˘ ÌȉÊÓ‰ ˙‡ ˜¯© ÌȉÊÓ ˘¯ÈÈ ‡Â‰˘

#include <windows.h>...BOOL DuplicateHandle(

HANDLE source_process,HANDLE source_handle,HANDLE target_process,HANDLE* target_handle,DWORD desired_access,BOOL inheritable_target,DWORD options);

ƯÁ‡ ÍÈω˙Ï „Á‡ ÍÈω˙Ó Â˙‡ ¯È·Ú‰Ï Ì‚ ‰ÏÂÎÈ ¨‰‰ÊÓ ˙ÏÙÎ˘Ó Âʉ ˙ίÚÓ‰ ˙‡È¯˜Ô¢‡¯‰ ËÓ‚¯‡· ¯È·Ú˘ ÍÎ ¨ÈÁΉ ÍÈω˙‰ ÍÂ˙· ‰‰ÊÓ ÏÙÎ˘Ï ˙Ó ÏÚ ‰· ˘Ó˙˘ ‡˙‡ ÌȯȷÚÓ È˘‰ ËÓ‚¯‡· ÆGetCurrentProcess()≠Ó ¯ÊÂÁ‰ ͯډ ˙‡ È˘ÈÏ˘‰Â‡ ÆÏÙ΢Ӊ ‰‰ÊÓ‰ ˙‡ Ï·˜È˘ ‰˙˘Ó‰ ˙·Â˙Î ˙‡ ÈÚÈ·¯·Â ¨ÏÙÎ˘Ï ÌȈ¯˘ ‰‰ÊÓ‰

≥π

Page 40: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

ÆÈ˘È˘‰ ËÓ‚¯‡· ÆTRUE ͯډ ˙‡ ¯È·Ú ÔÎÏ ¨‰˘¯Â‰ ¯· ‰È‰ÈÈ ˘„Á‰ ‰‰ÊÓ‰˘ ÌȈ¯ƘÙÒÈ ÏÙ΢Ӊ ‰‰ÊÓ‰˘ ‰˘È‚‰ ˙‡˘¯‰· ËÂÏ˘Ï Ìȯ˘Ù‡Ó ÈÚÈ·˘‰Â È˘ÈÓÁ‰ ÌÈËÓ‚¯‡‰Í¯Ú‰ ˙‡ Ô¯Á‡‰ ËÓ‚¯‡· ¯È·Ú ¨ÏÙ΢Ӊ ‰‰ÊÓ· ÂÓÎ ˙‡˘¯‰ Ô˙‡ ˙‡ ÌȈ¯ ‡˘ ÔÂÈÎÓ

Æ®0 ¯ȷډ© È˘ÈÓÁ‰ ËÓ‚¯‡‰Ó ÌÏÚ˙˙ ‰‡È¯˜‰ ʇ ¨DUPLICATE_SAME_ACCESSͯډ ˙‡ ¯È·Ú‰Ï Íȯˆ ¨˘¯È˘ ÏÙ΢Ӊ ‰‰ÊÓ· ˘Ó˙˘‰Ï ÏÎÂÈ ˘„Á‰ ÍÈω˙‰˘ È„ÎÍÈω˙‰˘ ‰ÏÚÙ‰‰ ˙ίÚÓÏ ÚÈ„ÂÓ˘ ¨CreateProcess() Ï˘ È˘ÈÓÁ‰ ËÓ‚¯‡· ÆTRUEÍȯˆ ˘„Á‰ ÍÈω˙‰ ¨ÔÎ ÂÓÎ ÆÈÁΉ ÍÈω˙‰ Ï˘ ‰˘¯Â‰‰ ȯ· ÌȉÊÓ‰ ˙‡ ˙˘¯Ï Íȯˆ ˘„Á‰

Ɖ„˜ى ˙¯Â˘· ‰‰ÊÓ‰ ¯ÙÒÓ ˙‡ ÂÏ ¯È·Ú ‰Ê‰ ÏÈ‚¯˙· ƯÙÒÓ ‰Ó ¨‰‰ÊÓ‰ Â‰Ó ˙Ú„Ï

˙‡ ÂÏ ÁÂÏ˘Ï Á˜ω ¯Â·Ú ˙ÈÎ˙ ıȯ‰Ï ˙¯˘‰ ÏÚ ÆËÏÙ‰ ÛÂÒȇ ÁÂ˜Ï ¯Â·Ú ˙ÈÎ˙ ˙ˆ¯‰Content- Ú„ÈÓ‰ ˙¯Â˘·© ËÏÙ‰ ͯ‡ ˙‡ ËÏÙ‰ ÈÙÏ ÁÂÏ˘Ï ˙¯˘‰ ÏÚ˘ ÔÂÈÎÓ Æ‰Ï˘ ËÏÙ‰Â˙‡ ÁÂÏ˘Ï ÔÎÓ ¯Á‡Ï ˜¯Â Âί‡ ˙‡ ¯ÂÙÒÏ ¨ıˆÂÁ· ËÏÙ‰ ˙‡ ÛÂÒ‡Ï ˙¯˘‰ ÏÚ ¨®Length

Æ˙ÂÓȇ˙Ó Ú„ÈÓ ˙Â¯Â˘ ÌÚ Á˜ÏÏ˙ίÚÓ‰ ˙‡È¯˜ ˙¯ÊÚ· ¯ˆÂÈ ˙¯˘‰˘ Ì˘ ¯ÒÁ ¯ÂȈ ˙¯ÊÚ· Úˆ·˙Ó ËÏÙ‰ ÛÂÒȇ

ÆCreatePipe

#include <windows.h>...BOOL CreatePipe(HANDLE* read_pipe,

HANDLE* write_pipe,SECURITY_ATTRIBUTES* sa,DWORD size);

‡Â‰ Ô¯Á‡‰ ËÓ‚¯‡‰ ÆÈ˘‰Ó ‡Â¯˜ÏÂ Ì‰Ó „Á‡Ï ·Â˙ÎÏ Ô˙È˘ ÌȉÊÓ È˘ ‰¯ÈÊÁÓ ‰‡È¯˜‰ÏÈ·‚Ó Âȇ ‡Â‰ Ï·‡ ¨¯ÂȈ‰ ¯Â·Ú ˙ˆ˜‰Ï Íȯˆ˘ ıˆÂÁ‰ Ï„Â‚Ï Ú‚Â· ˙ίÚÓÏ ‰Úˆ‰ ¯„‚·ÏÙÎ˘Ï Íȯˆ ÂÈχ ·Â˙ÎÏ Ô˙È˘ ‰‰ÊÓ‰ ˙‡ ƯÂȈ· ¯È·Ú‰Ï Ô˙È˘ Ú„ÈÓ‰ ˙ÂÓÎ ˙‡ ÔÙ‡ Ì¢·ÈË„ËÒ‰ ËÏÙ‰ ıÂ¯Ú ¯Â˙· · ˘Ó˙˘˙ ‡È‰˘ È„Î ¨ıȯ˘ ˙ÈÎÂ˙Ï Â˙‡ ˘È¯Â‰Ï ‰È‰È Ô˙È˘ ÍÎ˙ÈÎ˙‰ ˙‡ ıÈ¯È˘ ÍÈω˙ ¯ÂˆÈÏ ˙Ó ÏÚ CreateProcess≠Ï ‡¯Â˜ ˙¯˘‰ ÔÎÓ ¯Á‡Ï Æ‰Ï˘ÍÈω˙‰˘ È„Î Æ®˙ÂÈÎ˙ ÌÈÎÈω˙ ˙ˆ¯‰ ‡˘Â˘ ÏÈ‚¯˙· ÂÈ˘Ú˘ ‰ÓÏ ‰Ó„·© ˘˜È· Á˜ω˘˙‡˘ STARTUPINFO– ‚ÂÒÓ ‰·Ó· ˙Â˘Ï Íȯˆ ¨ËÏÙ‰ ıÂ¯Ú ¯Â˙· ¯ÂȈ· ˘Ó˙˘È ˘„Á‰dwFlags ‰„˘· ∫˙„˘ È˘ ®Ô¯Á‡ ÈÙÏ ËÓ‚¯‡© CreateProcess≠Ï ÌȯȷÚÓ Â˙·Â˙ίÂÓ˘Ï Íȯˆ hStdOutput ‰„˘·Â ¨STARTF_USESTDHANDLES Ú·˜‰ ˙‡ ˜ÈÏ„‰Ï Íȯˆ

ÆÂÏ˘ ËÏÙ‰ ˙‡ ·Â˙ÎÏ Íȯˆ ˘„Á‰ ÍÈω˙‰ ÂÈχ˘ ‰‰ÊÓ‰ ˙‡

ZeroMemory( &start_info, sizeof(STARTUPINFO) );start_info.cb = sizeof(STARTUPINFO);start_info.dwFlags = STARTF_USESTDHANDLES;start_info.hStdOutput = write_pipe_duplicate;

˙‡ „„ÓÏ ¨ÂÓÂÈÒÏ ˙ÂÎÁÏ ¨ıˆÂÁ ÍÂ˙Ï ÂÏ˘ ËÏÙ‰ ˙‡ ÛÂÒ‡Ï ˘È ˘„Á‰ ÍÈω˙‰ ˙¯ÈˆÈ ¯Á‡Ï˙‡ ÌÈÈÒÏ ÏÂÎÈ Á˜Ϸ ÏÙÈˢ ÍÈω˙‰ ˙ÚÎ ÆÁ˜ÏÏ Ú„ÈÓ‰ ˙‡ ÁÂÏ˘Ï ¨ıˆÂÁ· Ú„ÈÓ‰ ˙ÂÓÎ

ÆÂ˙ÏÂÚÙ

˙ÂÈÎ˙ ıȯӉ HTTP ˙¯˘Î „˜Ù˙˙˘ win32-serve Ì˘· ˙ÈÎ˙ ·Â˙ÎÏ ÍÈÏÚ ÆÏÈ‚¯˙‰ÌÚ Â˙‡ ˆȯ‰© ÂÏ ÔÈʇ‰Ï ˘È˘ ¯Ú˘ ¯ÙÒÓ ¨„Á‡ ËÓ‚¯‡ ˘È ˙ÈÎ˙Ï ÆÁ˜ÏÏ ÔËÏÙ ˙‡ ÁÏ¢Â

Æ®root≠Ï ÌȯÂÓ˘ ±∞≤≥ „Ú ÌȯÙÒÓ‰ ¨‰Â·‚ ¯ÙÒÓ‰¯Âˆ· ÌÈËÓ‚¯‡Â ˙ÈÎ˙ Ì˘Î GET≠‰ ˙˘˜·· ÁÏ˘ Á˜ω˘ URL≠‰ ˙‡ ˘¯ÙÓ ˙¯˘‰ÌÈÚÈÙÂÓ + ÈÓÈÒ· ÌÈ„¯ÙÂÓ ¨ÂÈχ „ÂÓˆ·Â ¨ıȯ‰Ï ˘È˘ ˙ÈÎ˙ Ì˘ ‡Â‰ ı·Â˜‰ Ì˘ ∫‰‡·‰

URL≠‰ ¨Ï˘ÓÏ Æ˙ÈÎ˙Ï ÌÈËÓ‚¯‡‰

40

Page 41: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

http://localhost:2000/fsutil+volume+diskfree+c:

˙ÈÎ˙‰ ˙‡ ıȯ‰Ï ‰˘˜· ¯Â˙· ≤∞∞∞ ¯ÙÒÓ ¯Ú˘Ï ÔÈʇÓ ·˘ÁÓ Â˙‡· ı¯˘ ˙¯˘ È„È ÏÚ ˘¯ÂÙÈ˙ÈÎ˙‰ Ì˘ ˙‡ ˘ÙÁÓ ˙¯˘‰ ÆÌÈÂ˙‰ ËÓ‚¯‡‰ ÌÚ ®˙¯˘‰ ı¯ ·˘ Íȯ„Ó·© fsutilÍÏÈ‡Â Ì˘Ó ˙ʯÁÓ‰ ˙„¯Ù‰Â URL≠· Ô¯Á‡‰ /≠‰ ˘ÂÙÈÁ È„È ÏÚ URL≠‰ ÍÂ˙· ÌÈËÓ‚¯‡‰Â

Æ+ ÈÓÈÒ ˙¯ÊÚ·ÏÎ Æ˙ÈÎ˙‰Ó ËÏÙ‰ ‰È¯Á‡Ï ¨Á¯ ˙¯Â˘ ¨˙Â¯Â˘ ˘ÂÏ˘ Ô· header ¯ÈÊÁ‰Ï Íȯˆ ˙¯˘‰

˙ÂÈ‰Ï ˙ÂÎȯˆ header≠‰ ˙Â¯Â˘ ˘ÂÏ˘ Æ\r\n≠· ÌÈÈ˙Ò‰Ï ‰Îȯˆ header≠· ‰¯Â˘

HTTP/1.0 200 OKContent-Length: 133Content-Type: text/plain

‰Îȯˆ ‡È‰Â© ÁÏ˘È˘ ËÏÙ‰ ı·Â˜ ͯ‡ ˙‡ ‰È˘‰ ¨‰Áψ‰ „˜ ˙ÈÈˆÓ ‰Â˘‡¯‰ ‰¯Â˘‰¨ı·Â˜‰ ‚ÂÒ ˙‡ ˙È˘ÈÏ˘‰Â ¨®≥≥π Ú·˜‰ ˙‡ ‡Ï ¨ËÏÙ‰ Ï˘ È˙ÈÓ‡‰ ͯ‡‰ ˙‡ ÏÈÎ‰Ï Ô·ÂÓΉȇ ˙ÈÎ˙‰ ̇ Ï˘ÓÏ© Ï˘Î Ï˘ ‰¯˜Ó· ˜ÈÂ„Ó ‰‡È‚˘ „˜ ¯ÈÊÁ‰Ï ͯˆ Ôȇ ÆËÂ˘Ù ËÒ˜Ë Ô‡Î

Æ®Ï˘Î execv≠ ˙ÓÈȘÏÎ ˙‡ ‰¯˜Ó Ïη ‡Â¯˜Ï ˙¯˘‰ ÏÚ Æ˙ÂÁ˜ÏÓ GET ‚ÂÒÓ ˙¢˜·Ï ˜¯ ·È‚‰Ï Íȯˆ ˙¯˘‰\r\n ˙ÂʯÁÓ È˙˘ Ï˘ Ûˆ¯ „Ú ¯ÓÂÏΩ ‰ÓÂÈÒ ˙‡ ˙ÈȈӉ Á¯‰ ˙¯Â˘Ï „Ú HTTP≠‰ ˙˘˜·

Æ®ÌÈÙˆ¯˙ÈÎ˙ Â˙‡ ÌÈˆÈ¯Ó Ì˙‡ ·˘ Íȯ„ÓÏ ˜È˙Ú‰Ï È‡„Î ¨ÌÎÏ˘ ˙¯˘‰ ˙‡ ˜Â„·Ï ˙Ó ÏÚ

Æwindows\system32 Íȯ„Ó‰ ÍÂ˙Ó fsutil.exe ‡ date.exe Ô‚Ήχ ˙¢˜·Ó© ÆÏ·˜Ó ‡Â‰˘ HTTP≠‰ ˙¢˜· ÏÎ ˙‡ ÂÏ˘ ËÏÙ‰ ÍÂ˙Ï ÒÈÙ„‰Ï Íȯˆ ˙¯˘‰

Æ®ÌÈÁÏ¢ ÌÈلل˘ ˙¢˜·‰ ‰·Ó ÏÚ „ÂÓÏÏ Ô˙ÈÏÎ ¯Á‡Ï Â˙ÏÂÚÙ ˙‡ ˜Â„·Ï ÌÈ·Ï˘ ‰˘ÂÏ˘· Â˙‡ ˘ÓÓÏ ÚˆÂÓ ¨ÏÈ‚¯˙‰ ˙·ίÂÓ ˙‡ÙÓ

∫·Ï˘

Á˜ÏÏ ‰Ú·˜ ËÏÙ ˙¯Â˘ ÁÏ¢ „ÈÓ˙ URL≠‰Ó ÌÏÚ˙Ó˘ ˙¯˘ ¢ÓÓ Ô¢‡¯ ·Ï˘· ƱÍÈω˙ ˙¯ÈˆÈ ‡ÏÏ Á˜ω ˙‡ ˙¯˘Ó ˙¯˘‰ ¨ÔÎ ÂÓΠƮԷÂÓÎ Ìȇ˙Ó header Û¯Ȉ·©

Æaccept≠Ï ·Â˘ ‡¯Â˜Â ¨¯˘˜‰ ˙‡ ¯‚ÂÒ ¨˙Á‡ ‰˘˜· ˙¯˘Ó ˙¯˘‰ Æ˘„Á

ÆÁÂ˜Ï ÏÎ ¯Â·Ú ˘„Á ÍÈω˙ ¯ˆ ˙ÚÎ Æ≤

ÆÏÈ‚¯˙· ˘¯„‰ ÈÙÏ ˙˘˜Â·Ó‰ ˙ÈÎ˙‰ ˙ˆ¯‰Â URL≠‰ ÁÂÚÙ ˙‡ ÂÙÈÒ‰ È˘ÈÏ˘‰ ·Ï˘· Æ≥

∫˙¯˘‰ ÌÚ Í¯ÚÏ ˘È˘ ÌÈÈÂÒÈ

·˘ÁÓÓ Èˆ¯ ¨internet explorer ‡ mozilla Ô‚ΠÔÙ„Ù„Ó ÂÈχ ¢‚ ˙¯˘‰ ˙‡ ˆȯ‰ ƱƯÁ‡

ÌÚÙ ÏÎ Ô΄ÂÚÓ Íȯ‡˙ ÌÈÏ·˜Ó Ì˙‡˘ ‡„ ˙¯˘‰ ˙¯ÊÚ· date ˙ÈÎ˙‰ ˙‡ ˆȯ‰ Æ≤ÆÔلل‰ Ï˘ refresh≠‰ ‡ reload≠‰ ¯Â˙ÙÎ ÏÚ ÌȈÁÂÏ Ì˙‡˘

˙¯˘‰ ͯ„ ‡Ï© ˙Â¯È˘È ÌÎÏ˘ ˙¯˘Ï ˙˘‚Ï ÂÒ ¨‰˘¯ÂÓ ˙¯˘Ï ‰˘È‚ ÌÎÏ ˘È ̇ Æ≥ÆÏ·˜Ó ˙¯˘‰˘ HTTP≠‰ ˙¢˜· ˙‡ ¢‰Â ‰˘¯ÂÓ‰ ˙¯˘‰ ͯ„ Ì‚Â ®‰˘¯ÂÓ‰

¥±

Page 42: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

A Nonblocking Server Using Select

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

¥≤

Page 43: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Responding to Windows Messages

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

¥≥

Page 44: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Permissions and Access Control Lists

In this exercise you will write a program that can display a file’s list of securitypermissions, assign a list of permissions to a file, and copy permissions from onefile to another.

Reading the security information associated with a file. The system call Get-SecurityInfo reads the list of permissions for an object (a file, mutex, event,process, and so on). Under Windows, almost every operating-system object is se-curable, and can be assigned a list of permissions. This system call returns a struc-ture of type SECURITY_DESCRIPTOR, which contains four fields: the owner ofthe object, the groups of users to which the object belongs, the list of permissions(the so-called discretionary access-control list, DACL), and the list of access-logginginstructions (the system access-control list, SACL). The system call allows you torequest all four fields, or just a subset.

#include <aclapi.h>...DWORD GetNamedSecurityInfo(

TCHAR* name,SE_OBJECT_TYPE object_type,SECURITY_INFORMATION SecurityInfo,SID** owner,SID** group,ACL** dacl,ACL** sacl,SECURITY_DESCRIPTOR** security_descriptor);

The first argument is the name of the object, for example, a file name. The sec-ond argument describes the type of object; for files, this argument should beSE_FILE_OBJECT. Another system call, GetSecurityInfo, retrieves the securityinformation of an object referred to by an open handle, not an object specified byname.

The third argument specifies the fields we want to read, and its value shouldbe a bitwise or-ing of a subset of the following constants

• DACL_SECURITY_INFORMATION

• OWNER_SECURITY_INFORMATION

• GROUP_SECURITY_INFORMATION

• SACL_SECURITY_INFORMATION

The system call stores the output structure in memory, and sets the pointer whoseaddress is passed as the last argument. Upon successful return, this pointer willpoint to the returned structure. When it is no longer needed, the calling programmust release the memory allocated to it using LocalFree. In addition to thispointer, the system call also sets up to four more pointers to areas within the output

44

Page 45: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

structure. If the program does not request all four fields, then it can pass NULL inthe corresponding arguments 4–7.

To be able to read the DACL, the identity of the owner and the identity of thegroup, the process issuing the system call must have a READ_CONTROL permissionto the object. That is, a process may not have enough permission to even read thesecurity information of an object. To read the SACL, the process must have a specialoperating-system privelege. In this assignment, we do not read the SACL.

Obtaining human-readable user and group names. A permission entry in theaccess-control list grants or denies somebody the authorization to perform a cer-tain action on the object. The entity to who permissions are granted or denied iscalled a principal. Under windows, a principal may be a user, a group of users, acomputer, as well as several other entities. A principal may be defined in a singlecomputer, or in a security name space called a domain. A domain usually stores thenames of principals in an organization. The system calls that return the securityinformation of an object do not name principals using human-readable strings, butusing internal identifiers called security identifiers (SID’s). These identifiers mustbe translated into strings before they are presented to human users.

#include <windows.h>...BOOL LookupAccountSid(

TCHAR* system_name,SID* sid,TCHAR* name,DWORD* name_length,TCHAR* domain,DWORD* domain_length,SID_NAME_USE* what_kind);

The system call expects, in the second argument, a pointer to a security identifier,and translates it to a string. The translation is performed in the computer whosename is given in the first argument, or, if the first argument is NULL, in the com-puter on which the program runs.

The SID is translated into the name of the domain in which the SID is defined,and the principal’s name. They are returned in arguments 5 and 3, respectively.Arguments 6 and 4 describe to the system call the length of the buffers that theprogram allocated for these strings. If one or both are too small, the system callfails but sets these variables to the required length. A program can pass 0 as thelength of both strings to find out how long they are, allocate them, and issue thesystem call again with correct lengths.

The last argument returns the kind of principal represented by the given SID.The kind can be one of the following

• SidTypeUser

• SidTypeGroup

• SidTypeWellKnownGroup

45

Page 46: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

• SidTypeComputer

• and several more, such as a domain or a computer account that is alreadyclosed.

Processing an Access-Contol List. The DACL consists of a sequence of entriescalled ACE’s (access-control entry). The number of entries in a list is stored in theAceCount field of the ACL data type. The system call GetAce reads an entry ofthe list, selected by an index (starting at 0), and returns it in a structure of typeACCESS_ACE.

#include <windows.h>...BOOL GetAce(ACL* acl,

DWORD index,VOID** ace);

The call returns an entry by setting a pointer to it. An access-control list can containseveral types of entries, each represented by a different data structure. To allowprograms to identify the data type of a specific entry, all entries start with a fixedprefix, which describes, among other things, the type of the entry. The data type ofthe prefix is ACE_HEADER, and its AceType field describes the entry’s type. In thisassignment we will process entries of the following types,

• ACCESS_ALLOWED_ACCESS_TYPE

• ACCESS_DENIED_ACCESS_TYPE

but there are many more types. The data types corresponding to these entriesare ACCESS_ALLOWED_OBJECT_ACE and ACCESS_DENIED_OBJECT_ACE. As theirnames suggest, the first type grants access and the second denies access. In theaccess-allowed and access-denied data types there are two important fields: Mask,which describes the kinds of allowed/denied operations (each operation is repre-sented by one bit in this field), and SidStart, whose address is the address of theSID for the principal that is granted/denied access by the entry. That is, the pro-gram must compute the address of this field, and use this address as the address ofa SID structure. It seems that the rationale behind this odd interface is that SID isnot a fixed-size structure.

The best way to process access-control entries in a program is using a uniondata type to store the data returned by GetAce. This union should include boththe header data type and the access allowed/denied types. The program first usesthe header member of the union to determine the kind of entry represented bythe data, and then the appropriate specific member.

The allowed or disallowed actions are described by the bits of the Mask field.You can inspect, set, or clear individual bits using the following constants. The firstthree constants are specific to files, while the rest apply to most object types.

• FILE_GENERIC_READ

46

Page 47: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

• FILE_GENERIC_WRITE

• FILE_GENERIC_EXECUTE

• GENERIC_READ

• GENERIC_WRITE

• GENERIC_EXECUTE

• GENERIC_ALL

• DELETE

• READ_CONTROL

• WRITE_DAC

• WRITE_OWNER

• SYNCHRONIZE

Creating a new access-control list. We shall now learn how to create a new access-contol list. The list must be created in a contiguous area of memory that should beallocated with LocalAlloc. We shall later see how to compute the required sizeof this area. But first, let’s see the system calls that are involved.

#include <windows.h>...BOOL InitializeAcl(ACL* acl,

DWORD acl_length,DWORD acl_revision_level);

BOOL AddAccessAllowedAce(ACL* acl,DWORD acl_revision,DWORD access_mask,SID* sid);

BOOL AddAccessDeniedAce(/* same interface*/...);

The first system call initializes the list. Its arguments include the address of thememory area allocated for the list, the size of this area, and the version for therequired list. There are two possible versions of access-control lists in Windows,a simple one, ACL_REVISION, and a more sophisticated one, ACL_REVISION_DS.The first supports only generic actions, and is sufficient for this assignment. Thesecond version supports object-specific actions, such as separate actions for files,processes, events, and so on. Older releases of Windows support only the simpleversion.

The next two calls each add a single entry to a list: the first adds an entry thatpermits access, the second an entry that denies access. In each, the first argument isa pointer to the DACL, the second a revision constant (same as in the initialization

47

Page 48: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

system call), the third is a union of the operations that this entry allowes/forbids,and the last is an identifier of a principal.

The entries in the new list are ordered according to the order in which theywere added to the list.

The size of the memory area allocated to the list should include the size of anACL structure and the sum of the sizes of the entries. The size of entries varies,because the size of SID’s vary. Therefore, you should compute the size of eachentry using a formula such as

sizeof(ACCESS_DENIED_ACE)-sizeof(DWORD)+GetLengthSid(sid);

The system call GetLengthSid, returns, of course, the length of a given SID. Thereason that we subtract the size of a DWORD is that the SidStart member of theACE structure, whose only purpose is to mark the location of the variable-lengthSID, is a DWORD.

Linking the list that we have created to a specific operating-system object isperformed using the following system call:

#include <aclapi.h>...DWORD SetNamedSecurityInfo(

TCHAR* name,SE_OBJECT_TYPE object_type,SECURITY_INFORMATION SecurityInfo,SID* owner,SID* group,ACL* dacl,ACL* sacl);

The interface of this system call is similar to that of GetNamedSecurityInfo,except that here we pass pointers to structures rather than pointers to pointers tostructures.

The assignment. Write a program called win32-acl that can display the ownerand access-control list of a file, and that can assign a new access-control list to afile. When the program is executed with a single argument, a file name, if printsthe name of the owner of the file (both owner and group) and the file’s DACL. Forexample,

c:> win32-acl c\:tempowner: [U] SWIFT\Administratorgroup: [G] SWIFT\NoneAllow: [A] BUILTIN\Administratorsgeneric:---- file:RWE...Allow: [W] \CREATOR OWNERgeneric:A--- file:---...

48

Page 49: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

The letter in brackets before each principal describes the kind of principal: U forusers, G for an arbitrary group of users, and W for well-known groups (groups thatthe operating system defines implicitly, not with an explicit list of members). Theprogram should display, as in the example, the type of every entry in the list (allowor deny), the principal, and the generic and file permissions, where each action isrepresented by a sinle letter.

When the program is executed with two arguments, a file name and an excla-mation mark, it should assign a new two-entry DACL to the file. The first entryshould allow the owner of the file all the generic permissions, including readingand changing ownership and access-control permissions. The second entry shoulddeny the owner of the file writing to the file.

In addition to implementing the program, answer the following questions.\

1. Attach to your submission the output of the program on the file c:\tempand on the executable file containing the program itself.

2. Create a new file using a text editor, and attach the program’ output on thatfile.

3. Now assign a new DACL to this file using your program, and try to read fromit and to write to it. Do you have permissions to read and write? Attach theoutput of the program on the file with the new DACL.

4. Now try to inspect these permissions using the operating system’s graphi-cal user interface. Right click on the file in Windows Explorer and chooseproperties, then security. What happens, and why do you think it happens?

5. Allow the operating system to modify the permissions according to the sug-gestion in the dialog box. Can you now read and write from the file? Attachthe output of your program on the file now.

49

Page 50: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Impersonation and Access Tokens

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

µ∞

Page 51: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

Dynamically-Linked Libraries

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

µ±

Page 52: Advanced System-Programming Exercises in the Win32 …stoledo/win32-os-exercises/eng-exercises.pdf · Advanced System-Programming Exercises in the Win32 Environment Sivan Toledo School

The Windows Registry

ÓÈ˘¯ ˙‡ ‚Ȉ‰Ï ÏÎÂ˙˘ ˙ÈÎÂ˙ ·Â˙ÎÏ ‡È‰ ‰Ê ÏÈ‚¯˙ ˙¯ËÓ

52