CS330: Operating Systems
Process API: System calls
Recap: The process abstraction
- The OS creates a process when we run an executable
a.out
Execute $./a.out
- When we execute “a.out”on a
shell a process control block
(PCB) is created
- Does it raise some questions
related to the exact working?
PCB (a.out)
CPU state
PID
Memory state
File state
…..
OS
Process creation: What and How?
- How does OS come into action after typing “./a.out”in a shell?
- Who invokes the system calls?
- What exact system calls are invoked?
- Where is the genesis? What is the first user process?
System call
- CPU executing user code can invoke
the OS functions using system calls
OS
Process
code
CPU
OS code
System call
- CPU executing user code can invoke
the OS functions using system calls
- The CPU executes the OS handler
for the system call
OS
Process
code
CPU
OS code
System call
- CPU executing user code can invoke
the OS functions using system calls
- The CPU executes the OS handler
for the system call
- How system call is different from a
function call?
OS
Process
code
CPU
OS code
System call
- CPU executing user code can invoke
the OS functions using system calls
- The CPU executes the OS handler for
the system call
- How is system call different from a
function call?
- Can be thought as an invocation of
privileged functions (will revisit)
OS
Process
code
CPU
OS code
System calls and user libraries
Applications
Library API
printf( )
Operating System
write( )
Monitor/Console
dev_write( )
- Most system calls are invoked
through wrapper library functions
- However,all system calls can be
invoked directly
- For example, in Linux systems,
syscall( ) wrapper can be used
(Refer: man syscall)
A simple system call: getpid( )
pid_t getpid( )
{
PCB *current = get_current_process( );
return (current → pid);
}
main( )
{
printf(“%dn”, getpid( ));
}
OS
USER
Process creation: What and How?
- How does OS come into action after typing “./a.out”in a shell?
- System calls invoked to explicitly give control to the OS
- What exact system calls are invoked?
- Who invokes the system calls?
- Where is the genesis? What is the first user process?
Process creation - fork( )
- fork( ) system call is weird; not a typical “privileged”function call
- fork( ) creates a new process; a duplicate of calling process
- On success,fork
- Returns PID of child process to the caller (parent)
- Returns 0 to the child
Parent Process
Parent Process
Child Process
fork( )
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
fork ( )
Copy
process
PCB (parent)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
fork ( )
Fix return
value
- Child should get ‘0’and
parent gets PID of child as
return value.How?
PCB (parent)
CPU state
PID
Memory state
File state
…..
PCB (child)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
fork ( )
Fix return
value
- Child should get ‘0’and
parent gets PID of child as
return value.How?
- OS returns different values
for parent and child
PCB (parent)
CPU state
PID
Memory state
File state
…..
PCB (child)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
fork ( )
Fix return
value
- Child should get ‘0’and
parent gets PID of child as
return value.How?
- OS returns different values
for parent and child
- When does child execute?
PCB (parent)
CPU state
PID
Memory state
File state
…..
PCB (child)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
fork ( )
Fix return
value
- Child should get ‘0’and
parent gets PID of child as
return value.How?
- OS returns different values
for parent and child
- When does child execute?
- When OS schedules the
child process
PCB (parent)
CPU state
PID
Memory state
File state
…..
PCB (child)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork
Parent Process
Syscall
handler
(fork)
OS
scheduler
Child Process
ret = 0
- PC is next instruction after
fork( ) syscall,for both parent
and child
- Child memory is an exact
copy of parent
- Parent and child diverge
from this point
PCB (parent)
CPU state
PID
Memory state
File state
…..
PCB (child)
CPU state
PID
Memory state
File state
…..
Load a new binary - exec( )
- Replace the calling process by a new executable
- Code,data etc.are replaced by the new process
- Usually,open files remain open
Process (1. exe) Process (2.exe)
exec (2.exe)
Typical implementation of exec
Process
(1.exe)
Syscall
handler
(exec)
exec (“2.exe”)
- The calling process commits self
destruction! (almost)
PCB (1.exe)
CPU state
PID
Memory state
File state
…..
Code
Data
1.exe
Typical implementation of exec
Process
(1.exe)
Syscall
handler
(exec)
exec (“2.exe”)
- The calling process commits self
destruction! (almost)
- The calling process is cleaned up and
replaced by the new executable
- PID remains the same
cleanup
Load 2.exe
from disk
Typical implementation of exec
Process
(1.exe)
Syscall
handler
(exec)
return (0)
PCB (2.exe)
CPU state
PID
Memory state
File state
…..
Code
Data
2.exe
- The calling process commits self
destruction! (almost)
- The calling process is cleaned up and
replaced by the new executable
- PID remains the same
- On return,new executable starts
execution
- PC is loaded with the starting address of
the newly loaded binary
Process creation: What and How?
- How does OS come into action after typing “./a.out”in a shell?
- System calls invoked to explicitly give control to the OS
- What exact system calls are invoked?
- fork( ),exec ( ),wait( ) and exit( )
- Who invokes the system calls? In what order?
-
- Where is the genesis? What is the first user process?
wait( ) and exit( )
Parent
Child
fork( )
Parent
- The wait system call
makes the parent wait
for child process to
exit
wait( )
wait( ) and exit( )
Parent
Child
fork( )
Parent
- The wait system call
makes the parent wait
for child process to
exit
- On child exit( ),the
wait() system call
returns in parent
wait( ) exit( )
Shell command line: fork + exec + wait
BASH
BASH
fork( )
BASH
- The BASH process
calls fork( )
Shell command line: fork + exec + wait
BASH
BASH
fork( )
BASH
exec (“a.out” )
Child Process (a.out)
wait( )
- Parent process calls
wait( ) to wait for child
to finish
- Child process invokes
exec( )
Shell command line: fork + exec + wait
BASH
BASH
fork( )
BASH
exec (“a.out” )
Child Process (a.out)
wait( )
exit( )
- When child exits,
parent gets notified
- The BASH shell is
ready for the next
command at this point
of time
Process creation: What and How?
- How does OS come into action after typing “./a.out”in a shell?
- System calls invoked to explicitly give control to the OS
- What exact system calls are invoked?
- fork( ),exec ( ),wait( ) and exit( )
- Who invokes the system calls? In what order?
- The shell process (bash process)
- What is the first user process?
Unix process family using fork + exec
Parent Process (init)
Child Process (init)
fork( )
Parent Process (init)
exec (/bin/sh )
Child Process (sh)
- Fork and exec are used
to create the process
tree
- Commands: ps,pstree
- See the /proc directory
in linux systems
Process creation: What and How?
- How does OS come into action after typing “./a.out”in a shell?
- System calls invoked to explicitly give control to the OS
- What exact system calls are invoked?
- fork( ),exec ( ),wait( ) and exit( )
- Who invokes the system calls?
- The shell process (bash process)
- What is the first user process?
- In Unix systems,it is called the init process
- Who creates and schedules the init process?

