Professional Documents
Culture Documents
Call to read()
read() in C program
21 August 2012
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
21 August 2012
call read()
read() wrapper
system_call()
sys_read()
Application
sys_read()
User Space
Kernel Space
21 August 2012
21 August 2012
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
C program invoking printf() library call, which calls write() system call
21 August 2012
What are the new system calls arguments, return value and error codes? Is it portable and robust? Verifying system call parameters
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
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
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
21 August 2012
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
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)
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
21 August 2012
15
man 2 read (if read is a system call) man 3 read (if read is a library routine)
21 August 2012
16