100% found this document useful (1 vote)
1K views

Process Creation and Termination in OS

The document discusses process creation and management in operating systems. It describes how processes are created using system calls like fork() and exec() and managed using process identifiers and process control blocks. A process tree is formed as processes create child processes. Important process states and scheduling queues like the ready queue and I/O queues are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

Process Creation and Termination in OS

The document discusses process creation and management in operating systems. It describes how processes are created using system calls like fork() and exec() and managed using process identifiers and process control blocks. A process tree is formed as processes create child processes. Important process states and scheduling queues like the ready queue and I/O queues are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

OPERATING SYSTEM CIA 1B

PROCESS CREATION

LEARNING OUTCOME: Understand the implementation of process management


system calls like fork (), exec (), etc.

A process is a program in execution. Process creation is a situation where a process creates


another process or processes. Here, the creating process is called the parent process, while the
created processes are called the child process. Each of these created processes might intend
create other processes, thereby creating a process tree (which can be seen in Linux using the
pstree command). New processes in Linux can be created using the fork () system call.

In most operating systems, processes recognized using unique integer values assigned to them by
the operating system. These unique values are called process identifiers or PID (these ids can be
seen in Linux using the top command). This unique identifier (PID) can be used by the operation
system, as an index to access various process attributes within the kernel.

Process attributes are used by the operation system to create a data structure called the Process
Control Block (PCB), which holds some essential information about each created process. These
attributes include the PID, program counter, and process state, priority of the process, opened
files and devices in use.

The init takes PID 1 and can be viewed as the direct or indirect ancestor of all processes. The init
process is started by the kernel during booting and runs until shutdown. After booting, the init
process can also create other processes such as web or print server, an ssh server etc. below is a
diagram showing the creation of processes (a process tree).
From the above diagram, we can see that the init process is the main processes, that is, all other
processes such as the login, kthread and the sshd spawned from it. So in this case, the init is the
parent process of the login, kthread and the sshd processes(and they are the child processes or
children of init). Also, we can observe that each of these child processes have created their own
processes, thereby making parent processes their created processes, hence forming a tree like
structure of processes called a process tree.

Normally, when a child process is created, it may require some resources such as CPU time,
memory, files etc. to accomplish its task. These resources may be arquired from the processor or
the child process may be constrained to a subset of the parent process resources.

When a parent process creates a child process, they are two expectations of execution;

1. Both processes (parent and child) execute concurrently.


2. The parent process waits for its child completion of execution before it continues.

Also, there are two address possibilities;

1. The child process runs a copy of the parent process, that is, they share any data.
2. The child process has a new process loaded into it.
In UNIX, when a new process is created, it consist of a copy of the address space of the
parent process. This mechanism allows both the parent and child processes to
communicate with each other.
Process management system calls:
Process management is implemented in UNIX systems using a number of system calls.
These system calls can be combined to form even more complex processes such as
zombie and orphan processes. Some important process management system calls include
fork(), exec(), exit() and the wait() system calls.

1. FORK() SYSTEM CALL:

This system call as earlier said in paragraph one is used to create new processes in
Linux. After a fork() call, the child and parent processes execute the same program
code and share any data, but in separate processes. Below is a diagram that shows a
parent process execution a fork() system call.
Return value:

On success, the fork() system call returns two values zero and a no-zero integer
value(the PID of the child process). The zero is returned to the child process, while
the child process PID is returned to the parent process.

On failure, the fork() system call returns a -1 to the parent and no child process is
created, henceforth, an errno (error number) is set to indicate this error. The return
value of fork() can be used by a program to know whether to execute in the parent or
child process.

EXEC() SYSTEM CALL:

This command is use to replace a process’s memory with a new program. After the
fork() system call, one of the processes typically calls the exec() system call which
replaces the process’s program memory space with a new program. On the execution
of the exec() system call, it loads a binary file memory, destroying the image of the
program containing it and starts its execution.

In this manner, the two processes can execute concurrently, while going separate
ways. The parent can now create more children, else if it has no jobs to accomplish, it
will initiate the wait() system call, to remove itself from the ready queue until the
termination of the child process.

WAIT() SYSTEM CALL:


This system call is used by the parent process, to remove itself from the ready queue
until the child process terminates. The parent process uses this system call when it has
no jobs to complete. If a wait system call is initiated by a process without a child
process, it immediately terminates without blocking the process.

A parent process can use the wait() system call to get the exit status of its child
process. Wait(int *status) can be use to get the exit status of a child process. This will
be store in the address where status is pointing to.
On failure, wait returns -1 and on success, wait returns a zero. The WIFEXIT(status)
and WEXITSTAUTUS(status) can be use to get the exit status of a child process. It
returns true on success, that is, if the child process terminated normally (that is using
the exit() system call).

The c program below summarizes the implementation of the above system calls.

From the above image,

Line 10 declares a variable used to hold the return value of fork() using the PID data type
“pid_t”.

In line 13 the fork() system call is invoked and its return value stored in pid.

In line 16, if the child process could not be created, then pid will be -1 and hence a standard
error will be indicated.

In line two line 21, if the child process was created, then fork() will have to return a zero,
hence the child process executes the exec() command in line 27 which list the process status
of a linux system.
Because the child process is execution, the parent process waits for its termination by
executing the wait() system call in line 33.

The diagram below shows the output of the above program.

The above system calls can be used to implement processes like the orphan and zombie
processes;

Orphan process:
This is a situation when a parent process terminates before the child process. That
is, the child process is sleeping and when it gets up, the parent had already terminated.
When this happens, the init process becomes the parent process to the orphan process.

