System Programming Course introduction Getting Started

25
System Programming Course introduction Getting Started … Getting Started …

description

– 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

Transcript of System Programming Course introduction Getting Started

Page 1: System Programming Course introduction Getting Started

System ProgrammingCourse introduction

Getting Started …Getting Started …

Page 2: System Programming Course introduction Getting Started

– 2 –

OBJECTIVES

Windows API, UNIX system calls, C libraryArchitecturestyle and programming conventionsAPI and functions

Page 3: System Programming Course introduction Getting Started

– 3 –

KERNELHAL: Hardware Abstraction

HARDWARE

Process Manager Systems ServicesVirtual Memory Manager

I/O ManagerNTExecutive

ProtectedSubsystems

ApplicationsOS/2

ProgramWindowsProgram

POSIXProgram

OS/2Subsystem

WindowsSubsystem

POSIXSubsystem

THE WINDOWS NT ARCHITECTURE

Page 4: System Programming Course introduction Getting Started

– 4 –

WINDOWS NT 5 (1/2)

All platforms use the Windows API, BUT there are All platforms use the Windows API, BUT there are differences: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 Note: Windows CE also supports Windows on several processor architecturesprocessor architectures

Page 5: System Programming Course introduction Getting Started

– 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

implementationsIn general, Windows programs are portable between platforms at

both the source and, mostly, binary level

Page 6: System Programming Course introduction Getting Started

– 6 –

THE WINDOWS NT ARCHITECTURE

Windows is the dominant environment running on the Windows is the dominant environment running on the NT (all NT (all versions)versions) executive executive

OS/2 and POSIX compatility modes are rarely usedOS/2 and POSIX compatility modes are rarely used

Historical interest onlyHistorical interest only

Page 7: System Programming Course introduction Getting Started

– 7 –

UNIX Kernel Architecture

Device DriversBlockCharacter

Buffer Cache

File Subsystem

Hardware Control

System Call Interface

ProcessControlSubsystem

System Libreries

User Applications

HardwareHardwareLevel

KernelLevel

UserLevel

Inter-ProcessCommunication

Scheduler

MemoryManagement

Page 8: System Programming Course introduction Getting Started

– 8 –

C Library, UNIX system calls, Win32 API

CC Library Library Defined in the ANSI-C standard

http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html

Functions defined in standard Functions defined in standard POSIX (Portable OperatingPOSIX (Portable Operating System Interface) 1003.1System Interface) 1003.1 POSIX functions NOT in C library are listed in:

http://cplus.kompf.de/posixlist.html

Windows Application Programming InterfaceWindows Application Programming Interface (Win32 API).(Win32 API). Windows supports the ANSI-C library Windows sockets Remote Procedure Calls

Page 9: System Programming Course introduction Getting Started

– 9 –

What is a System Call?

User-level processes (clients) request services from the User-level processes (clients) request services from the kernel (server) via special “kernel (server) via special “protected procedure protected procedure callscalls” ”

System calls provide: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

Page 10: System Programming Course introduction Getting Started

– 10 –

System Calls: how do they work ?

Process Kernel

(user mode) (kernel mode)

---------------------------------------------syscall (params)

-------------------

-------------------

-------------------

syscall routinebodies

.…

dispatch vector

switcher

other process

Page 11: System Programming Course introduction Getting Started

– 11 –

Implementing System Calls

Initiating a system call is known as “Initiating a system call is known as “trapping into the kerneltrapping into the kernel” and ” and is usually effected by a is usually effected by a softwaresoftware initiated initiated interruptinterrupt to the CPU to the CPU Example: Intel (“int 80h”), ARM (“swi”)

CPU CPU saves saves current current context, changes modecontext, changes mode and transfers to a well- and transfers to a well-defined location in the kerneldefined location in the kernel

System calls are designated by small integers; once in the kernel, System calls are designated by small integers; once in the kernel, a a dispatch tabledispatch table is used to invoke the corresponding kernel is used to invoke the corresponding kernel functionfunction

A special assembly instruction (Intel “iret”) is used to A special assembly instruction (Intel “iret”) is used to return toreturn to user modeuser mode from kernel mode from kernel mode

Page 12: System Programming Course introduction Getting Started

– 12 –

System Calls vs. Library Calls

System calls can only be initiated by System calls can only be initiated by assembly codeassembly code (special (special software interrupt instructions)software interrupt instructions)

