Full Transcript

Chapter 3 Processes Prepared by Dr. Babar Shah Objectives  To introduce the notion of a process -- a program in execution, which forms the basis of all computation  To describe the various features of processes, including scheduling, creation and termination, and communication  To explore...

Chapter 3 Processes Prepared by Dr. Babar Shah Objectives  To introduce the notion of a process -- a program in execution, which forms the basis of all computation  To describe the various features of processes, including scheduling, creation and termination, and communication  To explore inter-process communication using shared memory and message passing  To describe communication in client-server systems Process Concept  What is a Process? – a program in execution;  It is Program code and execution state (see later)  A process is just an instance of an executing program  The CPU switches back and forth from process to process. This rapid switching back and forth is called Multiprogramming.     In any one instant the CPU is running only one process, in the course of 1 second it may work on several of them, giving the illusion of parallelism. Program is passive entity stored on disk (executable file), process is active Execution of program starts via GUI mouse clicks, command line entry of its name, etc Job and process used interchangeably Process Concept (Cont.)   To execute, processes need resources like  Memory space (loaded into RAM) if too long some parts in SWAP or virtual memory (part of the disk –extension of RAM)  CPU  Other resources like Input and/or Output devices It is the OS which assigns resources to processes s Process in Memory –Address Space  The program code a.k.a. text section • A memory map of the executable file's code • Read only section and cannot be changed during execution  Data section containing global variables • A memory map of the executable file's initialized global variables with some values  Heap containing memory dynamically allocated during run time  Stack containing temporary data  Function parameters, return addresses, local variables Process State   As a process executes, it changes state: New: The process is being created. A program which is going to be picked up by the OS into the main memory is called a new process.  Ready: The process is waiting to be assigned to a processor. Whenever a process is created, it directly enters in the ready state, where it waits for the CPU to be assigned.  Running: Instructions are being executed (assigned to CPU) One of the processes from the ready state will be chosen by the OS depending upon the scheduling algorithm.  Waiting or Block: The process is waiting for some event to occur From the Running state, a process can make the transition to the block or wait state depending upon the scheduling algorithm. When a process waits for a certain resource to be assigned or for the input from the user then the OS move this process to the block or wait state and assigns the CPU to the other processes.  Terminated: The process has finished execution All the context of the process (Process Control Block) will also be deleted the process will be terminated by the Operating system. Diagram of Process State  With One CPU, Only 1 process is running  Many processes can be ready or waiting Process States Process Control Block (PCB) Question: how OS keeps track of processes? Answer: Using PCB (information associated with each process) While creating a process the operating system performs several operations. To identify the processes, it assigns a process identification number to each process. As the operating system supports multi-programming, it needs to keep track of all the processes. For this task, the process control block (PCB) is used to track the process’s execution status. Each block of memory contains information about the process and all these information is required and must be saved when the process is switched from one state to another. When the process makes a transition from one state to another, the operating system must update information in the process’s PCB. Process Control Block Process Control Block (PCB) Elements of PCB  Process state – Specifies the process state i.e., new, ready, running, waiting or terminated.  Process ID - Unique identifier given to processes  Program counter – Contains the location/address of the next instruction that needs to be executed in the process  CPU registers – Specifies the CPU registers that are used by the process. They may include accumulators, index registers, stack pointers, general purpose registers etc.  CPU scheduling information- Process priorities, pointers to scheduling queue etc., is the CPU scheduling information that is contained in the PCB.  Memory-management information – Memory allocated to the process. The memory management information includes the page tables or the segment tables depending on the memory system used.  Accounting information – CPU used, clock time elapsed since start, time limits, etc., are all a part of the PCB accounting information.  I/O status information – I/O devices allocated to process, list of open files etc., CPU Switch From Process to Process Operations on Processes Process Creation A Process may create several new processes, via a create-process system call, during the course of execution. The creating process is called a parent process, and the new processes are called the children of that process.  Parent process create children processes, which, in turn, create other processes, forming a tree of processes  Generally, the process is identified and managed via a process identifier (pid) i ni t pi d = 1 l ogi n pi d = 8415 khel per pi d = 6 bash pi d = 8416 ps pi d = 9298 sshd pi d = 3028 kthreadd pi d = 2 pdfl ush pi d = 200 emacs pi d = 9204 A Tree of Processes in Linux sshd pi d = 3610 t csch pi d = 4005 Operations on Processes Process Creation  Once the OS decides to create a new process it: Assigns a unique process identifier to the new process Allocates space for the process Initializes the process control block Sets the appropriate linkages Creates or expands other data structures Operations on Processes After Creation  After creating the process the Kernel can do one of the following, as part of the dispatcher routine:  Stay in the parent process. Control returns to user mode at the point of the fork call of the parent.  Transfer control to the child process. The child process begins executing at the same point in the code as the parent, namely at the return from the fork call.  Transfer control to another process. Both parent and child are left in the Ready to Run state. Operations on Processes Process Termination  Once a process terminates, all resources assigned to it will be deallocated by the OS  A process terminates either  Normal exit (voluntary), execute last instruction of the code then asks the operating system to delete it using the exit() system call. or interactive mode close the application  Error exit like instruction division by 0, accessing file not existing, etc.  A parent process can terminate a child process (has exceeded allocated resources, Task assigned to child is no longer required)  Some situation when a parent process is terminated, all child and grandchild processes are terminated as well (cascading termination) Process Scheduling  Maximize CPU use, quickly switch processes onto CPU for time sharing  Process scheduler selects among available processes for next execution on CPU  Maintains scheduling queues of processes   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 during their lifetime in the system migrate among the various queues Representation of Process Scheduling  Queueing diagram represents queues, resources, flows Inter-Process Communication (IPC)  Processes within a system may be independent or cooperating  An independent process is not affected by the execution of other processes  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 need inter-process communication (IPC) mechanism that allow them to exchange data and information.  Processes can communicate with each other by Two models of IPC  Shared memory  Message passing Communications Models (a) Message passing. (b) shared memory Inter-process Communication – Shared Memory  An area of memory shared among the processes that wish to communicate  Processes can then exchange information by reading and writing data to the shared region.  The communication is under the control of the users processes not the operating system.  Major issues is to provide mechanism that will allow the user processes to synchronize their actions when they access shared memory.  Synchronization is discussed in great details in Chapter 5. Inter-process Communication – Message Passing  Mechanism for processes to communicate and to synchronize their actions  Message system – Communication take place by means of messages exchanged between the cooperating processes.  IPC facility provides two operations:   send(message)  receive(message) The message size is either fixed or variable Message Passing (Cont.)   If processes P and Q wish to communicate, they need to:  Establish a communication link between them  Exchange messages via send/receive Implementation issues:  How are links established?  Can a link be associated with more than two processes?  How many links can there be between every pair of communicating processes?  What is the capacity of a link?  Is the size of a message that the link can accommodate fixed or variable?  Is a link unidirectional or bi-directional? Message Passing (Cont.)  Implementation of communication link  Physical:  Shared memory  Hardware bus  Network  Logical:  Direct or indirect  Synchronous or asynchronous  Automatic or explicit buffering Direct Communication   Processes must name each other explicitly:  send (P, message) – send a message to process P  receive(Q, message) – receive a message from process Q Properties of communication link  Links are established automatically  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 Indirect Communication   Messages are directed and received from mailboxes (also referred to as ports)  Each mailbox has a unique id  Processes can communicate only if they share a mailbox Properties of communication link  Link established only if processes share a common mailbox  A link may be associated with many processes  Each pair of processes may share several communication links  Link may be unidirectional or bi-directional Indirect Communication   Operations  create a new mailbox (port)  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 Indirect Communication   Mailbox sharing  P1, P2, and P3 share mailbox A  P1, sends; P2 and P3 receive  Who gets 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 the receiver was. Synchronization  Message passing may be either blocking or non-blocking  Blocking is considered synchronous   Blocking send -- the sender is blocked until the message is received  Blocking receive -- the receiver is blocked until a message is available Non-blocking is considered asynchronous  Non-blocking send -- the sender sends the message and continue  Non-blocking receive -- the receiver receives:  A valid message, or  Null message Buffering  Queue of messages attached to the link.  implemented in one of three ways 1. Zero capacity – no messages are queued on a link. Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits  Textbook:   Silberschatz, Operating System Concepts References:  W. Stallings, Operating Systems: Internals And Design Principles, 9/E  Andrew Tanenbaum & Herbert Bos, Modern Operating Systems, 4/E  https://www.cs.unc.edu/~porter/courses/cse306/s13/slides/

Use Quizgecko on...
Browser
Browser