250 likes | 277 Views
Chapter 5: Processes. Chapter 5 : Processes. Process Concept Process Scheduling Operations on Processes Inter - process Communication. Objectives. Identify the separate components of a process and illustrate how they are represented and scheduled in an operating system.
E N D
Chapter 5: Processes • Process Concept • Process Scheduling • Operations on Processes • Inter-process Communication
Objectives • Identify the separate components of a process and illustrate how they are represented and scheduled in an operating system. • Describe how processes are created and terminated in an operating system • Describe and contrast inter-process communication using shared memory and message passing.
Process Concept • An operating system executes a variety of programs that run as a process. • Process – a program in execution; process execution must progress in sequential fashion • Multiple parts • The program code, also called text section • Current activity including programcounter, processor registers • Stackcontaining temporary data • Function parameters, return addresses, local variables • Data sectioncontaining global variables • Heapcontaining memory dynamically allocated during run time
Process Concept (Cont.) • Program is passive entity stored on disk (executable file); process is active • Program becomes process when executable file loaded into memory • Execution of program started via GUI mouse clicks, command line entry of its name, etc • One program can be several processes • Consider multiple users executing the same program
Process State • As a process executes, it changes state • New: The process is being created • Running: Instructions are being executed • Waiting: The process is waiting for some event to occur • Ready: The process is waiting to be assigned to a processor • Terminated: The process has finished execution
Process Control Block (PCB) • Each process is represented in the operating system by a Process Control Block (PCB), also called a Task Control Block. • It contains many pieces of Information associated with each process. • Process state – running, waiting, etc • Program counter – location of instruction to next execute • CPU registers – contents of all process-centric registers • CPU scheduling information- priorities, scheduling queue pointers • Memory-management information – memory allocated to the process • Accounting information – CPU used, clock time elapsed since start, time limits • I/O status information – I/O devices allocated to process, list of open files
Process Scheduling • Maximize CPU use, quickly switch processes onto CPU core Scheduling Queues • As processes enter the system, They are put into a job queue, which consists of all processes in the system • Process scheduler selects among available processes for next execution on CPU core • Maintains scheduling queues of processes • Ready queue – set of all processes residing in main memory, ready and waiting to execute • Wait queues – set of processes waiting for an event (i.e. I/O) • Processes migrate among the various queues
Representation of Process Scheduling • A new process is initially put in the ready queue. It waits there until it is selected for execution, or is dispatched (يرسل). Once the process is allocated the CPU start execute it, one of several events could occur: 1. The process could issue an I/O request and then be placed in an I/O queue. 2. The process could create a new sub-process and wait for the sub-process's termination. 3. The process could be removed from the CPU, as a result of an interrupt, and be put back in the ready queue.
Schedulers • A process migrates (يهاجر) among the various scheduling queues throughout its lifetime. The operating system must select, for scheduling purposes, processes from these queues in some fashion. The selection process is carried out by the appropriate scheduler (جدولة). • In a batch system, more processes are submitted then can be executed immediately. These processes are spooled (تخزن) to a mass-storage device (typically a disk), where they are kept for later execution. • Types of scheduler • 1. Short-term scheduler (or CPU scheduler): – selects which process should be executed next and allocates CPU • Sometimes the only scheduler in a system. • Short-term scheduler is invoked frequently (استدعاء متكرر) (milliseconds) ⇒ (must be fast). • 2. Long-term scheduler (or job scheduler): – selects which processes should be brought into the ready queue
Schedulers • 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 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 • Long-term scheduler strives (do there best) for good process mix • 3. Some operating systems, such as time-sharing systems, may introduce an additional, intermediate level of scheduling. This is called Medium-term scheduler. • A medium-term scheduler can be added if degree of multiple programming needs to decrease, its sometimes it can be advantageous to remove processes from memory (and from active contention (نزاع) for the CPU) and thus reduce the degree of multiprogramming. Later, the process can be reintroduced into memory, and its execution can be continued where it left off. This scheme is called swapping.
Context Switch A context switch occurs when the CPU switches from one process to another. • Switching the CPU to another process requires performing the system to save the state of the current process and a load the saved state of the new process via a context switch. • When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run. • Context-switch speed varies from machine to machine, depending on the memoryspeed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers).
Operations on Processes • System must provide mechanisms for: • process creation • process termination
Process Creation • A process may create several new processes, via a create-process system call, during the course of execution. • Parentprocess create childrenprocesses, which, in turn create other processes, forming a tree of processes • Generally, process identified and managed via aprocess identifier (pid), which is typically an integer number. • The pid provides a unique value for each process in the system, and it can be used as an index to access various attributes of a process within the kernel.
Process Creation • A process will need certain resources (CPU time, memory, files, and I/O devices) to accomplish its task. • When a process creates a sub-process, that sub-process may be able to: - Obtain its resources directly from the operating system, or - It may be constrained to a subset of the resources of the parent process. • Resource sharing options • Parent and children share all resources • Children share subset of parent’s resources • Parent and child share no resources • Execution options When a process creates a new process, two possibilities exist in terms of execution: • Parent and children execute concurrently • Parent waits until children terminate
Process Termination • A process terminates when it finishes executing its final statement and asks the operating system to delete it by using the exit () system call. • At that point the process may: • Return a status value (typically an integer) from child to its parent process (via the wait() system call).. • All the resources of the process, including physical and virtual memory, open files and I/O buffers—are deallocated by the operating system • Parent may terminate the execution of children processes using the abort() system call. Some reasons for doing so: • Child has exceeded allocated resources • Task assigned to child is no longer required • The parent is exiting and the operating systems does not allow a child to continue if its parent terminates • Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all its children must also be terminated.
Interprocess Communication • Processes executing within a system may be independentor cooperating • A process is independent if it cannot affect or be affected by the other processes executing in the system. Any process that does not share data with any other process is independent • Cooperating process can affect or be affected by other processes, including sharing data. • Reasons for cooperating processes: • Information sharing • Computation speedup • Modularity • Convenience • Cooperating processes require an Inter ProcessCommunication (IPC) mechanism that will allow them to exchange data and information.
There are two fundamental models of inter-process communication: 1. A Shared memory model, a region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region. 2. Message passingmodel, communication takes place by means of messages exchanged between the cooperating processes. (a) Shared memory. (b) Message passing.