Processes normally call Processes normally call library “wrapper routines”library “wrapper routines” that hide the that hide the details of system call entry/exitdetails of system call entry/exit

Library calls are much faster than system callsLibrary calls are much faster than system calls

Library calls (man 3), system calls (man 2)Library calls (man 3), system calls (man 2)

Some library functions:Some library functions: never call syscalls (strlen), some always call syscalls (mmap), some occasionally call syscalls (printf)

Page 13: System Programming Course introduction Getting Started

– 13 –

Designing the Syscall Interface

Important to keep interface small, stableImportant to keep interface small, stable

Early UNIXes had about 60 system calls, Linux 2.6 has about 300; Early UNIXes had about 60 system calls, Linux 2.6 has about 300; Solaris more, Window more stillSolaris more, Window more still

Aside: Windows does not publicly document syscalls and only Aside: Windows does not publicly document syscalls and only documents library wrapper routines (unlike UNIX/Linux)documents library wrapper routines (unlike UNIX/Linux)

Syscall numbers cannot be reused (!); deprecated syscalls are Syscall numbers cannot be reused (!); deprecated syscalls are implemented by a special “not implemented” syscall (sys_ni)implemented by a special “not implemented” syscall (sys_ni)

Page 14: System Programming Course introduction Getting Started

– 14 –

Dual-Mode Architecture

Modern architectures all support at least Modern architectures all support at least two execution modestwo execution modes: : regular (user) and privileged (kernel)regular (user) and privileged (kernel)

Intel Intel supports 4 modes or supports 4 modes or ringsrings but only rings 0 and 3 are used but only rings 0 and 3 are usedSome instructions are Some instructions are not allowednot allowed in user mode and will cause an in user mode and will cause an

exceptionexception if executed if executedExamples include:Examples include:

Setting up page tables Almost any kind of device I/O Changing the mode bit

Page 15: System Programming Course introduction Getting Started

– 15 –

Trapping into the KernelTrapping into the kernel involves executing a special assembly instruction Trapping into the kernel involves executing a special assembly instruction

that that activates the interrupt hardwareactivates the interrupt hardware just like a device interrupt would just like a device interrupt would but it is done by software instead so it is known as a “but it is done by software instead so it is known as a “software software interruptinterrupt””

Intel uses the “int” (interrupt) instruction with the operand “80h” (80 hex = Intel uses the “int” (interrupt) instruction with the operand “80h” (80 hex = 128), indicating the 128), indicating the interrupt handlerinterrupt handler that should be executed that should be executed

““int 80h” ultimately causes control to transfer to the assembly label: int 80h” ultimately causes control to transfer to the assembly label: system_call system_call in the kernel (found in arch/kernel/i386/entry.S)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, Every process in Linux has the usual user-mode stack as well as a small, special kernel stackspecial kernel stack that is part of the kernel address space; trapping that is part of the kernel address space; trapping involves switching the stack pointer to the kernel stack (and back)involves switching the stack pointer to the kernel stack (and back)

Page 16: System Programming Course introduction Getting Started

– 16 –

Getting Started with Windows

Naming conventionsNaming conventions

Programming conventionsProgramming conventions

StyleStyle

Sample programSample program

Page 17: System Programming Course introduction Getting Started

– 17 –

THE Windows API

Windows is the 32-bit API used by: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:There are several major subdivisions, including: Windows Management Graphics Device Interface (GDI) System Services Multimedia Remote Procedure Calls

Page 18: System Programming Course introduction Getting Started

– 18 –

GETTING STARTED:Windows PRINCIPLES (1/2)

Nearly every resource is an “object” identified and Nearly every resource is an “object” identified and referenced by a “handle” of type referenced by a “handle” of type HANDLEHANDLEHandles’ 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 WindowsAPIsKernel objects must be manipulated by WindowsAPIs Not object oriented

HANDLEHANDLE datatype objects include: datatype objects include: files pipes processes memory mapping threads events, mutexes, semaphores

Page 19: System Programming Course introduction Getting Started

– 19 –

GETTING STARTED:Windows PRINCIPLES (2/2)

Windows Windows API API is rich and flexibleis 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 Windows thread is the basic unit of execution, rather than a processprocess 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, …

Page 20: System Programming Course introduction Getting Started

