27
Process Concept T S PRADEEP KUMAR

Lecture 5 process concept

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Lecture 5   process concept

Process ConceptT S PRADEEP KUMAR

Page 2: Lecture 5   process concept

Process Introduction Process States Process Control Block (PCB or TCB) Process creation and I/O Requests Schedulers

Topics

Page 3: Lecture 5   process concept

A program in execution Process in memory has

◦ Text section (includes program code)◦ Data section (global variables)◦ Stack (contains the return address, local address,

function parameters)◦ Heap (dynamic runtime memory during the

execution of process and this may vary)

Process - Introduction

Page 4: Lecture 5   process concept

stack

heap

data

text

Process in memory

Page 5: Lecture 5   process concept

Program Vs Process

Page 6: Lecture 5   process concept

Program is a passive entity Program stores sequence of instructions written

in a file stored in the secondary memory Process is active entity which has program

counter that tells the next instruction to be executed and also has a set of associated resources.

Program becomes a process when the file is loaded in the primary memory◦ Example: double click the executable file of

running a.out is example of program -> process

Program Vs Process

Page 7: Lecture 5   process concept

Process States

Page 8: Lecture 5   process concept

New – a process is generated Ready – multiple processes are ready to run

under a CPU. Running – currently executing on the CPU Waiting – Waiting for an Event or an IO,

Once the event is available, process moves to the ready state.

Terminated – Process is killed or terminated

Process States

Page 9: Lecture 5   process concept

Process Control Block

Page 10: Lecture 5   process concept

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.

Process Control Block

Page 11: Lecture 5   process concept

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., disk 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.

Process Control Block

Page 12: Lecture 5   process concept

pid_t pid Process identifier

Long state State of a process

Unsigned int time_slice Scheduling information

Struct task_struct *parent This process’s parent

Struct list_head child; This process’s children

Struct files_struct *files List of open files

Struct mm_struct *mm Addresss space of this process

Process in Linux

Page 13: Lecture 5   process concept

Processes running

Page 14: Lecture 5   process concept

PCB and I/O Requests

Page 15: Lecture 5   process concept

Process and I/O Requests

Page 16: Lecture 5   process concept

int main()

{

pid_t pid;

pid =fork();

if (pid < 0) { I* error occurred *I

fprintf(stderr, "Fork Failed");

return 1;

}

else if (pid == 0) { I* child process *I

execlp("lbinlls","ls",NULL);

}

else { I* parent process *I

}

wait (NULL) ;

printf("Child Complete");

return 0;}

Create a Child Process

Page 17: Lecture 5   process concept

A process migrates among various scheduling queues throughout its lifetime

Long Term Scheduler or Job Scheduler◦ in a batch system, more processes are

submitted than can be executed immediately.◦execute less frequently

Short Term Scheduler or CPU Scheduler◦ selects from among the processes that are

ready to execute and allocates the CPU to one of them.

◦ Runs most frequently (ex: runs every 100ms)

Scheduler

Page 18: Lecture 5   process concept

Processes are either I/O Bound or CPU Bound

Long Term schedulers should have the correct mix of I/O Process and CPU Bound processes

Short Term Scheduler schedules the Processes that are ready to run under the CPU

So Unix and Microsoft does not have Long Term Schedulers as it more time to get executed

Scheduler

Page 19: Lecture 5   process concept

When an interrupt occurs, the state of the process to be saved which called as context switching

It depends highly on the hardware support Hardware architecture should have more

number of registers as compared to more number of context switches.

Context Switching

Page 20: Lecture 5   process concept

Reasons for IPC◦ Information Sharing

Sharing file between processes◦ Computational Speedup

Processes are break down in to sub-process◦ Modularity◦ Convenience

Individual user can work on many tasks at the same time

Interprocess Communication (IPC)

Page 21: Lecture 5   process concept

Independent Processes◦ No interference with other processes in the

system Cooperative processes

◦ Shared Memory◦ Message Passing

Interprocess Communication (IPC)

Page 22: Lecture 5   process concept

IPC

Page 23: Lecture 5   process concept

Message Passing ◦ Suitable for smaller amount of data◦ Easier to implement◦ Usually implemented using System calls, so kernel is

bothered every-time during message communication Shared Memory

◦ Faster to implement◦ system calls are required only to establish shared-

memory regions.◦ Once shared memory is established, all accesses are

treated as routine memory accesses, and no assistance from the kernel is required.

IPC

Page 24: Lecture 5   process concept

Use of Producer – Consumer relations Producer will produce and consumer will

consume the items produced by the producer P-C problem achieved through

◦ Unbounded buffer No limit on the size of queue The consumer may have to wait for new items but

the producer can always produce new items. ◦ Bounded buffer

Implement like a circular Queue Producer will wait if the queue is full Consume will waif if the queue is empty

Shared Memory Systems

Page 25: Lecture 5   process concept

#define BUFFER_SIZE 10typedef struct {…}item;item buffer[BUFFER_SIZE];int in = 0;int out = 0;/*Buffer is empty when in==outBuffer is full when ((in+1)%BUFFER_SIZE)==out*/

Shared Memory

Page 26: Lecture 5   process concept

while (true) {/* produce an item in nextProduced *Iwhile ( ((in + 1) % BUFFER_SIZE) == out)I* do nothing *Ibuffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;}

Producer process

Page 27: Lecture 5   process concept

item nextConsumed;while (true) {while (in == out); II do nothingnextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;I* consume the item in nextConsumed *I}

Consumer Process