Sei sulla pagina 1di 20

Operating System

A collection of system programs which allow the user to run application software.

manages processes and resources.


abstracts the real hardware of the system and presents the users and applications with a virtual machine. has utilities for system administration tasks.

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 )

Solaris System Calls


Solaris has about 200 system calls.
Files and I/O Process management Interprocess communication Others

System Calls: Files & I/O


open, close create, unlink read, write lseek chmod mkdir, rmdir stat ioctl open and close a file create and remove a file read and write a file move to a specified byte in a file change access permission mode make and remove a directory get file status control device

System Calls: Process Management


fork exec exit wait create a new process execute a file terminate proces wait for a child process to terminate

sbrk change the size of the space allocated in the heap data segment of the process (used by memory allocation functions, e.g. malloc)

System Calls: Interprocess Communication


kill, sigsend pause sigaction send a signal to a process suspend process until signal set signal handler

Programming in UNIX
Programming in a UNIX system (and modern operating systems) often relies on two concepts:

controlled access to system services


realized by having system calls running in privileged
mode

common interface, various implementations


realized by API

Programming with System Calls


High level languages, including C, provide functions for programmers to use system calls just like normal function.
/usr/include/unistd.h man -s 2 intro

(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);

The argument string s is printed first, then the


system error message. Usually s is a user message indicating where the error occurs.

API (Application Programming Interface)


An API is a set of functions provided by an operating system or other system software.

An application program calls the functions to request the services.


An API clearly defines how to call functions and what the results are. (API is specification, not implementation) Examples: APIs for file system, graphics user interface, networking, etc.

Advantages of Using API


Portability
User programs that follow the APIs definition are portable.

An API can provide a common interface for different implementations of a service.


For example, the UNIX file system API is the same for all kinds of devices.

X windows API has many implementations on different machine architectures

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

Potrebbero piacerti anche