Sunteți pe pagina 1din 18

1

Process Management
The Process concept An operating system executes a variety of programs: Batch system jobs Time-shared systems user programs or tasks Program is a passive entity. A process is a program in execution. An active entity that can be assigned to and executed on a processor. A process is unit of work. It has limited time span. A process needs certain resources, including CPU time, memory, files, and I/O devices, to accomplish its task. The Process A program is a passive entity i.e. a file containing a list of instructions stored on disk called as executable file. 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 process when an executable file is loaded to memory .Two common techniques for loading executable files are: Double click on icon representing executable file Entering the name of the executable file on the command line(ex prog.exe or a.out) A process includes: Text section (Program code) Value of program counter and contents of processors registers (PCB) Stack (temporary data like function parameters, return addresses, local variables) Data section (global variables)

2 Heap (memory dynamically allocated to process to hold intermediate computation data at run time)

Process in memory Two processes may be associated with same program but they are not considered as separate processes e.g. many copies of one mail program.

A process may spawn many processes as it runs


Process State As the process executes ,it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states: new: The process is being created. ready: The process is waiting to be assigned to a processor. running: Instructions are being executed. waiting: The process is waiting for some event to occur (I/O or reception of signal) terminated: The process has finished execution. Only one process can be running on any processor at any instant.

Diagram of process state Process Control Block (PCB) Each process is represented by a process control block (PCB) also called as task control block. A process control block or PCB is a data structure (a table) that holds information about a process

Process Control Block(PCB) PCB is repository of information Information associated with each process: Process state Program counter CPU registers CPU scheduling information Memory-management information

4 Accounting information I/O status information 1. Process state The state may be new, ready, running, waiting, halted, and so on 2. Program counter The counter indicates the address of next instruction to be executed for this process. 3. CPU registers Number and type of registers They include accumulators, index registers, stack pointers, general purpose registers and any condition-code information. Along with the program counter, this state information must be saved when an interrupt occurs, to allow the process to be continued correctly afterward. 4. CPU scheduling information This includes a process priority, pointers to scheduling queues, and any other scheduling parameters. 5. Memory-management information Value of base and limit registers, page table or segment table 6. Accounting information Amount of CPU and real time used, time limits, account numbers , job or process numbers, 7. Accounting information List of I/O devices allocated to the process list of open files Process Scheduling: The objective of multiprogramming is to have some processes running at all time, to maximize CPU utilization. The objective of time sharing is to switch CPU among the processes so frequently that users can interact with each process while it is running. To meet these objectives, process scheduler selects an available process for execution on CPU.

5 For a single-processor system, there will never be more than one running process,. If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled.

The ready queue and various I/O device queues Scheduling Queues: Job queue Set of all processes in the system Ready queues 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 Ready queue Set of all processes residing in main memory, ready and waiting to execute. Header of the queue contains the pointers to first and last PCB. Each PCB contains a pointer to next PCB. I/O Device queues

6 Set of processes waiting for an I/O device

A new process initially put in ready queue, where it waits for CPU for execution. Once process is allocated CPU, following events may occur: The process issues an I/O request, and then be placed in an I/O device queue. The process creates a new sub process and waits for its termination. The process is removed forcibly from the CPU as a result of interrupt or expired time slice. The process ends.

7 A process migrates between the various scheduling queues throughout its life time. The operating system must select the processes from or to the scheduling queues. The selection process is carried out by schedulers.

Process Scheduling: Types of Schedulers Long-term scheduler (or job scheduler) Selects which processes should be brought into the ready queue. Short-term scheduler (or CPU scheduler) Selects process from ready queue which should be executed next and allocates CPU. Medium-term scheduler Swap in the process from secondary storage into the ready queue.

Process Scheduling: Schedulers Short-term scheduler is invoked very frequently (milliseconds) (must be fast). Long-term scheduler is invoked infrequently (seconds, minutes) (may be slow). The long-term scheduler controls the degree of multiprogramming. Processes can be described as I/O-bound process spends more time doing I/O than computations, many short CPU bursts may be needed.

8 CPU-bound process spends more time doing computations; long CPU bursts are required. System should have mix of both type of processes so I/O waiting queue and ready queue will have equal and balanced work load. Context Switch Many users processes and system processes run simultaneously. Switching CPU from one process to another process is context switch. The system must save the state of the old process and load the saved state of the new process. Context-switch time is overhead and the system does no useful work while switching. Switching speed varies from machine to machine depending upon memory, number of registers and hardware support. Information in saved process image User data Program counter System stack PCB How does a CPU Switch from Process to Process?

Operations on Processes

9 In multiprogramming, processes may be created and deleted dynamically. Operations on Processes: Process Creation One process (Parent process) can create several other processes (children processes), which, in turn create other processes, forming a tree of processes. Process is identified by unique number i.e. Process identifier (PID), typically a number. Processes Tree on Solaris System

Process Creation Each child process needs resources, resource sharing can be handled as: Parent and child share no resources, but access directly from OS Parent and children share all the resources. Children share subset of parents resources, because sub process can overload the system. Execution Parent and children execute concurrently. Parent waits until children terminate. Address space Child is duplicate of parent (same data and program as parent has). Child has a program loaded into it.

10

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 Termination Process executes last statement and ask OS to terminate itself through exit(). Child process may return output data to its parent via wait(). Process resources like virtual memory, open files, I/O buffers are deallocated by operating system. A process can cause termination of other process appropriate system call (e.g. TerminateProcess() in Win 32) Parent may terminate execution of children processes abort(). Reasons may be: Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting: Operating system does not allow child to continue if its parent terminates. Thus Cascading termination takes place. Cooperating Processes The concurrent processes executing in the OS may be either independent processes or cooperating processes. Independent processes cannot affect or be affected by the execution of another process.

