Download presentation
Presentation is loading. Please wait.
Published byAvis Eaton Modified over 8 years ago
1
System Programming Course introduction Getting Started …
2
– 2 – OBJECTIVES Windows API, UNIX system calls, C library Architecture style and programming conventions API and functions
3
– 3 – KERNEL HAL: Hardware Abstraction HARDWARE Process Manager Systems Services Virtual Memory Manager I/O Manager NT Executive Protected Subsystems Applications OS/2 Program Windows Program POSIX Program OS/2 Subsystem Windows Subsystem POSIX Subsystem THE WINDOWS NT ARCHITECTURE
4
– 4 – WINDOWS NT 5 (1/2) All platforms use the Windows API, BUT there are differences: Windows NT4 (and above) has full NSA “Orange Book” C2 security features. “NT” means NT 4.0 and above (including all NT5) Windows 9X only runs on Intel x86 architecture Only NT supports SMP Windows 2003 also runs on Itanium,... Windows 2003 for Win64 migration Note: Windows CE also supports Windows on several processor architectures
5
– 5 – WINDOWS NT5 (2/2) Windows NT uses UNICODE international character set throughout Windows 9X limits asynchronous I/O to serial devices Windows NT has a fully protected kernel Windows NT supports the NTFS, a robust file system Windows 9X and CE will not support as many resources Open files, processes, etc. Many Windows 9X Windows functions have restricted implementations In general, Windows programs are portable between platforms at both the source and, mostly, binary level
6
– 6 – THE WINDOWS NT ARCHITECTURE Windows is the dominant environment running on the NT (all versions) executive OS/2 and POSIX compatility modes are rarely used Historical interest only
7
– 7 – UNIX Kernel Architecture
8
– 8 – C Library, UNIX system calls, Win32 API C Library Defined in the ANSI-C standard http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html Functions defined in standard POSIX (Portable Operating System Interface) 1003.1 POSIX functions NOT in C library are listed in: http://cplus.kompf.de/posixlist.html Windows Application Programming Interface (Win32 API). Windows supports the ANSI-C library Windows sockets Remote Procedure Calls
9
– 9 – What is a System Call? User-level processes (clients) request services from the kernel (server) via special “protected procedure calls” System calls provide: An abstraction layer between processes and hardware, allowing the kernel to provide access control, arbitration A virtualization of the underlying system A well-defined “API” (ASI?) for system services
10
– 10 – System Calls: how do they work ? ProcessKernel (user mode)(kernel mode) --------------- --------------- --------------- syscall (params) ------------------- syscall routine bodies.… dispatch vector switcher other process
11
– 11 – Implementing System Calls Initiating a system call is known as “trapping into the kernel” and is usually effected by a software initiated interrupt to the CPU Example: Intel (“int 80h”), ARM (“swi”) CPU saves current context, changes mode and transfers to a well- defined location in the kernel System calls are designated by small integers; once in the kernel, a dispatch table is used to invoke the corresponding kernel function A special assembly instruction (Intel “iret”) is used to return to user mode from kernel mode
12
– 12 – System Calls vs. Library Calls System calls can only be initiated by assembly code (special software interrupt instructions) Processes normally call library “wrapper routines” that hide the details of system call entry/exit Library calls are much faster than system calls Library calls (man 3), system calls (man 2) Some library functions: never call syscalls (strlen), some always call syscalls (mmap), some occasionally call syscalls (printf)
13
– 13 – Designing the Syscall Interface Important to keep interface small, stable Early UNIXes had about 60 system calls, Linux 2.6 has about 300; Solaris more, Window more still Aside: Windows does not publicly document syscalls and only documents library wrapper routines (unlike UNIX/Linux) Syscall numbers cannot be reused (!); deprecated syscalls are implemented by a special “not implemented” syscall (sys_ni)
14
– 14 – Dual-Mode Architecture Modern architectures all support at least two execution modes: regular (user) and privileged (kernel) Intel supports 4 modes or rings but only rings 0 and 3 are used Some instructions are not allowed in user mode and will cause an exception if executed Examples include: Setting up page tables Almost any kind of device I/O Changing the mode bit
15
– 15 – Trapping into the Kernel Trapping into the kernel involves executing a special assembly instruction that activates the interrupt hardware just like a device interrupt would but it is done by software instead so it is known as a “software interrupt” Intel uses the “int” (interrupt) instruction with the operand “80h” (80 hex = 128), indicating the interrupt handler that should be executed “int 80h” ultimately causes control to transfer to the assembly label: system_call in the kernel (found in arch/kernel/i386/entry.S) Every process in Linux has the usual user-mode stack as well as a small, special kernel stack that is part of the kernel address space; trapping involves switching the stack pointer to the kernel stack (and back)
16
– 16 – Getting Started with Windows Naming conventions Programming conventions Style Sample program
17
– 17 – THE Windows API Windows is the 32-bit API used by: Windows 9X (95, 98, Me) Windows NT Windows CE (palmtops, embedded systems, etc.) Win64 is very similar at the source level Supported on Windows 2003 and Itanium processor family Windows statements nearly always apply to Win64 There are several major subdivisions, including: Windows Management Graphics Device Interface (GDI) System Services Multimedia Remote Procedure Calls
18
– 18 – GETTING STARTED: Windows PRINCIPLES (1/2) Nearly every resource is an “object” identified and referenced by a “handle” of type HANDLE Handles’ role is similar to UNIX file descriptors, BUT Handles are blind objects, whereas UNIX file descriptors are integers (in a sequential order: 0, 1, 2, …) Kernel objects must be manipulated by WindowsAPIs Not object oriented HANDLE datatype objects include: filespipes processesmemory mapping threadsevents, mutexes, semaphores
19
– 19 – GETTING STARTED: Windows PRINCIPLES (2/2) Windows API is rich and flexible Many functions perform the same or similar operations Each function has numerous parameters and flags Many synchronization and communication components available Windows thread is the basic unit of execution, rather than a process A process can contain one or more threads Each process has its own code and data address space Threads share the process address space Threads are “lightweight” and more efficient than processes Used for servers, asynchronous I/O, …
20
– 20 – Windows NAMING CONVENTIONS Long and descriptive WaitForSingleObject WaitForMultipleObjects Predefined descriptive data types in upper case BOOL, DWORD, LPDWORD,... Predefined types avoid the * operator and make distinctions: LPTSTR (defined as TCHAR * ) and LPCTSTR (defined as const TCHAR * ) Variable names in API descriptions use “Hungarian” notation - we’ll avoid this convention lpFileName — long pointer [to a zero terminated string]
21
– 21 – Windows PROGRAMMING CONVENTIONS is always included is always included All objects identified by variables of type HANDLE CloseHandle function applies to (nearly) all objects Symbolic constants and flags which explain their meaning INVALID_HANDLE_VALUE and GENERIC_READ ReadFile, WriteFile, and many other Windows functions return Boolean values System error codes obtained through GetLastError () C library always available But you cannot fully exploit Windows with it
22
– 22 – EXAMPLE: Windows FILE COPY (1/3) /* Basic cp file copy program */ /* cp file1 file2: Copy file1 to file2 */ #include /* Always required for Windows */ #include #include #define BUF_SIZE 256 /* Increase for faster copy */ int main (int argc, LPTSTR argv []) { HANDLE hIn, hOut; /* Input and output handles */ DWORD nIn, nOut; /* Number bytes transferred */ CHAR Buffer [BUF_SIZE]; if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; }
23
– 23 – EXAMPLE: Windows FILE COPY (2/3) /* Create handles for reading and writing. Many*/ /*default values are used */ hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIn == INVALID_HANDLE_VALUE) { printf ("Cannot open input file\n"); return 2; } hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hOut == INVALID_HANDLE_VALUE) { printf ("Cannot open output file\n"); return 3; }
24
– 24 – EXAMPLE: Windows FILE COPY (3/3) /*Input and output file handles are open.*/ /*Copy file. Note end-of-file detection */ while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) WriteFile (hOut, Buffer, nIn, &nOut, NULL); /*Deallocate resources, such as open handles */ CloseHandle (hIn); CloseHandle (hOut); return 0; }
25
– 25 – How to copy a file Four implementations: standard C library (chaptr01/cpC.c); standard C library (chaptr01/cpC.c); Unix style (POSIX) (chaptr01/cpU.c); Unix style (POSIX) (chaptr01/cpU.c); Win32 “base” (chaptr01/cpW.c); Win32 “base” (chaptr01/cpW.c); Win32 “convenience” function: CopyFile (chaptr01/cpCF.c); Win32 “convenience” function: CopyFile (chaptr01/cpCF.c);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.