530 likes | 609 Views
Operating Systems. Recitation summary What you should know for the exam. You should know. System/API calls and their parameters discussed at the class or used in hw assignments (e.g. CreateFile creates/opens file depending on a parameter)
E N D
Operating Systems Recitation summary What you should know for the exam
You should know • System/API calls and their parameters discussed at the class or used in hw assignments • (e.g. CreateFile creates/opens file depending on a parameter) • Some details on Windows system internals we discussed • (e.g. object reference counting, memory management, file system) • Windows Networking Tools • (e.g. use ipconfig to find out my ip address) • Purpose and capabilities of networking protocols we have discussed • (e.g. Dns protocol is used to resolve symbolic name into numeric ip)
System Calls • No need to memorize exact details • But ,you are expected to know • Approximate name (exact for important function) • What it does (behavior) • Effect of most important parameters • Usage Pattern
Subjects • Win32 Types and Objects, Unicode Strings • Files • Processes • Threads • Process/Thread Synchronization • Virtual Memory • Memory Mapped Files • Dynamic Link Libraries • Networking and Windows Net Utilities • Sockets Programming • Windows Security • Structured Exception Handling
1. Win32 types, objects, Unicode • Reference Counting and CloseHandle • Handle Permissions and Security • Signaled State of an object • LPTSTR, TCHAR, DWORD • L”aa”, _T(“aa”) • GetLastError()
Handle to a kernel object is an index into the process handle table, and hence is invalid in any other process Handle table entry contains the system-space address (8xxxxxxx or above) of the data structure; this address is the same regardless of process context Although handle table is per-process, it is actually in system address space (hence protected) Event Object Handle Table HandleCount = 1 ReferenceCount = 1 Process B Handle Table 1.1 Handles, Pointers, and Objects System Space Process A handles index
Event Object Handle Table HandleCount = 1 ReferenceCount = 0 HandleCount = 2 ReferenceCount = 0 HandleCount = 3 ReferenceCount = 0 HandleCount = 3 ReferenceCount = 4 DuplicateHandle Thread (in a wait state for the event) Process B Handle Table Note: there is actually another data structure, a “wait block”, “between” the thread and the object it’s waiting for 1.1 Handles, Pointers, and Reference Count Process A System Space handles index
2.Files • CreateFile • ReadFile • WriteFile • SetFilePointer • FindFirstFile • FindNextFile • CloseHandle/FindClose • GetFileSize
2.1 CreateFile • HANDLE WINAPI CreateFile( __in LPCTSTR lpFileName, __in DWORD dwDesiredAccess, __in DWORD dwShareMode, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __in DWORD dwCreationDisposition, __in DWORD dwFlagsAndAttributes, __in_opt HANDLE hTemplateFile );
2.2 SetFilePointer • DWORD WINAPI SetFilePointer( __in HANDLE hFile, __in LONG lDistanceToMove, __inout_opt PLONG lpDistanceToMoveHigh, __in DWORD dwMoveMethod );
3.Processes • BOOL WINAPI CreateProcess( __in_opt LPCTSTR lpApplicationName, __inout_opt LPTSTR lpCommandLine, __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes, __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in BOOL bInheritHandles, __in DWORD dwCreationFlags, __in_opt LPVOID lpEnvironment, __in_opt LPCTSTR lpCurrentDirectory, __in LPSTARTUPINFO lpStartupInfo, __out LPPROCESS_INFORMATION lpProcessInformation );
3.Processes • CreateProcess • GetProcessExitCode • TerminateProcess • WaitForSingleObject
4. Threads • CreateThread • TerminateThread • WaitForSingleObject
4.Threads • HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in SIZE_T dwStackSize, __in LPTHREAD_START_ROUTINE lpStartAddress, __in_opt LPVOID lpParameter, __in DWORD dwCreationFlags, __out_opt LPDWORD lpThreadId );
Thread Thread Thread Thread Single and Multithreaded Processes code data files code data files registers stack registers registers registers stack stack stack single-threaded multi-threaded
Thread States • Five-state diagram for thread scheduling: • init: The thread is being created • ready: The thread is waiting to be assigned to a CPU • running: The thread’s instructions are being executed • waiting: The thread is waiting for some event to occur • terminated: The thread has finished execution interrupt quantum expired init terminated admitted schedulerdispatch exit ready running waiting I/O or eventcompletion waiting for I/O or event
Process Control Block (PCB) Process ID (PID) Parent PID • This is an abstract view • Windows implementation of PCB is split in multiple data structures … Next Process Block PCB List of open files Handle Table Image File Name Thread Control Block (TCB) List of ThreadControl Blocks Next TCB Program Counter … Registers …
CPU Switch from Thread to Thread Thread T1 Thread T2 Interrupt or system call executing Save state into TCB1 ready orwaiting Reload state from TCB2 Interrupt or system call ready orwaiting executing Save state into TCB2 Reload state from TCB1 ready orwaiting executing
5.Synchronization • CreateEvent/SetEvent • CreateMutex/ReleaseMutex • CreateSemaphore/ ReleaseSemaphore • InitializeCriticalSection/DeleteCriticalSection • WaitForSingleObject/WautForMultipleObjects • GetLastError()
5.1 Events • HANDLE WINAPI CreateEvent( __in_opt LPSECURITY_ATTRIBUTES lpEventAttributes, __in BOOL bManualReset, __in BOOL bInitialState, __in_opt LPCTSTR lpName ); • BOOL WINAPI SetEvent( __in HANDLE hEvent );
5.2 Mutex • HANDLE WINAPI CreateMutex( __in_opt LPSECURITY_ATTRIBUTES lpMutexAttributes, __in BOOL bInitialOwner, __in_opt LPCTSTR lpName ); • BOOL WINAPI ReleaseMutex( __in HANDLE hMutex ); If a thread already owns a mutex =>WaitForSingleObject does not block
5.3 Semaphores • HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, LONG lInitialCount, LONG lMaximumCount, LPCTSTR lpName ); • BOOL ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount );
5.4 Critical Section • void WINAPI InitializeCriticalSection( __out LPCRITICAL_SECTION lpCriticalSection ); • void WINAPI EnterCriticalSection( __inout LPCRITICAL_SECTION lpCriticalSection ); • void WINAPI LeaveCriticalSection( __inout LPCRITICAL_SECTION lpCriticalSection ); • void WINAPI DeleteCriticalSection( __inout LPCRITICAL_SECTION lpCriticalSection );
6. Virtual Memory • VirualAlloc/VirtualFree • VirtualLock/VirtualUnlock • SetProcessWorkingSetSize
6.1 Allocation • LPVOID WINAPI VirtualAlloc( __in_opt LPVOID lpAddress, __in SIZE_T dwSize, __in DWORD flAllocationType, __in DWORD flProtect ); • BOOL WINAPI VirtualFree( __in LPVOID lpAddress, __in SIZE_T dwSize, __in DWORD dwFreeType );
index physical page number (“page frame number” or “PFN”) index x86 Virtual Address Translation PFN 0 1 31 0 2 Page table selector Page table entry selector Byte within page 3 4 CR3 5 physical address 6 7 8 9 10 11 12 Page Tables (up to 512 per process, plus up to 512 system-wide) Page Directory (1024 entries) Physical Pages (up to 2^20)
Virtual Address Translation • The hardware converts each valid virtual address to a physical address virtual address Page Directory Virtual page number Byte within page Address translation (hardware) Page Tables if page not valid... page fault (exception, handled by software) Translation Lookaside Buffer Physical page number Byte within page a cache of recently- used page table entries physical address
6.2 Lock Memory • BOOL WINAPI VirtualLock( __in LPVOID lpAddress, __in SIZE_T dwSize ); • BOOL WINAPI VirtualUnlock( __in LPVOID lpAddress, __in SIZE_T dwSize );
7.Memory mapped Files • CreateFileMapping • MapViewOfFile/UnmapViewOfFile
Shared and Private Pages Process A Process B • For shared pages, multiple processes’ PTEs point to same physical pages 00000000 Physical Memory 7FFFFFFF 80000000 C0000000 C1000000 FFFFFFFF
7.1 CreateFileMapping • HANDLE WINAPI CreateFileMapping( __in HANDLE hFile, __in_opt LPSECURITY_ATTRIBUTES lpAttributes, __in DWORD flProtect, __in DWORD dwMaximumSizeHigh, __in DWORD dwMaximumSizeLow, __in_opt LPCTSTR lpName );
7.2 MapViewOfFile • LPVOID WINAPI MapViewOfFile( __in HANDLE hFileMappingObject, __in DWORD dwDesiredAccess, __in DWORD dwFileOffsetHigh, __in DWORD dwFileOffsetLow, __in SIZE_T dwNumberOfBytesToMap ); • BOOL WINAPI UnmapViewOfFile( __in LPCVOID lpBaseAddress );
8.Dynamic Link Libraries • LoadLibrary/FreeLibrary • GetProcAddress • __declspec(dllexport), __declspec(dllimport) • DllMain
Address Binding Sourceprogram • Addresses in source programs are symbolic • Compiler binds symbolic to relocatable addresses • Loader binds relocatable addresses to absolute addresses Binding can be done at any step: • i.e., compiler may generate absolute code (as for MS-DOS .COM programs) other object modules Compiler orassembler Compiletime Object module Systemlibraries Linkageeditor loadtime Loadmodule dynamicallyloadedsystemlibraries loaderr In-memorybinarymemoryimage executiontime(run time)
8.1 Run-Time • HMODULE WINAPI LoadLibrary( __in LPCTSTR lpFileName ); • FARPROC WINAPI GetProcAddress( __in HMODULE hModule, __in LPCSTR lpProcName );
9.Net Utilities • Ipconfig • Ping • Tracert • Route • Arp • Nslookup • Netstat
9. Opening Browser • Plug Network Cable->Broadcast DHCP to config • Type address->use dns server to translate • Have destination IP->use routing table to find next hop • Have IP of next hop->use arp table/protocol to translate destination IP into MAC address • Connect on TCP port 80 and send HTTP GET request • Wait for ack, resend if needed • Obtain HTML content , disconnect and show to a user
10.Sockets Programming • Socket • Bind • Listen • Accept • Connect • Send/Recv • Closesocket
11.Security • OpenProcessToken • LookupAccountSid • GetTokenInformation • ACL/ACE format/purpose
11. OpenProcessToken BOOL WINAPI OpenProcessToken( __in HANDLE ProcessHandle, __in DWORD DesiredAccess, __out PHANDLE TokenHandle ); BOOL WINAPI GetTokenInformation( __in HANDLE TokenHandle, __in TOKEN_INFORMATION_CLASS TokenInformationClass, __out_opt LPVOID TokenInformation, __in DWORD TokenInformationLength, __out PDWORD ReturnLength );
12.Structured Exception Handling • __try • __except • GetExceptionCode()