11 Cooperating processes (share data with other) can affect or be affected by the execution of another process. Advantages of process cooperation Information sharing Several user interested in same information so we must provide environment to allow concurrent access. Computation speed up Breaking the main task into subtask and each of which run parallel Each subtask assigned to each process. Modularity Dividing system function into separate processes. Convenience A user may editing, printing, compiling in parallel. At that time both the process should not disturb each other. Models for Inter process Communication Shared Memory Message passing

Shared Memory A region of memory is shared, processes can exchange information by reading and writing data to the shared region. It is fast and useful for large data. Message passing

12 Communication takes place by message passing between the processes It is useful for exchanging small amount of data. Mostly used in distributed environment 1.Shared Memory It requires to establish a region of shared memory which resides in the address space of the process creating shared-memory segment. Communicating processes can exchange information by reading and writing data in the shared area. Shared area need to be maintained by communicating processes. Example: producer- consumer process Producer process produces information that is consumed by a consumer process A buffer in memory is declared that can be filled by producer and emptied by consumer. Producer and consumer must be synchronized as consumer should not try to consume the data which has not been produced yet. Example: producer- consumer process Unbounded-buffer solution It has no practical limit on the size of the buffer Consumer may wait for new data but producer can always produce new data. Bounded-buffer solution It has fixed buffer size Consumer must wait if buffer is empty and producer must wait if buffer is full. Bounded-buffer solution Shared buffer is implemented as a circular array with two logical pointers in and out. Variable in points to next free position in the buffer and out points to first full position in the buffer. Buffer is empty when

13 in == out Buffer is full when (in + 1) % BUFFER SIZE) == out #define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Producer Process item nextProduced; while (true) { /* Produce an item in nextProduced */ while (((in + 1) % BUFFER SIZE) == out); /* do nothing*/ buffer[in] = nextProduced; in = (in + 1) % BUFFER SIZE; } Consumer Process item nextConsumed while (true) { while (in == out) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER SIZE;

14 // consume the item from the buffer in nextConsumed }

2. Message passing
Message passing system allows processes to communicate with each other without shared memory. Message passing facility provides two operations: send(message) receive(message) Message size may be: Fixed size: System implementation is straight forward and task of programming is more difficult. Variable size: System implementation is complex and task of programming becomes simpler. If P and Q processes wish to communicate, they need to: establish a communication link between them exchange messages via send() / receive() Implementation of communication link physical (e.g., shared memory, hardware bus) logical (e.g., logical properties) Logical implementation of communication link Direct or Indirect Communication Direct Communication Properties of communication link A link is established automatically between pair of processes that want to communicate.

15 The processes must know the identity of each other. A link is associated with exactly one pair of communicating processes. Between each pair there exists exactly one link. The link may be unidirectional, but is usually bi-directional. There can be symmetry or asymmetry in addressing Symmetric addressing Processes must name each other explicitly: send (P, message): send a message to process P receive (Q, message): receive a message from process Q Asymmetric addressing send (P, message): send a message to process P receive (id, message): receive a message from any process, the variable id is set to the name of the process with which communication has taken place. Disadvantages: Limited modularity: change of the name of a process may necessitate examining all other process definitions. All references to the old name must be found and modified to the new name. Indirect Communication Properties of communication link A link is established between a pair of processes if both share a common mailbox A link may be associated with more than two processes. A number of different links may exist between each pair of communicating processes, with each link corresponding to one mailbox. Link may be unidirectional or bi-directional. Messages are directed and received from mailboxes Each mailbox has a unique id. Processes can communicate only if they share a mailbox.

16 A process can communicate with one or more processes via a number of different mail boxes. Operations create a new mailbox send and receive messages through mailbox destroy a mailbox Primitives are defined as send(A, message) send a message to mailbox A receive(A, message) receive a message from mailbox A Mailbox sharing P1, P2, and P3 share mailbox A. P1, sends; P2 and P3 receive. Problem: Which process (P2 or P3) will receive the message Solutions Allow a link to be associated with at most two processes. Allow only one process at a time to execute a receive operation. Allow the system to select arbitrarily the receiver. Sender is notified who was the receiver. A mailbox may be owned either by OS or by the process. If mailbox owned by the process Mailbox is part of the address space of the process. Owner process only receives the messages from this mailbox. Other processes only can send message to this mailbox. Since each mailbox has a unique owner, there is no confusion about who should receive the message sent to this mailbox. If mailbox owned by the process When the process that owns a mailbox terminates, the mailbox disappears.

17 Any process that subsequently send message to this mailbox must be notified that the mailbox is no longer exists. Ownership and receive privilege may be passed to other processes through appropriate system calls. If mailbox owned by OS A mailbox owned by OS is independent. OS does following operations create a new mailbox provides access rights to processes allows processes to send and receive messages through mailbox destroy a mailbox Synchronization: Message passing may be either blocking or non-blocking. Blocking is considered as synchronous Non-blocking is considered as asynchronous send and receive primitives may be either blocking or non-blocking. Any combination of send and receive is possible. Blocking send (synchronous) The sending process is blocked until the message received by the receiving process or by the mailbox. Non-blocking send (asynchronous) The sending process sends the message and resumes operation. Blocking receive (synchronous) The receiving process is blocked until the message is available. Non-blocking receive (asynchronous) The receiving process receives either a valid message or a null. Buffering

18 Messages resides in temporary queue which can be implemented in one of three ways. 1. Zero capacity 0 messages, no message can wait in queue. Sender must wait for receiver. 2. Bounded capacity finite length of n messages, Sender must wait if link is full. 3. Unbounded capacity infinite length of queue, Sender never waits. 4. Zero-capacity is message system without buffering Bounded and unbounded is with automatic buffering.

S-ar putea să vă placă și