3-process-api 3-process-api 3-process-api 3-process-api 3-process-api

  • 1.
  • 2.
    Recap: The processabstraction - The OS creates a process when we run an executable a.out Execute $./a.out - When we execute “a.out”on a shell a process control block (PCB) is created - Does it raise some questions related to the exact working? PCB (a.out) CPU state PID Memory state File state ….. OS
  • 3.
    Process creation: Whatand How? - How does OS come into action after typing “./a.out”in a shell? - Who invokes the system calls? - What exact system calls are invoked? - Where is the genesis? What is the first user process?
  • 4.
    System call - CPUexecuting user code can invoke the OS functions using system calls OS Process code CPU OS code
  • 5.
    System call - CPUexecuting user code can invoke the OS functions using system calls - The CPU executes the OS handler for the system call OS Process code CPU OS code
  • 6.
    System call - CPUexecuting user code can invoke the OS functions using system calls - The CPU executes the OS handler for the system call - How system call is different from a function call? OS Process code CPU OS code
  • 7.
    System call - CPUexecuting user code can invoke the OS functions using system calls - The CPU executes the OS handler for the system call - How is system call different from a function call? - Can be thought as an invocation of privileged functions (will revisit) OS Process code CPU OS code
  • 8.
    System calls anduser libraries Applications Library API printf( ) Operating System write( ) Monitor/Console dev_write( ) - Most system calls are invoked through wrapper library functions - However,all system calls can be invoked directly - For example, in Linux systems, syscall( ) wrapper can be used (Refer: man syscall)
  • 9.
    A simple systemcall: getpid( ) pid_t getpid( ) { PCB *current = get_current_process( ); return (current → pid); } main( ) { printf(“%dn”, getpid( )); } OS USER
  • 10.
    Process creation: Whatand How? - How does OS come into action after typing “./a.out”in a shell? - System calls invoked to explicitly give control to the OS - What exact system calls are invoked? - Who invokes the system calls? - Where is the genesis? What is the first user process?
  • 11.
    Process creation -fork( ) - fork( ) system call is weird; not a typical “privileged”function call - fork( ) creates a new process; a duplicate of calling process - On success,fork - Returns PID of child process to the caller (parent) - Returns 0 to the child Parent Process Parent Process Child Process fork( )
  • 12.
    Typical implementation offork Parent Process Syscall handler (fork) fork ( ) Copy process PCB (parent) CPU state PID Memory state File state …..
  • 13.
    Typical implementation offork Parent Process Syscall handler (fork) fork ( ) Fix return value - Child should get ‘0’and parent gets PID of child as return value.How? PCB (parent) CPU state PID Memory state File state ….. PCB (child) CPU state PID Memory state File state …..
  • 14.
    Typical implementation offork Parent Process Syscall handler (fork) fork ( ) Fix return value - Child should get ‘0’and parent gets PID of child as return value.How? - OS returns different values for parent and child PCB (parent) CPU state PID Memory state File state ….. PCB (child) CPU state PID Memory state File state …..
  • 15.
    Typical implementation offork Parent Process Syscall handler (fork) fork ( ) Fix return value - Child should get ‘0’and parent gets PID of child as return value.How? - OS returns different values for parent and child - When does child execute? PCB (parent) CPU state PID Memory state File state ….. PCB (child) CPU state PID Memory state File state …..
  • 16.
    Typical implementation offork Parent Process Syscall handler (fork) fork ( ) Fix return value - Child should get ‘0’and parent gets PID of child as return value.How? - OS returns different values for parent and child - When does child execute? - When OS schedules the child process PCB (parent) CPU state PID Memory state File state ….. PCB (child) CPU state PID Memory state File state …..
  • 17.
    Typical implementation offork Parent Process Syscall handler (fork) OS scheduler Child Process ret = 0 - PC is next instruction after fork( ) syscall,for both parent and child - Child memory is an exact copy of parent - Parent and child diverge from this point PCB (parent) CPU state PID Memory state File state ….. PCB (child) CPU state PID Memory state File state …..
  • 18.
    Load a newbinary - exec( ) - Replace the calling process by a new executable - Code,data etc.are replaced by the new process - Usually,open files remain open Process (1. exe) Process (2.exe) exec (2.exe)
  • 19.
    Typical implementation ofexec Process (1.exe) Syscall handler (exec) exec (“2.exe”) - The calling process commits self destruction! (almost) PCB (1.exe) CPU state PID Memory state File state ….. Code Data 1.exe
  • 20.
    Typical implementation ofexec Process (1.exe) Syscall handler (exec) exec (“2.exe”) - The calling process commits self destruction! (almost) - The calling process is cleaned up and replaced by the new executable - PID remains the same cleanup Load 2.exe from disk
  • 21.
    Typical implementation ofexec Process (1.exe) Syscall handler (exec) return (0) PCB (2.exe) CPU state PID Memory state File state ….. Code Data 2.exe - The calling process commits self destruction! (almost) - The calling process is cleaned up and replaced by the new executable - PID remains the same - On return,new executable starts execution - PC is loaded with the starting address of the newly loaded binary
  • 22.
    Process creation: Whatand How? - How does OS come into action after typing “./a.out”in a shell? - System calls invoked to explicitly give control to the OS - What exact system calls are invoked? - fork( ),exec ( ),wait( ) and exit( ) - Who invokes the system calls? In what order? - - Where is the genesis? What is the first user process?
  • 23.
    wait( ) andexit( ) Parent Child fork( ) Parent - The wait system call makes the parent wait for child process to exit wait( )
  • 24.
    wait( ) andexit( ) Parent Child fork( ) Parent - The wait system call makes the parent wait for child process to exit - On child exit( ),the wait() system call returns in parent wait( ) exit( )
  • 25.
    Shell command line:fork + exec + wait BASH BASH fork( ) BASH - The BASH process calls fork( )
  • 26.
    Shell command line:fork + exec + wait BASH BASH fork( ) BASH exec (“a.out” ) Child Process (a.out) wait( ) - Parent process calls wait( ) to wait for child to finish - Child process invokes exec( )
  • 27.
    Shell command line:fork + exec + wait BASH BASH fork( ) BASH exec (“a.out” ) Child Process (a.out) wait( ) exit( ) - When child exits, parent gets notified - The BASH shell is ready for the next command at this point of time
  • 28.
    Process creation: Whatand How? - How does OS come into action after typing “./a.out”in a shell? - System calls invoked to explicitly give control to the OS - What exact system calls are invoked? - fork( ),exec ( ),wait( ) and exit( ) - Who invokes the system calls? In what order? - The shell process (bash process) - What is the first user process?
  • 29.
    Unix process familyusing fork + exec Parent Process (init) Child Process (init) fork( ) Parent Process (init) exec (/bin/sh ) Child Process (sh) - Fork and exec are used to create the process tree - Commands: ps,pstree - See the /proc directory in linux systems
  • 30.
    Process creation: Whatand How? - How does OS come into action after typing “./a.out”in a shell? - System calls invoked to explicitly give control to the OS - What exact system calls are invoked? - fork( ),exec ( ),wait( ) and exit( ) - Who invokes the system calls? - The shell process (bash process) - What is the first user process? - In Unix systems,it is called the init process - Who creates and schedules the init process?