Chapter Zero - Politecnico di Torino

Report
System Programming
Course introduction
Getting Started …
OBJECTIVES

Windows API, UNIX system calls, C library
 Architecture
 style and programming conventions
 API and functions
–2–
THE WINDOWS NT ARCHITECTURE
Applications
Protected
Subsystems
NT
Executive
OS/2
Program
Windows
Program
OS/2
Subsystem
Windows
Subsystem
Process Manager
POSIX
Program
POSIX
Subsystem
Systems Services
I/O Manager
Virtual Memory Manager
KERNEL
HAL: Hardware Abstraction
–3–
HARDWARE
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
–4–
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

–5–
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
–6–
UNIX Kernel Architecture
User Applications
User
Level
System Libreries
System Call Interface
File Subsystem
Kernel
Level
Inter-Process
Communication
Process
Buffer Cache Control Scheduler
Subsystem
Character
Block
Memory
Management
Device Drivers
Hardware Control
Hardware
Level
–7–
Hardware
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).



–8–
Windows supports the ANSI-C library
Windows sockets
Remote Procedure Calls
What is a System Call?
User-level processes (clients) request services from the
kernel (server) via special “protected procedure
calls”
System calls provide:



–9–
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
System Calls: how do they work ?
Process
Kernel
(user mode)
(kernel mode)
dispatch vector
-------------------------------------------
.…
syscall (params)
syscall routine
bodies
-------------------
-------------------------------------
– 10 –
other process
switcher
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 welldefined 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
– 11 –
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:



– 12 –
never call syscalls (strlen),
some always call syscalls (mmap),
some occasionally call syscalls (printf)
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)
– 13 –
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:



– 14 –
Setting up page tables
Almost any kind of device I/O
Changing the mode bit 
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)
– 15 –
Getting Started with Windows
Naming conventions
Programming conventions
Style
Sample program
– 16 –
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:





– 17 –
Windows Management
Graphics Device Interface (GDI)
System Services
Multimedia
Remote Procedure Calls
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:



– 18 –
files
processes
threads
pipes
memory mapping
events, mutexes, semaphores
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





– 19 –
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, …
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

– 20 –
lpFileName — long pointer [to a zero terminated string]
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

– 21 –
But you cannot fully exploit Windows with it
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;
}
– 22 –
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;
}
– 23 –
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;
}
– 24 –
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);
– 25 –

similar documents