36
Unit 2: Process Concept Prof. Mrs. Trupti S. Indi Walchand Institute of Technology Department of Information Technology Solapur University

Principle Of Operating System Chapter 2

Embed Size (px)

DESCRIPTION

Principle Of Operating System ,Process Creation,information,switching etc

Citation preview

Page 1: Principle Of Operating System Chapter 2

Unit 2: Process Concept

Prof. Mrs. Trupti S. IndiWalchand Institute of TechnologyDepartment of Information TechnologySolapur University

Page 2: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

2

Processes

• Process Concept• Process Scheduling• Operations on Processes• Interprocess Communication (IPC)• Threads

Page 3: Principle Of Operating System Chapter 2

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

Page 4: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

4

Process In Memory

Page 5: Principle Of Operating System Chapter 2

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

Page 6: Principle Of Operating System Chapter 2

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

Page 7: Principle Of Operating System Chapter 2

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

Page 8: Principle Of Operating System Chapter 2

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.

Page 9: Principle Of Operating System Chapter 2

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

Page 10: Principle Of Operating System Chapter 2

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.

Page 11: Principle Of Operating System Chapter 2

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 ?

Page 12: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

12

CPU Switch From Process to Process

Page 13: Principle Of Operating System Chapter 2

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

Page 14: Principle Of Operating System Chapter 2

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

Page 15: Principle Of Operating System Chapter 2

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

Page 16: Principle Of Operating System Chapter 2

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

Page 17: Principle Of Operating System Chapter 2

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

Page 18: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

18

Ready Queue and Various I/O Device Queues

Page 19: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

19

Representation of Process Scheduling

Page 20: Principle Of Operating System Chapter 2

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

Page 21: Principle Of Operating System Chapter 2

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

Page 22: Principle Of Operating System Chapter 2

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

Page 23: Principle Of Operating System Chapter 2

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

Page 24: Principle Of Operating System Chapter 2

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.

Page 25: Principle Of Operating System Chapter 2

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

Page 26: Principle Of Operating System Chapter 2

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

Page 27: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

27

Process Creation

Page 28: Principle Of Operating System Chapter 2

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);}

}

Page 29: Principle Of Operating System Chapter 2

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

Page 30: Principle Of Operating System Chapter 2

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

Page 31: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

31

A tree of processes on a typical Solaris

Page 32: Principle Of Operating System Chapter 2

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

Page 33: Principle Of Operating System Chapter 2

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()

Page 34: Principle Of Operating System Chapter 2

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

Page 35: Principle Of Operating System Chapter 2

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

Page 36: Principle Of Operating System Chapter 2

Prof. T. S. Indi, Walchand Institute of Technology, Solapur University

36

Continue…