PROCESS MANAGMENT

57
PROCESS MANAGMENT 1

description

PROCESS MANAGMENT. Processes and Process Control Blocks. Process can be defined as the followings: A program in execution An instance of a program running on a computer. The entity that can be assigned to and executed on a processor. - PowerPoint PPT Presentation

Transcript of PROCESS MANAGMENT

Processes and Process Control Blocks

PROCESS MANAGMENT1Processes and Process Control BlocksProcess can be defined as the followings:A program in execution An instance of a program running on a computer. The entity that can be assigned to and executed on a processor.A unit of activity characterized by the execution of a sequence of instructions, a current state, and an associated set of system resources.2 ProcessA batch system executes jobs, whereas a time- shared system has user programs, or tasks. Even on a single-user system, such as Microsoft Windows and Macintosh OS, a user may be able to run several programs at one time: a word processor, web browser, and e-mail package.3 ProcessEven if the user can execute only one program at a time, the operating system may need to support its own internal programmed activities, such as memory management.4 ProcessOS can manage the execution of applications so that:Resources are made available to multiple applications.The physical processor is switched among multiple applications so all will appear to be progressing.The processor and I/O devices can be used efficiently.5 ProcessTwo essential elements of a process are program code(which may be shared with other processes that are executing the same program) and a set of data associated with that code.At any given point in time, while the program is executing, this process can be uniquely characterized by a number of elements, including the following:6 Process7

The information in the list is stored in a data structure, typically called a process control block (PCB), that is created and managed by the OS and uniquely identifies a process.

ProcessAccounting information: This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on. status information: The information includes the list of I/O devices allocated to this process, a list of open files(Active files) , and so on.8 ProcessThe process control block is the key tool that enables the OS to support multiple processes and to provide for multiprocessing. When a process is interrupted, the current values of the program counter and the processor registers (context data) are saved in the appropriate fields of the corresponding process control block, and the state of the process is changed to some other value, such as blocked or ready.9 Process StatesWe can characterize the behavior of an individual process by listing the sequence of instructions that execute for that process. Such a listing is referred to as a trace of the process. We can characterize behavior of the processor by showing how the traces of the various processes are interleaved.A small dispatcher program is used to switch the processor from one process to another.10 A two state process modelThe operating systems principal responsibility is controlling the execution of processes; this includes determining the interleaving pattern for execution and allocating resources to processes.11

Process States The Creation and Termination of ProcessesWhen a new process is to be added to those currently being managed, the OS builds the data structures that are used to manage the process and allocates address space in main memory to the process.When one process spawns another, the former is referred to as the parent process, and the spawned process is referred to as the child process.

12 Process States Reasons for job creation

13

Process States Any computer system must provide a means for a process to indicate its completion. A batch job should include a Halt instruction or an explicit OS service call for termination. In the former case, the Halt instruction will generate an interrupt to alert the OS that a process has completed.For an interactive application, the action of the user will indicate when the process is completed.On a personal computer or workstation, a user may quit an application (e.g., word processing or spreadsheet).All of these actions ultimately result in a service request to the OS to terminate the requesting process.14 A five state modelFive state process model

15 Process States Running: The process that is currently being executed Ready: A process that is prepared to execute when given the opportunity.Blocked/Waiting: A process that cannot execute until some event occurs, such as the completion of an I/O operation.16 Process States New: A process that has just been created but has not yet been admitted to the pool of executable processes by the OS. Typically, a new process has not yet been loaded into main memory, although its process control block has been created.Terminated: A process that has been released from the pool of executable processes by the OS, either because it halted or because it aborted for some reason.17 Process States The new process will terminate, usually due to one of the following conditions:

1. Normal exit (voluntary). 2.Error exit (voluntary).3.Fatal error (involuntary).4.Killed by another process(involuntary).

18 Process States

19 Process StatesProcess can be executed in one of the following modes:User modeKernel modeConsiderations of memory protection and privileged instructions lead to the concept of modes of operation.A user program executes in a user mode, in which certain areas of memory are protected from the users use and in which certain instructions may not be executed. The monitor executes in a system mode, or called kernel mode, in which privileged instructions may be executed and in which protected areas of memory may be accessed.

20OS Control StructuresIf the OS is to manage processes and resources, it must have information about the current status of each process and resource.Although the details will differ from one OS to another, fundamentally, all operating systems maintain information in these four categories.Memory tablesI/O tablesFile tables andProcess tables21To manage and control a process the OS must know first where the process is located; second, it must know the attributes of the process that are necessary for its management (e.g., process ID and process state).A process includes a set of programs to be executed, data, stack, and attributes. We can refer to this collection as the process image.22OS Control Process/context SwitchingAt some time, a running process is interrupted and the OS assigns another process to the Running state and turns control over to that process.A process switch may occur any time that the OS has gained control from the currently running process.