Zombie process:
This is a situation when a child process terminates without informing the parent process.
In this case, the parent process is sleeping and when it gets up, the child process had
already terminated. Here, given that the parent process was not informed about the
termination of the child process, the resources that were allocated will be wasted. The C
program below implements a zombie process.
PROCESS SCHEDULING

Introduction:

This is an activity performed by a process manage where, processes are assigned to resources
this can complete the job. The main aim of multiprogramming is to have multiple processes
running at all times to maximize CPU utilization. The objective of time sharing systems is to
switch between processes so frequently those users can interact with each process while
running. In order for this to be possible, a scheduler is needed to select and load available
processes into the CPU. A single processor system never runs more than one process. In case
there is more than one process that process will have to finish its execution before CPU gets
assigned to another process.

Scheduling Queues:

This refers to queues of processes or devices. There are three types of queues namely; job
queue, ready queue and device queue.

Whenever a process enters a system, it is kept in the job queue. This queue contains all
processes in the system.

Processes residing in the main memory and waiting for execution are stored in the ready
queue. This queue is stored in the form of a linked list.

Since, there are many processes running in a system, when a certain process makes an I/O
request, it might have to wait for a process which is currently using that I/O device to finish.
These processes are stored in the device queue. So, the device queue is a list of processes
waiting for an I/O device. Every device has a device queue. Below is a queuing-diagram
representation of process scheduling.
From the above diagram, each box represents a queue. There are also two types of queues
present above that is, the ready and device queues. The circles represent resources that serve
the queues and the arrows represent the flow if processes. A process enters the system, it is
stored in the ready queue. It waits until it is selected and allocated some cpu time. Once in
the CPU and is execution, the following can occur.

1. The process can issue an I/O request. As a result of this request, it will be placed in an
I/O queue.
2. The process can execute the fork() system call to create a child process and will have to
wait for the termination of the child process.
3. The process can be removed forcefully from the CPU because of an interrupt and put
back in the ready queue.
In the case 1 and case 2 above, the process first switches to the waiting state
before going to the ready state. A process will continue in this cycle until it
terminates and its PCB gets de-allocated from it.
PROCESS SCHEDULERS

In a process’ life time, it switches between multiple queues and states. The operation
system must select for scheduling purposes a process in a certain way or system or routine. This
selection is carried out by schedulers. There are two main process schedulers (long-term and
short-term schedulers) and an additional scheduler (medium term scheduler).

Long-term schedulers or job schedulers are responsible for selecting processes from the storage
pool in the secondary storage and loading them into the ready queue in the main memory for
execution.

The long-term scheduler must select carefully a mixture if both I/O bound and CPU bound
processes in order to yield optimum throughput (the amount of processes that can be executed in
a system).

Short-term schedulers on the other hand are responsible for selection processes from the ready
queue for execution. This scheduler may use multiple scheduling algorithms to determine the
next process to be executed.

The short-term scheduler executes frequently than a long-term scheduler. This is because a
process might execute only for few milliseconds.

The diagram below shows the movement of the two schedulers.


An additional scheduler known as the medium-term scheduler is responsible for swapping out
processes form the main memory to the secondary memory. The swapped out process can be
executed later on from where it stopped. A process may be swapped from the main memory
when it is suspended, that is when it makes an I/O request and hence cannot progress towards
completion. The process is swapped to clear memory for other processes. This swapping helps in
reducing the degree of multiprogramming and improve mix I/O and CPU bound processes in the
memory. The diagram below demonstrates medium-term scheduling.

PROCESS SCHEDULING POLICIES

These policies help schedulers to determine what process to select and when to execute a
process.

1. Fairness
This I a policy which helps the sytem to accomplish some important goal like minimizing
CPU utilization and maximize throughput, by not giving unfair advantages to programs
which might interfere with CPU utilization, wait time, throughput, etc.
2. First Come First Served (FCFS)
This is also known as FIFO (first-in-first-out) and processes jobs in the order in which
they are received. This policy is not fair because if a long job is running, then other
processes have to wait for its completion. This could lock like a system freeze to the end
user.
3. Round Robin policy
In this policy, a timer is used to determine when to move a current process to the waiting
queue for others to execute. For example, if the time is set 100ms and a process is
currently running, after 100ms it will have to stop its execution and go to the back of the
line for the next process to execute.
Also, if the timer is too long, then processes will still have to wait until the current
process finishes its execution, which takes us back to FIFO. However, if the time is too
short the system spends time context switching thus, throughput suffers.
4. Shortest Job First (SJF)
This policy enables the schedulers to search through the time needed by each process to
complete its execution and then, it executes the process with the shortest time need for its
completion, in this scenario, I/O jobs take the priority.
The drawback of this policy is that it is difficult to predict then time left for a CPU job to
complete its execution. This might also end up starving longer-running processed of
resources hence, system freeze,
5. Priority Based Scheduling
Here, processes are assigned priorities. The process with the highest priority gets
executed first. Processes with the same priority use FIFO. The priority can be decided on
the bases of memory, time and other resource requirements.
6. Shortest Remaining Time (SRT)
In this case, the processor is assigned to a process having the shortest time left for its
completion. This process can be preempted by a newer process with a shorter time to its
completion. It is often implemented in batch systems where short jobs need preference.
7. Multiple-Level Queues Scheduling
This is not an independent policy. That is, it uses other policies to group and schedule
processes with common characteristics
- Multiple queues are maintained for processes with common characteristics.
- Each queue can have its own scheduling policy.
- Priorities are assigned to each queue.
For instance, I/O bound processes can be stored in one queue and CPU-bound processes
in another queue, then, based on the algorithms of each queue, the scheduler alternately
selects jobs from each queue and assigns them to the CPU.

You might also like