3 Process Syscall
3 Process Syscall
Mythili Vutukuru
CSE, IIT Bombay
API for process management
• What API does OS provide to user programs to manage processes?
• How to create, run, terminate processes?
• API = Application Programming Interface
= functions available to write user programs
• API provided by OS is a set of “system calls”
• System call is a function call into OS code that runs at higher CPU privilege level
• Sensitive operations (e.g., access to hardware) are allowed only at a higher
privilege level
• Some “blocking” system calls cause the process to be blocked and
context switched out (e.g., read() from disk), while others (e.g., getpid()
to get PID) can return immediately
2
Portability of code across OS
• POSIX API: standard set of system calls (and some C library functions)
available to user programs, defined for portability
• Programs written using POSIX API can run on any POSIX compliant OS
• Most modern OSes are POSIX compliant
• Program may still need to be recompiled for different architectures
• Program language libraries hide the details of invoking system calls
• The printf function in libc calls the write system call to write to screen
• User programs usually do not need to worry about invoking system calls
• ABI (application binary interface) is the interface between machine
code and underlying hardware: ISA, calling convention, …
3
Process related system calls (in Unix)
• fork() creates a new child process
• All processes are created by forking from a parent
• OS starts init process after boot up, which forks other processes
• The init process is ancestor of all processes, including shell/terminal
• exec() makes a process execute a given executable
• exit() terminates a process
• wait() causes a parent to block until child terminates
• Many variants of the above system calls exist in language libraries
with different arguments
4
Process creation: fork
• Parent process calls “fork” system call to create (spawn) a new process
• New child process created with new PID
• Memory image of parent is copied into that of child
• Parent and child run different copies of same code
8
Image credit: OSTEP
Example code with fork int ret = fork()
int x = 1
• What values of x are printed? if(ret == 0) {
• Parent and child both start with their own print “I am child”
independent copies of variable x in their x = x+1
print x
memory images
}
• Child increments its copy of x, prints 2 else if(ret > 0) {
• Parent decrements its copy of x, prints 0 print “I am parent”
x = x -1
print x
}
19
$echo hello
hello
Example shell code $
• How does the shell run a user command?
do forever {
• Read input from user input(command)
• Shell process forks a child process
int ret = fork()
• Child process runs exec with “echo”
program executable as argument, calls exit if(ret == 0) {
when done exec(command)
• Parent shell calls wait, blocks till child }
terminates, reaps it, goes back for next else {
input wait()
}
}
More on shell and commands
• Some commands already exist as programs written by OS developers
and compiled into executables
• Shell runs such command by simply calling exec in child process
• Some commands are implemented directly in shell code itself
• Think: why doesn’t shell exec command directly? Why fork a child?
• Do we want the shell program code to be rewritten fully?
• For “cd” command, “chdir” system call used to change directory of
parent process itself, no child process is forked. Why?
• Every process has a current working directory
• Do we want to change directory of some child process or shell itself?
$sleep 10 &
$
Foreground and background execution
• By default, user command runs in foreground, shell cannot accept next
command until previous one finishes
• Background execution: when we type command followed by &
• Shell starts child to run command, but does not wait for command to finish
• Background processes reaped at a later time by shell
• When? Periodically? When next input is typed?
• How? There is a way to invoke wait where parent is not blocked even if child has
not exited (explore it on your own)
• It is also possible to run multiple commands in the foreground
• One after the other serially (next command starts after previous finishes)
• Or, all start at same time in parallel
• Explore how such things can be done in the standard Linux shell 22
$ls > foo.txt
$
I/O redirection
• Every process has some I/O channels (“files”) open, which can be
accessed by file descriptors
• STDIN, STDOUT, STDERR open by default for all processes
• Parent shell can manipulate these file descriptors of child before
exec in order to do things like I/O redirection
• E.g., output redirection is done by closing the default STDOUT and
opening a regular file in its place
STDIN from keyboard