1 - Systemcalls
1 - Systemcalls
1 - Systemcalls
A collection of system programs which allow the user to run application software.
OS Mechanisms Two mechanisms for controlling access to system resources are different modes of execution system calls
Processor Modes
Modern processors typically can operate in 2 modes: "user mode" and "kernel mode " .
User mode
processor executes normal instructions in the user's program.
Kernel mode
processor executes both normal and privileged instructions Processor can access additional registers and memory address space that are accessible only in kernel mode
Kernel
The kernel is the core of the operating system. It consists of code and data structures that are protected and can be accessed only in the kernel mode.
System Calls
User programs are not allowed to access system resources directly. They must ask the OS to do that for them. OS provides a set of functions that can be called by user programs to request for OS services. These functions are called system calls
System Calls
System calls run in kernel mode. They can be called by executing a special instruction (trap or software interrupt) which causes processor to switch to the kernel mode and jump to a previously defined location in the kernel. When the system call finishes, processor returns to the user program and runs in user mode.
Differences between library functions and system calls System calls run in kernel-mode but library functions run in user-mode and may call system calls. System calls are not linked to user programs. There are only a small number of system calls. (about 250 in Linux, see /usr/include/asm/unistd.h )
sbrk change the size of the space allocated in the heap data segment of the process (used by memory allocation functions, e.g. malloc)
Programming in UNIX
Programming in a UNIX system (and modern operating systems) often relies on two concepts:
(Solaris) or man
2 intro
(Linux)
errno and perror When a system call fails, it usually returns -1 and sets the global variable errno to a value describing what went wrong. The function perror() translates this error code into human-readable form and prints a system error message.
void perror(const char *s);
Using an API allows upgrading system software without changing user programs
Privileged Instructions
Two modes of execution --> two kinds of instructions : normal instructions, e.g., add, sub, etc. privileged instructions, e.g.,
read/write reserved registers
read/write protected memory
Exception Mechanism
Switching from user mode to kernel mode to perform system operations and return to user mode Exception is caused by hardware interupt (I/O) and software interrupt Software interrupt or trap Some special instructions cause processor to switch between user and kernel modes: trap , return_from_trap Exception Handler When an exception occurs, an exception handler is called. Exception handler code is in kernel's protected memory Exception hander code for software traps are system calls