0% found this document useful (0 votes)
53 views

Cs837: Adv. Operating Systems: Dr. Mian M.Hamayun

The document discusses process creation, hierarchy, and operating system execution in Unix-like systems. It describes how a parent process uses the fork() and exec() system calls to create a child process as a copy of itself. The child process can then optionally call exec() to replace its process image with a new program. The parent process receives the child's process ID from fork(), allowing it to track and wait for the child. This establishes a strict parent-child hierarchy among processes in Unix.

Uploaded by

Shah Zaib
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)
53 views

Cs837: Adv. Operating Systems: Dr. Mian M.Hamayun

The document discusses process creation, hierarchy, and operating system execution in Unix-like systems. It describes how a parent process uses the fork() and exec() system calls to create a child process as a copy of itself. The child process can then optionally call exec() to replace its process image with a new program. The parent process receives the child's process ID from fork(), allowing it to track and wait for the child. This establishes a strict parent-child hierarchy among processes in Unix.

Uploaded by

Shah Zaib
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/ 37

CS837: ADV.

OPERATING SYSTEMS
PROCESS CREATION, HIERARCHY & OPERATING
SYSTEM EXECUTION

Dr. Mian M.Hamayun


[email protected]
http://seecs.nust.edu.pk/faculty/mianhamayun.html
Process Creation 
Process Creation in Unix 
max 
Process  Stack  Stack segment 
List 

Process 
Process 
Control 
Process 
Control 
Process 
Block 
Control 
Block 
Control 
Block  Data segment 
Block 

Heap 

Data 

Program code  Text segment

Kernel  protected 

Process List Structures 
Process Creation 
• Assign unique process identifier to the new 
process 
• Allocate space for the process 
– Allocate memory for process image (program, 
data, stack) 
• Initialise the Process Control Block 
• Add process to the Ready queue 
Process Hierarchy in Unix
• Unix has a strict process hierarchy 
– a parent process creates a new child process 
– Child process is a copy of the parent process 
• When is a process created 
– OS starts and creates processes and services 
– User login, creates a shell 
– Process spawns another process (parent – child 
processes) 
 
Process Creation under Unix 
• A process creates new processes with the kernel 
system calls fork() and exec() 
– A new slot in the process table is allocated 
– A unique process ID is assigned to the child process 
– The process image of the parent process is copied, 
except the shared memory areas 
– Child process now also owns the same open files as 
parent 
– Child process is added to the Ready queue and will 
start execution 
Programming Process Creation 
• System call fork() 
int main( ... )
{
int pid = fork() ;
if ( pid == 0 )
{
// child process
// program code for child
}
else if ( pid > 0 )
{
// parent process
// program code for parent
}

return 0 ;
}
Process Creation under Unix 
Parent starts

int main()
{
// printf (“Parent\n”);
. . .
. . .
int ret = fork() ;
. . .
. . .
. . .
. . .
. . .
}
Process Creation under Unix 
Kernel Mode 
Parent starts

int main()
{
// printf (“Parent\n”);
. . .
. . .
int ret = fork() ;
. . .
. . .
. . .
. . .
. . .
}
Process Creation under Unix 
Kernel Mode 
Parent starts

int main() int main()


{ {
// printf (“Parent\n”); // printf (“Parent\n”);
. . . . . .
. . . Parent copied  . . .
int ret = fork() ; int ret = fork() ;
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
} }
Process Creation under Unix 
Kernel Mode 
Parent starts

int main() int main()


