1 / 25

System Programming Course introduction

Learn about Windows NT architecture, system calls, C library, and programming conventions. Explore Windows API functions and dual-mode architecture.

marciam
Download Presentation

System Programming Course introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System ProgrammingCourse introduction Getting Started …

  2. OBJECTIVES • Windows API, UNIX system calls, C library • Architecture • style and programming conventions • API and functions

  3. THE WINDOWS NT ARCHITECTURE OS/2Program WindowsProgram POSIXProgram Applications OS/2 Subsystem Windows Subsystem POSIX Subsystem Protected Subsystems NTExecutive Systems Services Process Manager I/O Manager Virtual Memory Manager KERNEL HAL: Hardware Abstraction HARDWARE

  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 NTsupports SMP • Windows 2003 also runs on Itanium, . . . • Windows 2003 for Win64 migration • Note: Windows CE also supports Windows on several processor architectures

  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. 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. User Applications User Level System Libreries System Call Interface File Subsystem Inter-Process Communication Process Kernel Control Scheduler Buffer Cache Level Subsystem Memory Character Block Management Device Drivers Hardware Control Hardware Hardware Level UNIX Kernel Architecture

  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 OperatingSystem 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 supportsthe ANSI-C library • Windows sockets • Remote Procedure Calls

  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. --------------------------------------------- syscall (params) ------------------- ------------------- ------------------- dispatch vector .… syscall routinebodies other process System Calls: how do they work ? Process Kernel (user mode) (kernel mode) switcher

  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 touser mode from kernel mode

  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. 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. 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. 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. Getting Started with Windows • Naming conventions • Programming conventions • Style • Sample program

  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. 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: • files pipes • processes memory mapping • threads events, mutexes, semaphores

  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. Windows NAMING CONVENTIONS • Long and descriptive • WaitForSingleObjectWaitForMultipleObjects • 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. Windows PROGRAMMING CONVENTIONS • <windows.h> 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. EXAMPLE: Windows FILE COPY (1/3) • /* Basic cp file copy program */ • /* cp file1 file2: Copy file1 to file2 */ • #include <windows.h> /* Always required for Windows */ • #include <stdio.h> • #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. 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. 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. How to copy a file • Four implementations: • • standard C library (chaptr01/cpC.c); • • Unix style (POSIX) (chaptr01/cpU.c); • • Win32 “base” (chaptr01/cpW.c); • • Win32 “convenience” function: CopyFile (chaptr01/cpCF.c);

More Related