Lecture No. 06
Dated: 28-04-2025
Operations on Processes
- Creation
- Deletion
Process Creation
A process
(the parent process
) may use create-process
system call to create child processes
which later can create other processes
, creating a hierarchy.
These child processes
may take resources from the operating system
or are restricted to a subset of resources given to their parent process
which avoids overloading the system.
During the execution, there are 2 possibilities.
- The
parent
continues to execute concurrently with itschildren
. - The
parent
waits for itschildren
to terminate.
In terms of address space, there are 2 possibilities.
- The
child process
is a duplicate of theparent process
. - The
child process
has a program loaded into it.
In UNIX
, each process
is identified by a unique integer (PID
).
A new process
is created with the fork
system call, duplicating the parent
's address space.
Both parent
and child
continue execution
after fork
, with the child
receiving a return code of \(0\), and the parent
receiving the child
's PID
.
Typically, execlp
is called by one process
to replace its memory with a new program
.
The parent
can create more children
or use wait
to pause until a child
terminates, after which it continues and exits.
Process Termination
- Normal Termination:
Process
callsexit()
after finishing execution.OS
deallocates all resources.- Data may be returned to the
parent
viawait()
.
- Forced Termination:
Parent
can terminatechild
(e.g., withabort()
).- Reasons: resource overuse, task no longer needed,
parent
exiting.
- Cascading Termination:
- Some systems automatically terminate
children
ifparent
exits.
- Some systems automatically terminate
- UNIX Behavior:
exit()
andwait()
manage process termination and synchronization.- Orphaned
children
are adopted by theinit
process.
The fork()
System Call
When fork()
is called, a new child process
is created with a copy of the parent
’s address space, enabling easy communication.
Both parent
and child
continue after fork()
, with the child
receiving \(0\) and the parent
receiving the child
’s PID
.
If fork()
fails, it returns \(-1\) to the parent and no child is created.
After the fork()
system call the parent
and the child
share the following
- Environment
- Open file descriptor table
- Signal handling settings
- Nice value
- Current working directory
- Root directory
- File mode creation mask (umask)
The following things are different in the parent
and the child
:
- Different process ID (
PID
) - Different parent process ID (
PPID
) Child
has its own copy ofparent
's file descriptors
The fork()
call may fail due to following reasons
- System reached maximum number of
processes
- User reached maximum number of
processes
- Not enough
swap space