Chapter2 Process
Chapter2 Process
Lecture notes:
Prep. By: Mr. Daya Ram Budhathoki
Chapter2:
Introduction to process:
A process is an instance of a program in execution. A program by itself is not a process; a program is a passive entity, such
as a file containing a list of instructions stored on disks. (often called an executable file), whereas a process is an active
entity, with a program counter specifying the next instruction to execute and a set of associated resources. A program
becomes a process when an executable file is loaded into memory.
Program: A set of instructions a computer can interpret and execute.
Process:
– Dynamic
– Part of a program in execution
– a live entity, it can be created, executed and terminated.
– It goes through different states
wait
running
Ready etc
– Requires resources to be allocated by the OS
– one or more processes may be executing the same code.
Program:
– static
– no states
This example illustrate the difference between a process and a program:
main ()
{
int i , prod =1;
for (i=0;i<100;i++)
prod = pord*i;
}
It is a program containing one multiplication statement (prod = prod * i) but the process will execute 100 multiplication, one
at a time through the 'for' loop.
Although two processes may be associated with the same program, they are nevertheless considered two separate execution
sequences. For instance several users may be running different copies of mail program, or the same user may invoke many
copies of web browser program. Each of these is a separate process, and although the text sections are equivalent, the data,
heap and stack section may vary.
1
The process Model?
Fig:1.1: (a) Multiprogramming of four programs. (b) Conceptual model of
four independent, sequential processes. (c) Only one program is active at
any instant.
A process is just an executing program, including the current values of the program counter, registers, and variables.
Conceptually, each process has its own virtual CPU. In reality, of course, the real CPU switches back and forth from process
to process, but to understand the system, it is much easier to think about a collection of processes running in (pseudo)
parallel, than to try to keep track of how the CPU switches from program to program. This rapid switching back and forth is
called multiprogramming
Process Creation:
There are four principal events that cause processes to be created:
1. System initialization.
2. Execution of a process creation system call by a running process.
3. A user request to create a new process.
4. Initiation of a batch job.
Parent process create children processes, which, in turn create other processes, forming a tree of processes . Generally,
process identified and managed via a process identifier (pid)
When an operating system is booted, often several processes are created.
Some of these are foreground processes, that is, processes that interact with (human) users and perform work for them.
Others are background processes, which are not associated with particular users, but instead have some specific function.
For example, a background process may be designed to accept incoming requests for web pages hosted on that machine,
waking up when a request arrives to service the request. Processes that stay in the background to handle some activity such
as web pages, printing, and so on are called daemons
In addition to the processes created at boot time, new processes can be created afterward as well. Often a running process
will issue system calls to create one or more new processes to help it do its job.
In interactive systems, users can start a program by typing a command or double clicking an icon.
In UNIX there is only one system call to create a new process: fork. This call creates an exact clone of the calling process.
After the fork, the two processes, the parent and the child, have the same memory image, the same environment strings, and
the same open files. That is all there is. Usually, the child process then executes execve or a similar system call to change its
memory image and run a new program. For example, when a user types a command, say, sort, to the shell, the shell forks off
a child process and the child executes sort.
2
The C program shown below describes the system call, fork and exec used in UNIX. We now have two different process
running a copy of the same program. The value of the PID for the child process is zero; that for the parent is an integer value
greater than zero. The child process overlays its address space with the UNIX command /bin/ls using the execlp() system
call. (execlp is version of the exec). The parent waits for the child process to complete with the wait() system call. When the
child process completes (by either implicitly or explicitly invoking the exit() ) the parent process resumes from the call to
wait, where it completes using the exit() system call. This is also illustrated in the fig. below.
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
printf("%d", pid);
exit(1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete\n");
printf("%d",pid);
exit(0);
}
}
The last situation in which processes are created applies only to the batch systems found on large mainframes. Here users
can submit batch jobs to the system (possibly remotely). When the operating system decides that it has the resources to run
another job, it creates a new process and runs the next job from the input queue in it.
3
UNIX System Call:
fork system call creates new process
exec system call used after a fork to replace the process’ memory space with a new
program
Process Control Block:
In operating system each process is represented by a process control block(PCB) or a
task control block. Its a data structure that physically represent a process in the
memory of a computer system. It contains many pieces of information associated with
a specific process that includes the following.
• Identifier: A unique identifier associated with this process, to distinguish it
from all other processes.
• State: If the process is currently executing, it is in the running state.
• Priority: Priority level relative to other processes.
• Program counter: The address of the next instruction in the program to be
executed.
• Memory pointers: Includes pointers to the program code and data associated
with this process, plus any memory blocks shared with other processes.
• Context data: These are data that are present in registers in the processor
while the process is executing.
• I/O status information: Includes outstanding I/O requests, I/O devices (e.g., tape
drives) assigned to this process, a list of files in use by the process, and so on.
• Accounting information: May include the amount of processor time and clock
time used, time limits, account numbers, and so on.
Fig1.2: Process Control
Block
Process Termination:
After a process has been created, it starts running and does whatever its job is: After some time it will terminate due to one
of the following conditions.
1. Normal exit (voluntary).
2. Error exit (voluntary).
3. Fatal error (involuntary).
4. Killed by another process (involuntary).
4
Process States:
Each process may be in one of the following states:
Fig1.3: Process state Transition diagram
● New: The process is being created.
● Running:Instructions are being executed.
● Waiting: The process is waiting for some event to occur ( such as I/O completion or reception of a signal)
● Ready:The process is waiting to be assigned to a processor.
● Terminated: The process has finished execution.
Implementation of Process:
Operating system maintains a table (an array of structure) known as process table with one entry per process to implement
the process. The entry contains detail about the process such as, process state, program counter, stack pointer, memory
allocation, the status of its open files, its accounting information, scheduling information and everything else about the
process that must be saved when the process is switched from running to ready or blocked state, so that it can be restarted
later as if it had never been stopped.
5
parent process
process group
Signals
Time when process started
CPU time used
Children's CPU time
Time of next alarm
Each I/O device class is associated with a location (often near the bottom of the memory) called the Interrupt Vector. It
contains the address of interrupt service procedure. Suppose that user process 3 is running when a disk interrupt occurs.
User process 3's program counter, program status word and possibly one or more registers are pushed onto the (current)
stack by the interrupt hardware. The computer then jumps to the address specified in the disk interrupt vector. That is all the
hardware does. From here on, it is up to the software in particular the interrupt service procedure.
Interrupt handling and scheduling are summarized below.
1. Hardware stack program counter etc.
2. Hardware loads new program counter from interrupt vector
3. Assembly languages procedures save registers.
4. Assembly language procedures sets up new stack.
5. C interrupt service runs typically reads and buffer input.
6. Scheduler decides which process is to run next.
7. C procedures returns to the assembly code.
8. Assembly language procedures starts up new current process.
Fig:The above points lists the Skeleton of what the lowest level of the operating system does when an interrupt
occurs.
Context Switching:
Switching the CPU to another
process requires performing a state
save of the current process and a
state restore restore of a different
process. This task is known as
context switch. When a context
switch occurs, the kernel saves the
the context of the old process in its
PCB and loads the saved context of
the new process scheduled to run.
Context switch time is pure
overhead, because the system does
no useful work while switching. Its
speed varies from machine to
machine, depending on the
memory speed, the number of
registers that must be copied etc.
Fig: CPU switch from one process to another
6
Threads:
A thread is a basic unit of CPU utilization, it comprises a thread ID, a program counter, a register set, and a stack. It shares
with other threads belonging to the same process its code section, data section, and other operating system resources, such
as open files and signals. A traditional ( or heavy weight) process has a single thread of control. If a process has multiple
thread of control, it can perform more than one task at a time. Fig below illustrate the difference between single threaded
process and a multithreaded process.
Thread simply enable us to split up a program into logically separate pieces and have the pieces run independently of one
another until they need to communicate. In a sense threads are a further level of object orientation for multitasking system.
Multithreading:Many software package that run on modern desktop pcs are multithreaded. An application is implemented
as a separate process with several threads of control. A web browser might have one thread to display images or text while
other thread retrieves data from the network. A wordprocessor may have a thread for displaying graphics, another thread for
reading the character entered by user through the keyboard, and a third thread for performing spelling and grammar
checking in the background.
Why Multithreading:
In certain situations, a single application may be required to perform several similar task such as a web server accepts client
requests for web pages, images, sound, graphics etc. A busy web server may have several clients concurrently accessing it.
So if the web server runs on traditional single threaded process, it would be able to service only one client at a time. The
amount of time that the client might have to wait for its request to be serviced is enormous.
One solution of this problem can be thought by creation of new process. When the server receives a new request, it creates a
separate process to service that request. But this method is heavy weight. In fact this process creation method was common
before threads become popular. Process creation is time consuming and resource intensive. If the new process perform the
same task as the existing process, why incur all that overhead? It is generally more efficient for one process that contains
multiple threads to serve the same purpose. This approach would multithreaded the web server process. The server would
cerate a separate thread that would listen for clients requests. When a request is made, rather than creating another process,
it will create a separate thread to service the request.
7
Benefits of Multithreading:
Responsiveness: Mutlithreaded interactive application continues to run even if part of it is blocked or performing a lengthy
operation, thereby increasing the responsiveness to the user.
Resource Sharing: By default, threads share the memory and the resources of the process to which they belong. It allows
an application to have several different threads of activity withing the same address space.
Economy:Allocating memory and resources for process creation is costly. Since thread shares the resources of the process
to which they belong, it is more economical to create and context switch threads. It is more time consuming to create and
manage process than threads.
Utilization of multiprocessor architecture: The benefits of multi threading can be greatly increased in multiprocessor
architecture, where threads may be running in parallel on different processors. Mutlithreading on a multiCPU increases
concurrency.
Process VS Thread:
Process Thread
Heavy weight Light weight
Unit of Allocation Unit of Execution
– Resources, privileges etc – PC, SP, registers
PC—Program counter, SP—Stack pointer
Interprocess communication is expensive: need to Interthread communication cheap: can use process
context switch memory and may not need to context switch
Secure: one process cannot corrupt another process Not secure: a thread can write the memory used by
another thread
Process are Typically independent Thread exist as subsets of a process
Process carry considerable state information. Multiple thread within a process share state as well as
memory and other resources.
8
Processes have separate address space Thread share their address space
processes interact only through systemprovided inter Context switching between threads in the same process is
process communication mechanisms. typically faster than context switching between processes.
Threads Model:
Fig: One to one Threading model
Fig: Many to one Fig:Many to Many