{ {
// printf (“Parent\n”); // printf (“Parent\n”);
. . . . . .
. . . Parent copied  . . .
int ret = fork() ; int ret = fork() ;
. . . . . . Child starts 
. . . Parent continues  . . . Child continues 
. . . . . .
. . . . . .
. . . . . .
} }
Process Creation under Unix 
• A paradoxical situation occurs: 
– When fork() is called by parent process, there is 
only one process 
– When fork() is finished and returns a return value, 
there are two processes 
• Child process is clone of parent process, with that, also 
the call of fork() has been cloned 
• Parent process continues execution at the return from 
calling fork() 
• Child process begins executing at the same point in the 
code as parent – at the return from calling fork() 
 
 
Process Creation under Unix 
• Distinction between parent and child process 
– System call fork() has different return value in 
parent and child processes 
• Return value of fork() 
– Parent process: it returns the process ID of the 
child process 
– Child process: fork() return 0 
Programming Process Creation 
• Our program has to serve the parent as well as the 
child process 
int main( ... )
{
int pid = fork() ;
if ( pid == 0 )
{
// child process
// program code for child
}
else if ( pid > 0 )
{
// parent process
// program code for parent
}

return 0 ;
}
Programming Process Creation 
• Run a new program in child process image 
– Use of system call exec() 
int main() int main()
{ {
// printf (“Parent\n”); // printf (“Parent\n”);
. . . . . . New program 
. . . Parent copied  . . .
int ret = fork() ; if ( fork()==0 )
. . . {
. . . Parent continues  exec ( “myprogram”);
. . . . . .
. . . . . .
. . . . . .
} }
Programming Process Creation 
• System call exec() replaces content of cloned 
image with new program  New program starts 
int main() int main() executing 
{ {
// printf (“Parent\n”); // printf (“myprogram\n”);
. . . . . .
. . . . . .
int ret = fork() ; . . .
. . . . . .
. . . Parent continues  . . .
. . . }
. . .
. . .
}
System function exec() 
• exec() comes in different flavours 
– execl() 
– execle() 
– execlp() 
– execv() 
– execve() 
– execvp() 
• Meaning of the name­annotations 
– “e”: an array of pointers to env variables is explicitly passed to the new 
process image 
– “l”: command­line arguments are passed individually 
– “p”: PATH env variable is used to find file name of program to be 
executed 
– “v”: command­line arguments are passed as an array of pointers 
Example: Starting a Command in the 
int main( ... ) Shell 
{
if ( (pid = fork()) == 0 ) /* child process */
{
printf ( "Child process id: %i\n", getpid() ) ;
/* replace parent process image with executing a new program */

err = execlp ( "/bin/ls", "ls", NULL ) ;

fprintf ( stderr, "Child execution: %d\n, err ) ;


exit ( err ) ;
}
else if ( pid > 0 ) /* parent process */
{
printf ( "Parent process id %d\n", getpid() ) ;
/* parent will wait for child process to complete */
child = waitpid ( pid, &status, 0) ;
}

return 0 ;
}
Process wait 

• Parent process may call system function 
waitpid() to wait for the exit of child process 
Unix Process Hierarchy 
ROM 
Boot 
Loader 
sched 
Kernel 
pid = 0 

Unix SysV, BSD: /etc/init  init  User Processes 


Linux: /sbin/init  pid = 1 

inetd  other processes, such 
as GUI, etc, 

ftpd  telnetd  httpd  xterm 

bash  bash 

other user programs
gedit 
Windows Process Relationships 
• Example Windows 
– Windows does not have any concept of a process 
hierarchy 
– All processes are equal 
– Process handle: when a process is created by a 
“parent” process, the parent receives a process
handle, helps to control the “child” 
– This handle can be transferred to other processes 
(processes in Unix cannot disinherit their children) 
Execution of the Operating 
System 
Execution of the Operating System 
• Is the Operating 
System itself a 
process? 
• Is it executing 
– Separately, only when 
processes are 
interrupted? 
– As part of the user 
process images? 
– As a set of processes 
(micro kernel)? 
Execution of the Operating System 
• Is the Operating System itself running as a 
process? Or as a collection of processes? 
• Various design options 
– Non­process kernel: 
• Traditional approach, many older operating systems 
• Kernel only executes when user processes interrupted 
– Execution of all OS functions in the context of a 
user process, mode switch 
– Process­based Operating systems, mode switch 
and context switch 
Execution of the Operating System 
• There are different forms of kernels 
– Kernel operations separate from user operations 
• Only user programs are executing as processes 
• OS code executes in privileged mode, OS is a supervisor 
– All OS software is virtually executed within a user 
process, only Dispatcher outside user programs 
(process switching functionality 
• OS is a collection of routines that are called by user 
programs 
• Mode switch occurs when user calls system routines 
– OS runs as a collection of system processes, 
dispatcher (process switching) is a small separate part
Non­process Kernel 
• Many older operating systems 
• Kernel acts as a monitor for user processes, only 
user processes regarded as processes 
– Kernel operations separate from user operations, is a 
supervisor 
– No concurrent execution of user processes and kernel 
– Processes are interrupted and control is switched back 
to kernel 
– Kernel executes only during these interruptions 
• OS code executes in privileged mode , Operating 
system code situated within a reserved memory 
region, has its own system stack 
Non­process Kernel 
• Kernel operations separate from user operations 
– Only user programs are executing as processes 
– OS code executes in privileged mode, OS is a 
supervisor
 

P1  P2  Pn

Kernel 
Execution within User Process 
• User address space includes 
kernel functions 
– User address space “covers”
kernel, can call system 
functions from within process 
– Mode switch necessary to 
execute these functions 
– No context switch, as we are 
still in the same process 
Execution within User Process 
Process­based Operating Systems 
• Kernel functions run as separate processes, 
run in kernel mode 
• Concurrency within kernel, kernel processes 
scheduled together with user processes 
• Useful in multi­processor environments, as 
kernel functionality may be distributed to 
other CPU cores 
Multi­Processing 
Modern Processor Architectures: 
Multiple Processors 
• Improve performance by introducing multiple 
processors to allow true parallelism of executing 
programs 
• Symmetric Multiprocessing (SMP) 
– Two or more processors 
– Processors share the same memory and access to I/O 
devices 
– Uniform instruction set: all processors can perform the 
same functions 
– Operating system takes SMP architecture into account, 
manages processor utilisation: 
• process / task scheduling 
• Synchronisation to shared hardware resources etc. 
SMP Organisation 
SMP Advantages 
• Performance 
– More than one process can be running 
simultaneously, each on a different processor 
• Availability 
– Failure of a single process does not halt the system 
• Incremental Growth 
– Performance of a system can be enhanced by adding 
additional processors 
• Scaling 
– Systems can be scaled to requirements 
SMP Design Considerations for 
Operating System 
• A multiprocessor OS must provide all the features of a multitasking 
system as running on single processors plus has to accommodate 
the difficulties of operating with multiprocessors 
• Key issues 
– Re­entrant kernel routines: 
• The same kernel code is executed simultaneously by different processors 
– Scheduling
• Processes are scheduled on different processors 
– Synchronization 
• True parallelism of process execution and access to shared resources such as 
I/O and memory, effective synchronisation needed 
– Memory management 
• Processors share the same physical memory – this shared resource has to be 
managed carefully (page replacement algorithms) 
– Reliability and fault tolerance 
• If one processor fails, tasks should be distributed to other processors 
• “graceful degradation” of a system 
Multicore Systems 
• A processor has multiple processing cores 
– Parallelism on the processor chip itself 
– Each core has all the components of a single 
processor 
• Performance advantages 
– Multiple processors on a single chip brings huge 
performance advantages 
– Introduction of different levels of cache memory 

You might also like