– 20 –

Windows NAMING CONVENTIONSLong and descriptiveLong and descriptive

WaitForSingleObject WaitForMultipleObjects

Predefined descriptive data types in upper casePredefined descriptive data types in upper case BOOL, DWORD, LPDWORD, ...

Predefined types avoid the Predefined types avoid the ** operator and make operator and make distinctions:distinctions: LPTSTR (defined as TCHAR *) and LPCTSTR (defined as const TCHAR *)

Variable names in API descriptions use “Hungarian” Variable names in API descriptions use “Hungarian” notation - notation - we’ll avoid this conventionwe’ll avoid this convention lpFileName — long pointer [to a zero terminated string]

Page 21: System Programming Course introduction Getting Started

– 21 –

Windows PROGRAMMING CONVENTIONS

<windows.h><windows.h> is always included is always includedAll objects identified by variables of type All objects identified by variables of type HANDLEHANDLE

CloseHandle function applies to (nearly) all objects

Symbolic constants and flags which explain their meaningSymbolic constants and flags which explain their meaning INVALID_HANDLE_VALUE and GENERIC_READ

ReadFileReadFile, , WriteFileWriteFile, and many other Windows functions , and many other Windows functions return Boolean valuesreturn Boolean values

System error codes obtained through System error codes obtained through GetLastError ()GetLastError ()

C library always availableC library always available But you cannot fully exploit Windows with it

Page 22: System Programming Course introduction Getting Started

– 22 –

EXAMPLE: Windows FILE COPY (1/3)

/* Basic cp file copy program /* Basic cp file copy program */*//* cp file1 file2: Copy file1 to file2 /* cp file1 file2: Copy file1 to file2 */*/

#include <windows.h> /* Always required for Windows */#include <windows.h> /* Always required for Windows */#include <stdio.h>#include <stdio.h>#define BUF_SIZE 256 /* Increase for faster copy */#define BUF_SIZE 256 /* Increase for faster copy */int main (int argc, LPTSTR argv [])int main (int argc, LPTSTR argv []){{

HANDLE hIn, hOut; /* Input and output handles */HANDLE hIn, hOut; /* Input and output handles */DWORD nIn, nOut; /* Number bytes transferred */DWORD nIn, nOut; /* Number bytes transferred */CHAR Buffer [BUF_SIZE];CHAR Buffer [BUF_SIZE];if (argc != 3) {if (argc != 3) {

printf ("Usage: cp file1 file2\n");printf ("Usage: cp file1 file2\n");return 1;return 1;

}}

Page 23: System Programming Course introduction Getting Started

– 23 –

EXAMPLE: Windows FILE COPY (2/3)

/* Create handles for reading and writing. Many/* Create handles for reading and writing. Many */*//*/* default values are used default values are used */*/

hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL,hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);if (hIn == INVALID_HANDLE_VALUE) {if (hIn == INVALID_HANDLE_VALUE) {

printf ("Cannot open input file\n");printf ("Cannot open input file\n");return 2;return 2;

}}hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL,hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL,

CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hOut == INVALID_HANDLE_VALUE) {if (hOut == INVALID_HANDLE_VALUE) {

printf ("Cannot open output file\n");printf ("Cannot open output file\n");return 3;return 3;

}}

Page 24: System Programming Course introduction Getting Started

– 24 –

EXAMPLE: Windows FILE COPY (3/3)

/*/* Input and output file handles are open.Input and output file handles are open. */*//*/* Copy file. Note end-of-file detection Copy file. Note end-of-file detection */*/

while (ReadFile (hIn, Buffer, BUF_SIZE,while (ReadFile (hIn, Buffer, BUF_SIZE,&nIn, NULL) && nIn > 0)&nIn, NULL) && nIn > 0)

WriteFile (hOut, Buffer, nIn, &nOut, NULL);WriteFile (hOut, Buffer, nIn, &nOut, NULL);

/*/* Deallocate resources, such as open handles */Deallocate resources, such as open handles */CloseHandle (hIn); CloseHandle (hOut);CloseHandle (hIn); CloseHandle (hOut);return 0;return 0;

}}

Page 25: System Programming Course introduction Getting Started

– 25 –

How to copy a file

Four implementations: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 Win32 “convenience” function: CopyFile (chaptr01/cpCF.c);(chaptr01/cpCF.c);