0% found this document useful (0 votes)
36 views30 pages

Process API

The document discusses the process abstraction in operating systems, detailing how processes are created and managed through system calls such as fork(), exec(), wait(), and exit(). It explains the role of the operating system in executing user commands and the interaction between user processes and the OS. Additionally, it highlights the significance of the init process as the first user process in Unix systems.

Uploaded by

aarohii.iitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views30 pages

Process API

The document discusses the process abstraction in operating systems, detailing how processes are created and managed through system calls such as fork(), exec(), wait(), and exit(). It explains the role of the operating system in executing user commands and the interaction between user processes and the OS. Additionally, it highlights the significance of the init process as the first user process in Unix systems.

Uploaded by

aarohii.iitb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CS330: Operating Systems

Process API: System calls


Recap: The process abstraction
- The OS creates a process when we run an executable

Execute - When we execute “a.out” on a


a.out $./a.out
shell a process control block
(PCB) is created
OS PCB (a.out) - Does it raise some questions
CPU state
PID related to the exact working?
Memory state
File state
…..
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


Process
code
the OS functions using system calls

OS
CPU

OS code
System call

- CPU executing user code can invoke


Process
code
the OS functions using system calls
- The CPU executes the OS handler
for the system call
OS
CPU

OS code
System call

- CPU executing user code can invoke


Process
code
the OS functions using system calls
- The CPU executes the OS handler
for the system call
OS
CPU - How system call is different from a
function call?

OS code
System call
- CPU executing user code can invoke
the OS functions using system calls
Process
code - The CPU executes the OS handler for
the system call
- How is system call different from a
OS
CPU function call?
- Can be thought as an invocation of
privileged functions (will revisit)
OS code
System calls and user libraries

Applications

printf( )
- Most system calls are invoked
Library API
through wrapper library functions
write( ) - However, all system calls can be
Operating System
invoked directly
- For example, in Linux systems,
dev_write( )
syscall( ) wrapper can be used
Monitor/Console
(Refer: man syscall)
A simple system call: getpid( )
USER
OS
main( ) pid_t getpid( )
{ {
printf(“%d\n”, getpid( )); PCB *current = get_current_process( );
} return (current → pid);
}
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( )
Parent Process

Parent Process fork( )

Child Process

- 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
Typical implementation of fork

Parent Process

fork ( )

Syscall Copy
handler process
(fork)

PCB (parent)
CPU state
PID
Memory state
File state
…..
Typical implementation of fork

Parent Process

fork ( )
- Child should get ‘0’ and
parent gets PID of child as
Fix return
Syscall value return value. How?
handler
(fork)

PCB (parent) PCB (child)


CPU state CPU state
PID PID
Memory state Memory state
File state File state
….. …..
Typical implementation of fork

Parent Process

fork ( )
- Child should get ‘0’ and
parent gets PID of child as
Fix return
Syscall value return value. How?
handler
(fork) - OS returns different values
for parent and child
PCB (parent) PCB (child)
CPU state CPU state
PID PID
Memory state Memory state
File state File state
….. …..
Typical implementation of fork

Parent Process

fork ( )
- Child should get ‘0’ and
parent gets PID of child as
Fix return
Syscall value return value. How?
handler
(fork) - OS returns different values
for parent and child
PCB (parent) PCB (child) - When does child execute?
CPU state CPU state
PID PID
Memory state Memory state
File state File state
….. …..
Typical implementation of fork

Parent Process

fork ( )
- Child should get ‘0’ and
parent gets PID of child as
Fix return
Syscall value return value. How?
handler
(fork) - OS returns different values
for parent and child
PCB (parent) PCB (child) - When does child execute?
CPU state CPU state
PID PID - When OS schedules the
Memory state Memory state
File state File state child process
….. …..
Typical implementation of fork

Parent Process Child Process


- PC is next instruction after
ret = 0 fork( ) syscall, for both parent
and child
Syscall
handler OS
scheduler
- Child memory is an exact
(fork)
copy of parent
- Parent and child diverge
PCB (parent) PCB (child)
CPU state CPU state from this point
PID PID
Memory state Memory state
File state File state
….. …..
Load a new binary - exec( )

Process (1. exe) exec (2.exe) Process (2.exe)

- Replace the calling process by a new executable


- Code, data etc. are replaced by the new process
- Usually, open files remain open
Typical implementation of exec
Process - The calling process commits self
(1.exe)
destruction! (almost)
exec (“2.exe”)

Syscall
handler
(exec)

PCB (1.exe)
CPU state Code
PID
Memory state Data
File state
…..
1.exe
Typical implementation of exec
Process - The calling process commits self
(1.exe)
destruction! (almost)
exec (“2.exe”)
- The calling process is cleaned up and
Syscall
replaced by the new executable
handler
(exec)
- PID remains the same
Load 2.exe
cleanup from disk
Typical implementation of exec
Process - The calling process commits self
(1.exe)
destruction! (almost)
return (0)
- The calling process is cleaned up and
Syscall
replaced by the new executable
handler
(exec)
- PID remains the same
- On return, new executable starts
PCB (2.exe)
CPU state Code execution
PID - PC is loaded with the starting address of
Memory state Data
File state
…..
the newly loaded binary
2.exe
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

fork( ) - The wait system call


makes the parent wait
Parent Child for child process to
exit
wait( )
wait( ) and exit( )
Parent

fork( ) - The wait system call


makes the parent wait
Parent Child for child process to
exit
wait( ) exit( ) - On child exit( ), the
wait() system call
returns in parent
Shell command line: fork + exec + wait
BASH

fork( ) - The BASH process


calls fork( )
BASH BASH
Shell command line: fork + exec + wait
BASH

fork( ) - Parent process calls


wait( ) to wait for child
BASH BASH to finish
- Child process invokes
wait( ) exec (“a.out” )
exec( )

Child Process (a.out)


Shell command line: fork + exec + wait
BASH

fork( ) - When child exits,


parent gets notified
BASH BASH - The BASH shell is
ready for the next
wait( ) exec (“a.out” )
command at this point
of time

exit( ) Child Process (a.out)


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)

fork( ) - Fork and exec are used


to create the process
tree
Parent Process (init) Child Process (init)
- Commands: ps, pstree
- See the /proc directory
exec (/bin/sh )
in linux systems

Child Process (sh)


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?

You might also like