Unit 2: Process Concept
Prof. Mrs. Trupti S. IndiWalchand Institute of TechnologyDepartment of Information TechnologySolapur University
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
2
Processes
• Process Concept• Process Scheduling• Operations on Processes• Interprocess Communication (IPC)• Threads
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
3
Process Concept
• An OS executes a variety of programs:
- Batch system: jobs
- Time- sharing systems: user programs or tasks
• Process is a program in execution
• A process is the unit of work in a modern time-sharing system
• A process is more than program code, it also includes current
activity
• A process includes: PC, stack and data section
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
4
Process In Memory
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
5
Process Concept
• A process includes: PC, stack and data section• The process stack contains:– Temporary data such as method parameters
return addresses and local variables• A data section contains global variables
• “A program by itself is not a process”– Program is a passive entity such as file on disk– Process is an active entity, with PC specifying _?__
and set of associated resources
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
6
Process State
As a process executes, it changes state1) new: The process is being created2) running: Instructions are being executed3) waiting: The process is waiting for some
event to occur4) ready: The process is waiting to be assigned
to a processor5) terminated: The process has finished
execution
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
7
Diagram of Process States
1. Admitted2. Interrupted3. Scheduler Dispatch4. I/O or event wait5. I/O or event completion6. Exit
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
8
Diagram of Process States
• Only one process can be running on any processor at any instant.
• Many processes many be ready and waiting.
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
9
Process Control Block (PCB)
Each process is represented by PCB also called task control block
i. Process stateii. Program counteriii. CPU registersiv. CPU scheduling informationv. Memory-management informationvi. Accounting informationvii. I/O status information
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
10
Process Control Block (PCB)PCB contains information associated with a specific process given as:
Process state: The state may be new, ready, running, waiting, and terminated.
Program Counter: The counter indicates the address of the next instruction to be executed for this process.
CPU registers: The registers vary in number and type, depending on the computer architecture.They include accumulators, index registers, stack pointers and GPRs, condition code info.
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
11
QUIZ1) Process is ____________________________2) Program is ______ entity and process is ________ entity.
(active , passive)3) Process states are ___, _____, _____, _____, and ______4) Process includes ____, _______ and ______5) In which state process is, if “Process is waiting to be
assigned to a processor”6) PCB is stands for __________________ and it is also called
as ____________________7) When I/O or event completion occurs then process
changes its state from ______ to ______8) How many process can be in running at any instant on
single processor ?
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
12
CPU Switch From Process to Process
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
13
• When current executing process P0 interrupted, the OS saves the sate into PCB0 of P0 and load the PCB1 of process P1 and P0 process will be idle and P1 will be executing.
• After P1 execution is completed, the OS will save state into PCB1 and reload state from PCB0 and P1 process will be idle whereas P0 will be executing.
CPU Switch From Process to Process
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
14
• When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process
• Context-switch time is overhead; the system does no useful work while switching
• Time dependent on hardware support
Context Switch
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
15
• Multiprogramming maximizes ___________ ?
• Time-sharing switches CPU among _________ and improves user _____________
• For uni-processor system if one process is running the rest one are waiting until the CPU is free and can be _____________
Process Scheduling
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
16
We define process scheduling as:• The act of determining which process in the
ready state should be moved to the running state.
• That is, decide which process should run on the CPU next.
Process Scheduling
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
17
Process Scheduling Queue
• Job queue – set of all processes in the system
• Ready queue – set of all processes residing in main memory, ready and waiting to execute
• Device queues – set of processes waiting for an I/O device
• Processes migrate among the various queues
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
18
Ready Queue and Various I/O Device Queues
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
19
Representation of Process Scheduling
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
20
The goal of the scheduler:• Implement the virtual machine in such a
way the user perceives that each process is running on it’s own computer.
Schedulers
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
21
• Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue
• Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU
Schedulers
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
22
• Short-term scheduler is invoked very frequently (milliseconds) (must be fast)
• Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow)
• The long-term scheduler controls the degree of multiprogramming
• Processes can be described as either:– I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
– CPU-bound process – spends more time doing computations; few very long CPU bursts
Schedulers
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
23
I/O Bound Process:• Processes that are mostly waiting for the completion of
input or output (I/O) are I/O Bound.
• Interactive processes, such as office applications are mostly I/O bound the entire life of the process. Some processes may be I/O bound for only a few short periods of time.
• The expected short run time of I/O bound processes means that they will not stay the running the process for very long.
• They should be given high priority by the scheduler.
Schedulers
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
24
SchedulersCPU Bound Process:• CPU Bound processes are ones that are implementing algorithms
with a large number of calculations.
• They can be expected to hold the CPU for as long as the scheduler will allow.
• Programs such as simulations may be CPU bound for most of the life of the process.
• Users do not typically expect an immediate response from the computer when running CPU bound programs.
• They should be given a lower priority by the scheduler.
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
25
• Parent process create children processes, which, in turn create other processes, forming a tree of processes
• Resource sharing– Parent and children share all resources– Children share subset of parent’s resources– Parent and child share no resources
• Execution– Parent and children execute concurrently– Parent waits until children terminate
Process Creation
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
26
• Address space– Child duplicate of parent– Child has a program loaded into it
• UNIX examples– fork system call creates new process– exec system call used after a fork to replace the
process’ memory space with a new program
Process Creation
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
27
Process Creation
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
28
#include<unistd.h>#include<sys/types.h>#include<stdio.h>#include<stdlib.h>
int main(){
pid_t pid;/* fork another process */
pid = fork();if (pid < 0) { /* error
occurred */fprintf(stderr, "Fork
Failed");exit(-1);
}
C Program Forking Separate Processelse 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");
exit(0);}
}
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
29
Output
[tsindi@localhost ~]$ vi NewProc.c[tsindi@localhost ~]$ cc NewProc.c[tsindi@localhost ~]$ ./a.outa.out F1.cpp f2Exe GetInfo.cpp NonRecursive.cass1.c f1Output fcfsPgm.cpp getInfoExe PolyLL.cBinaryTree.c F2.cpp forkPgm.c NewProc.cChild Complete[tsindi@localhost ~]$ vi NewProc.c
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
30
Linux/Unix Command exec:DESCRIPTION• This command treats its arguments as the
specification of one or more subprocesses to execute. The arguments take the form of a standard shell pipeline where each arg becomes one word of a command, and each distinct command becomes a subprocess.
• If the initial arguments to exec start with - then they are treated as command-line switches and are not part of the pipeline specification.
exec Family of Functions
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
31
A tree of processes on a typical Solaris
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
32
Process Termination• Process executes last statement and asks the
operating system to delete it (exit)– Output data from child to parent (via wait)– Process’ resources are deallocated by operating system
• Parent may terminate execution of children processes (abort)– Child has exceeded allocated resources– Task assigned to child is no longer required– If parent is exiting• Some operating system do not allow child to continue if its
parent terminates– All children terminated - cascading termination
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
33
Study
• fork()• wait()• exec() family of functions execl(), execlp(),
execle(), execv(), execvp(), execvpe()• sleep()• exit()• abort()
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
34
• Independent process cannot affect or be affected by the execution of another process
• Cooperating process can affect or be affected by the execution of another process
• Advantages of process cooperation– Information sharing – Computation speed-up– Modularity– Convenience
Cooperating Processes
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
35
• Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process– unbounded-buffer places no practical limit on the
size of the buffer– bounded-buffer assumes that there is a fixed
buffer size
Producer-Consumer Problem
Prof. T. S. Indi, Walchand Institute of Technology, Solapur University
36
Continue…
Top Related