Cs837: Adv. Operating Systems: Dr. Mian M.Hamayun
Cs837: Adv. Operating Systems: Dr. Mian M.Hamayun
OPERATING SYSTEMS
PROCESS CREATION, HIERARCHY & OPERATING
SYSTEM EXECUTION
Process
Process
Control
Process
Control
Process
Block
Control
Block
Control
Block Data segment
Block
Heap
Data
Kernel protected
0
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
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 nameannotations
– “e”: an array of pointers to env variables is explicitly passed to the new
process image
– “l”: commandline arguments are passed individually
– “p”: PATH env variable is used to find file name of program to be
executed
– “v”: commandline 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 */
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
inetd other processes, such
as GUI, etc,
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
– Nonprocess 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
– Processbased 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
Nonprocess 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
Nonprocess 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
Processbased Operating Systems
• Kernel functions run as separate processes,
run in kernel mode
• Concurrency within kernel, kernel processes
scheduled together with user processes
• Useful in multiprocessor environments, as
kernel functionality may be distributed to
other CPU cores
MultiProcessing
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
– Reentrant 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