You are on page 1of 16

OPERATING SYSTEMS (CS C372 & IS C362)

LECTURE 10: SYSTEM CALLS

fscanf system call execution

Call to read()

read() in C program

read() in the C library

read system call

21 August 2012

Biju K Raveendran@BITS Pilani.

System call interface


App Software
API

User-running
syscall - trap exit()

Sys Software
Sys Call (OS) Interface

Kernel-running
read(), write(), wait(), sleep()

Zombie

OS
Sw-Hw I-face (drivers)

Blocked

Ready

Hardware

Taken from Serguei A. Mokhov, Concordia University

21 August 2012

Biju K Raveendran@BITS Pilani.

Invoking system call handler & executing system call

call read()

read() wrapper

system_call()

sys_read()

Application

C library read() wrapper

System call handler

sys_read()

User Space

Kernel Space

21 August 2012

Biju K Raveendran@BITS Pilani.

21 August 2012

Biju K Raveendran@BITS Pilani.

Each arrow in the figure represents a jump in CPU instruction flow, and each jump may require flushing the prefetch queue and possibly a ``cache miss'' event. Transitions between user and kernel space are especially important(Context switch), as they are the most expensive in processing time and prefetch behavior.

21 August 2012

Biju K Raveendran@BITS Pilani.

Standard C Library Example

C program invoking printf() library call, which calls write() system call

21 August 2012

Biju K Raveendran@BITS Pilani.

New System calls Design considerations

What is the purpose of new system call?

If possible it should have only one purpose

What are the new system calls arguments, return value and error codes? Is it portable and robust? Verifying system call parameters

Ensure it is valid and legal


Important to check the validity of pointers a user gives Before following a pointer into user space, the system must ensure

The pointer points to a region of memory in user space The pointer points to a region of memory in the processs address space If reading, memory is marked readable. If writing memory is marked writable.
Biju K Raveendran@BITS Pilani. 8

21 August 2012

Difference between System call and function call


System call typically accessed via function calls. System call involves context switching (from user to kernel and back) where as function call does not. Takes much longer time than function calls. Avoiding excessive system calls might be a wise strategy for programs that need to be tightly optimized. Most of the system calls return a value (error if failed) where as it is not necessary for subroutine. If an error (return value 1) use perror (Message) to print the error.

21 August 2012

Biju K Raveendran@BITS Pilani.

Pros and Cons of system calls

Pros

Simple to implement and easy to use In Linux it is very fast Need a system call number, which needs to be officially assigned to you during the developmental kernel series. Once stabilized, the interface can not change with out breaking user space application Each architecture needs to separately register the system call and support it. For simple exchanges of information, a system call is overkill (overheads because of cache miss and context switch)
Biju K Raveendran@BITS Pilani. 10

Cons

21 August 2012

Types of System Calls


Process control File management Device management Information maintenance Communications

21 August 2012

Biju K Raveendran@BITS Pilani.

11

System call
System Calls for process control
fork() wait(), waitpid() execl(), execlp(), execv(), execvp() exit() Signal - signal(sig, handler), kill(sig, pid), alarm(), pause() getpid(), getppid() nice()

21 August 2012

Biju K Raveendran@BITS Pilani.

12

System call
System calls for low level file I/O
creat(name, permissions), close(fd), unlink(fd), write(fd, buffer, n_to_write), System calls open close read write lseek
21 August 2012

man (2)

open(name, mode), read(fd, buffer, n_to_read), lseek(fd, offest, whence)

Library calls fopen fclose fread, getchar, scanf, fscanf, getc, fgetc, gets, fgets fwrite, putchar, printf, fprintf, putc, fputc, puts, fputs fseek
Biju K Raveendran@BITS Pilani. 13

System call
System Calls for IPC
pipe High level popen, pclose Low level pipe(filedes), dup(fd) FIFO (named pipe in Linux Unidirectional(Simplex) Communication) mkfifo, mknod Message Queues msgctl, msgget, msgsnd, msgrcv Shared Memory shmat, shmctl, shmdt, shmget
21 August 2012 Biju K Raveendran@BITS Pilani. 14

Examples of Windows and Unix System Calls

21 August 2012

Biju K Raveendran@BITS Pilani.

15

Information about system calls and library routines


man 2 read (if read is a system call) man 3 read (if read is a library routine)

21 August 2012

Biju K Raveendran@BITS Pilani.

16

You might also like