Upload
pradeep-kumar-ts
View
1.413
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
Process ConceptT S PRADEEP KUMAR
Process Introduction Process States Process Control Block (PCB or TCB) Process creation and I/O Requests Schedulers
Topics
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
stack
heap
data
text
Process in memory
Program Vs Process
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
Process States
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
Process Control Block
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
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
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
Processes running
PCB and I/O Requests
Process and I/O Requests
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
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
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
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
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)
Independent Processes◦ No interference with other processes in the
system Cooperative processes
◦ Shared Memory◦ Message Passing
Interprocess Communication (IPC)
IPC
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
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
#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
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
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