23

Process/context SwitchingFirst, let us consider system interrupts. Actually, we can distinguish, as many systems do, two kinds of system interrupts, one of which is simply referred to as an interrupt, and the other as a trap. The former is due to some sort of event that is external to and independent of the currently running process, such as the completion of an I/O operation. The latter relates to an error or exception condition generated within the currently running process, such as an illegal file access attempt.24 Cooperating ProcessesThe concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process is independent if it cannot affect or be affected by the other processes executing in the system. Clearly, any process that does not share any data (temporary or persistent) with any other process is independent. On the other hand, a process is cooperating if it can affect or be affected by the other processes executing in the system. Clearly, any process that shares data with other processes is a cooperating process.25 Cooperating ProcessesWe may want to provide an environment that allows process cooperation for several reasons: Information sharing: Since several users may be interested in the same piece of information(for instance, a shared file), we must provide an environment to allow concurrent access to these types of resources. Computation speedup: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing in parallel with the others. Such a speedup can be achieved only if the computer has multiple processing elements (such as CPU or I/O channels).26 Cooperating ProcessesModularity: We may want to construct the system in a modular fashion, dividing the system functions into separate processes or threads. Convenience: Even an individual user may have many tasks on which to work at one time. For instance, a user may be editing, printing, and compiling in parallel.27 Cooperating ProcessesTo illustrate the concept of cooperating processes, let us consider the producer-consumer problem, which is a common paradigm for cooperating processes.A producer process produces information that is consumed by a consumer process. For example, a print program produces characters that are consumed by the printer driver. A compiler may produce assembly code, which is consumed by an assembler. The assembler, in turn, may produce object modules, which are consumed by the loader.28 ThreadsThe concept of a process as embodying two characteristics:Resource ownership: A process includes a virtual address space to hold the process image; recall that the process image is the collection of program, data, stack, and attributes defined in the process control block. From time to time, a process may be allocated control or ownership of resources. The OS performs a protection function to prevent unwanted interference between processes with respect to resources.Scheduling/execution: The execution of a process follows an execution path(trace) through one or more programs. This execution may be interleaved with that of other processes. Thus, a process has an execution state and a dispatching priority and is the entity that is scheduled and dispatched by the OS.29 ThreadsThese two characteristics are independent and could be treated independently by the OS. To distinguish the two characteristics, the unit of dispatching is usually referred to as a thread or lightweight process, while the unit of resource ownership is usually still referred to as a process or task.30ThreadsProcesses do not share resources well high context switching overhead Idea: Separate concurrency from protection Multithreading: a single program made up of a number of different concurrent activities A thread (or lightweight process) basic unit of CPU utilization; it consists of: program counter, register set and stack space A thread shares the following with peer threads: code section, data section and OS resources (open files, signals) No protection between threads Collectively called a task. Heavyweight process is a task with one thread.

31Threads32

ThreadsBenefits of Multithreaded programmingResponsivenessResource SharingEconomy: It is much more time consuming to create and manage processes that threads. Eg. In Solaris, creating a process is thirty times slower than creating thread and context switching is five times slower. Utilization of Multiprocessor Architectures 33 Threads MultithreadingMultithreading refers to the ability of an OS to support multiple, concurrent paths of execution within a single process.In a multithreaded environment, a process is defined as the unit of resource allocation and a unit of protection.

34ThreadsThread State State shared by all threads in process/addr space Contents of memory (global variables, heap) I/O state (file system, network connections, etc) State private to each thread Kept in TCB (Thread Control Block) CPU registers (including, program counter) Execution stack Parameters, Temporary variables return PCs are kept while called procedures are executing

35ThreadsThread context switch still requires a register set switch, but no memory management related work!! Thread states ready, blocked, running, terminated Threads share CPU and only one thread can run at a time. No protection among threads.

36ThreadsTypes of threadsKernel Supported Threads (Mach and OS/2)User Level ThreadsHybrid approach implements both user-level and kernel-supported threads (Solaris 2)

37ThreadsKernel ThreadsSupported by the Kernel Native threads supported directly by the kernel Every thread can run or block independently

Downside of kernel threads: a bit expensive Need to make a crossing into kernel mode to schedule Examples Windows XP/2000, Solaris, Linux,Tru64 UNIX, Mac OS X, Mach, OS/2

38ThreadsUser Level ThreadsSupported above the kernel, via a set of library calls at the user level. Thread management done by user-level threads library User program provides scheduler and thread package May have several user threads per kernel thread User threads may be scheduled non-premptively relative to each other (only switch on yield()) Advantages Cheap, Fast Threads do not need to call OS and cause interrupts to kernel Disadv: If kernel is single threaded, system call from any thread can block the entire task. Example thread libraries: POSIX Pthreads, Win32 threads, Java threads

