2 Process A program in execution An instance of a program running on a computerThe entity that can be assigned to and executed on a processorA unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system instructions
3 Process ManagementA process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity.Process needs resources to accomplish its taskCPU, memory, I/O, filesInitialization dataProcess termination requires reclaim of any reusable resources
4 Process ManagementSingle-threaded process has one program counter specifying location of next instruction to executeProcess executes instructions sequentially, one at a time, until completionMulti-threaded process has one program counter per threadTypically system has many processes, some user, some operating system running concurrently on one or more CPUsConcurrency by multiplexing the CPUs among the processes / threads
5 Process Elements Identifier State Priority Program counter Memory pointersContext dataI/O status informationAccounting information
6 Process Control Block Contains the process elements Created and managed by the operating systemAllows support for multiple processes
13 Context SwitchWhen CPU switches to another process, the system must save the state of the old process and load the saved state for the new processContext-switch time is overhead; the system does no useful work while switchingTime dependent on hardware support
14 Two-State Process Model Process may be in one of two statesRunningNot-running
19 Five State Model Transition NULL----NEW: A new process is created due to any of the four reasons described in the creation of processes (New batch Job, Interactive logon, To provide service & Spawning).NEW-----READY: Operating system moves a process from new state to ready state, when it is prepared to accept an additional process. There could be limit on number of processes to be admitted to the ready state.READY---RUNNING: Any process can be moved from ready to running state when ever it is scheduled.
20 Five State Model Transition RUNNING----EXIT: The currently running process is terminated if it has signaled its completion or it is aborted.RUNNING----READY:The most commonly known situation is that currently running process has taken its share of time for execution (Time out). Also, in some events a process may have to be admitted from running to ready if a high priority process has occurred.
21 Five State Model Transition RUNNING----BLOCKED:A process is moved to the blocked state, if it has requested some data for which it may have to wait. For example the process may have requested a resource such as data file or shared data from virtual memory, which is not ready at that time.BLOCKED---READY:A process is moved to the ready state, if the event for which it is waiting has occurred.
22 Five State Model Transition There are two more transition but are not shown for clarity.READY----EXIT:This is the case for example a parent process has generated a single or multiple children processes & they are in the ready state. Now during the execution of the process it may terminate any child process, therefore, it will directly go to exit state.BLOCKED----EXIT:Similarly as above, during the execution of a parent process any child process waiting for an event to occur may directly go to exit if the parent itself terminates.
27 Suspended ProcessesProcessor is faster than I/O so all processes could be waiting for I/OSwap these processes to disk to free up more memoryBlocked state becomes suspend state when swapped to diskTwo new statesBlocked/SuspendReady/Suspend
33 Process Creation Assign a unique process identifier Allocate space for the processInitialize process control blockSet up appropriate linkagesEx: add new process to linked list used for scheduling queueCreate of expand other data structuresEx: maintain an accounting file
35 Process CreationParent process create children processes, which, in turn create other processes, forming a tree of processesResource sharingParent and children share all resourcesChildren share subset of parent’s resourcesParent and child share no resourcesExecutionParent and children execute concurrentlyParent waits until children terminate
36 Process TerminationProcess executes last statement and asks the operating system to delete it (exit)Output data from child to parent (via wait)Process’ resources are deallocated by operating systemParent may terminate execution of children processes (abort)Child has exceeded allocated resourcesTask assigned to child is no longer requiredIf parent is exitingSome operating system do not allow child to continue if its parent terminatesAll children terminated - cascading termination
37 Process TerminationReasons for process termination
38 Process TerminationReasons for process termination
39 Modes of Execution User mode System mode, control mode, or kernel mode Less-privileged modeUser programs typically execute in this modeSystem mode, control mode, or kernel modeMore-privileged modeKernel of the operating system
40 When to Switch a Process Clock interruptprocess has executed for the maximum allowable time sliceI/O interruptMemory faultmemory address is in virtual memory so it must be brought into main memory
41 When to Switch a Process Traperror or exception occurredmay cause process to be moved to Exit stateSupervisor callsuch as file open
42 Change of Process State Save context of processor including program counter and other registersUpdate the process control block of the process that is currently in the Running stateMove process control block to appropriate queue – ready; blocked; ready/suspendSelect another process for executionUpdate the process control block of the process selectedUpdate memory-management data structuresRestore context of the selected process
43 Cooperating Processes Independent process cannot affect or be affected by the execution of another processCooperating process can affect or be affected by the execution of another processAdvantages of process cooperationInformation sharingComputation speed-upModularityConvenience
44 Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actionsMessage system – processes communicate with each other without resorting to shared variablesIPC facility provides two operations:send(message) – message size fixed or variablereceive(message)If P and Q wish to communicate, they need to:establish a communication link between themexchange messages via send/receiveImplementation of communication linkphysical (e.g., shared memory, hardware bus)logical (e.g., logical properties)
45 Direct Communication Processes must name each other explicitly: send (P, message) – send a message to process Preceive(Q, message) – receive a message from process QProperties of communication linkLinks are established automaticallyA link is associated with exactly one pair of communicating processesBetween each pair there exists exactly one linkThe link may be unidirectional, but is usually bi-directional
46 Indirect Communication Messages are directed and received from mailboxes (also referred to as ports)Each mailbox has a unique idProcesses can communicate only if they share a mailboxProperties of communication linkLink established only if processes share a common mailboxA link may be associated with many processesEach pair of processes may share several comm. linksLink may be unidirectional or bi-directional
47 Indirect Communication Operationscreate a new mailboxsend and receive messages through mailboxdestroy a mailboxPrimitives are defined as:send(A, message) – send a message to mailbox Areceive(A, message) – receive a message from mailbox A
48 Indirect Communication Mailbox sharingP1, P2, and P3 share mailbox AP1, sends; P2 and P3 receiveWho gets the message?SolutionsAllow a link to be associated with at most two processesAllow only one process at a time to execute a receive operationAllow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
49 Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronousBlocking send has the sender block until the message is receivedBlocking receive has the receiver block until a message is availableNon-blocking is considered asynchronousNon-blocking send has the sender send the message and continueNon-blocking receive has the receiver receive a valid message or null
50 ThreadsResource ownership - process includes a virtual address space to hold the process imageScheduling/execution- follows an execution path that may be interleaved with other processesThese two characteristics are treated independently by the operating systemDispatching is referred to as a thread or lightweight processResource of ownership is referred to as a process or task
51 MultithreadingOperating system supports multiple threads of execution within a single processMS-DOS supports a single threadUNIX supports multiple user processes but only supports one thread per processWindows, Solaris, Linux, Mach, and OS/2 support multiple threads
53 Process Have a virtual address space which holds the process image Protected access to processors, other processes, files, and I/O resources
54 Thread An execution state (running, ready, etc.) Saved thread context when not runningHas an execution stackSome per-thread static storage for local variablesAccess to the memory and resources of its processall threads of a process share this
56 Benefits of ThreadsTakes less time to create a new thread than a processLess time to terminate a thread than a processLess time to switch between two threads within the same processSince threads within the same process share memory and files, they can communicate with each other without invoking the kernel
57 Uses of Threads in a Single-User Multiprocessing System Foreground to background workAsynchronous processingSpeed of executionModular program structure
58 ThreadsSuspending a process involves suspending all threads of the process since all threads share the same address spaceTermination of a process, terminates all threads within the process
59 Thread States States associated with a change in thread state Spawn Spawn another threadBlockUnblockFinishDeallocate register context and stacks
63 User-Level Threads All thread management is done by the application The kernel is not aware of the existence of threads
64 Kernel-Level Threads Windows is an example of this approach Kernel maintains context information for the process and the threadsScheduling is done on a thread basis
65 Kernel-Level ThreadsThis approach overcome the two principal drawbacks of ULT approachThe kernel can simultaneously schedule threads on different processorsIf one thread of a process is blocked it can schedule another thread of the same processThe disadvantage isTransfer of control from one thread to another thread within the same process requires mode switching
66 Combined Approaches Example is Solaris Thread creation done in the user spaceBulk of scheduling and synchronization of threads within application
67 Combined Approaches -Adv Multiple threads within the same application can run concurrently on a number of processors.Blocking system calls need not block theentire process.