39ThreadsMultithreading ModelsThere are three common ways of establishing relationship between user threads and kernel threadsMany to one One to one Many to many 40Threads Many to one modelMaps many user level threads to one kernel thread

41

Efficient The entire process may block if a thread make a blocking system callExamples: Solaris Green Threads GNU Portable Threads

ThreadsOne to One modelEach user thread maps to a kernel thread

Creating kernel threads for each user can burden the performance of an application: Most OS limit the number of threadsExample:Windows NT/XP/2000; Linux; Solaris 9 and later 42

ThreadsMany to Many ModelAllows many user level threads to be mapped to many kernel threads Allows the OS to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

43

Interprocess Communication (IPC)Separate address space isolates processes High Creation/Memory Overhead; (Relatively) High Context-Switch Overhead Mechanism for processes to communicate and synchronize actions. Via shared memory - Accomplished by mapping addresses to common DRAM Read and Write through memory Via Messaging system - processes communicate without resorting to shared variables. send() and receive() messages Can be used over the network! Messaging system and shared memory not mutually exclusive can be used simultaneously within a single OS or a single process. 44Shared Memory CommunicatrionCommunication occurs by simply reading/writing to shared address page Really low overhead communication Introduces complex synchronization problems 45

Cooperating Processes via Message Passing IPC facility provides two operations. send (message) - message size can be fixed or variable receive (message) If processes P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Fixed vs. Variable size message Fixed message size straightforward physical implementation, programming task is difficult due to fragmentation Variable message size - simpler programming, more complex physical implementation.

46Direct CommunicationSender and Receiver 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. Exactly one link between each pair. Link may be unidirectional, usually bidirectional.

47Indirect CommunicationMessages are directed to and received from mailboxes (also called ports) Unique ID for every mailbox. Processes can communicate only if they share a mailbox. send(A, message) /* send message to mailbox A */ receive(A, message) /* receive message from mailbox A */ Properties of communication link Link established only if processes share a common mailbox. Link can be associated with many processes. Pair of processes may share several communication links Links may be unidirectional or bidirectional

48Indirect Communication using Mailboxes 49

Mailbox Operations create a new mailbox send/receive messages through mailbox destroy a mailbox Issue: Mailbox sharing P1, P2 and P3 share mailbox A. P1 sends message, P2 and P3 receive who gets message?? Possible Solutions disallow links between more than 2 processes allow only one process at a time to execute receive operation allow system to arbitrarily select receiver and then notify sender.

50Message Buffering Link has some capacity - determine the number of messages that can reside temporarily in it. Queue of messages attached to link Zero-capacity Queues: 0 messages sender waits for receiver (synchronization is called rendezvous) Bounded capacity Queues: Finite length of n messages sender waits if link is full Unbounded capacity Queues: Infinite queue length sender never waits

51Message Problems - Exception Conditions Lost Messages OS guarantees retransmission sender is responsible for detecting it using timeouts sender gets an exception Scrambled Messages Message arrives from sender P to receiver Q, but information in message is corrupted due to noise in communication channel. Solution need error detection mechanism, e.g. CHECKSUM need error correction mechanism, e.g. retransmission

52Producer-Consumer Problem Paradigm for cooperating processes; producer process produces information that is consumed by a consumer process. We need buffer of items that can be filled by producer and emptied by consumer. Unbounded-buffer places no practical limit on the size of the buffer. Consumer may wait, producer never waits. Bounded-buffer assumes that there is a fixed buffer size. Consumer waits for new item, producer waits if buffer is full. Producer and Consumer must synchronize.

53Bounded Buffer using messaging Producer repeat produce an item in nextp; send(consumer, nextp); until false; Consumer

repeat receive(producer, nextc); consume item from nextc; until false; 54

Bounded-buffer - Shared Memory Solution Shared data var n; type item = .; var buffer: array[0..n-1] of item; in, out: 0..n-1; in =0; out= 0; /* shared buffer = circular array */ /* Buffer empty if in == out */ /* Buffer full if (in+1) mod n == out */ /* noop means do nothing */ 55Bounded Buffer - Shared Memory Solution Producer process - creates filled buffers repeat produce an item in nextp while (( in+1) mod n = = out) do noop; buffer[in] = nextp; in= (in+1 )mod n; until false; 56Consumer process - Empties filled buffers repeat while (in = =out) do noop; nextc = buffer[out] ; out= out+1 mod n; consume the next item in nextc until false 57Bounded Buffer - Shared Memory Solution