Presentation is loading. Please wait.

Presentation is loading. Please wait.

CT213 - Computing systems Organization

Similar presentations


Presentation on theme: "CT213 - Computing systems Organization"— Presentation transcript:

1 CT213 - Computing systems Organization
Process Management CT213 - Computing systems Organization

2 Content Process Manager Process – User perspective
Process – Operating system perspective Threads Operating system services for process management UNIX Process Management Win 2k Process and Thread Management Linux Process and Thread Management

3 What is a processes A process is a program in execution Components:
Object program to be executed (called the program text in Unix) The input data to be processed (obtained from a file, interactively or by other means) and the results it produces Resources required by the program (i.e. files, etc..) PCB – Process Control Block, containing the state of the process in execution. Clear distinction between a program and a process: Program – static entity made up of source program language statements that define process behavior when executed on a set of data Process – dynamic entity that executes a program on a particular set of data using resources allocated by the system; two or more processes could execute the same program, each using its own data and resources

4 What is a process In order to execute, a process needs an Abstract Machine Environment to manage its use of resources PCB is required to map the environment state on the physical machine state i.e. part of the PCB is the location of the current instruction in the object program that is being executed; this is useful information to load the PC of the processor, whenever the process gains control of the processor. The OS keeps a process descriptor for each process

5 Program execution Each execution of the program generates a process that is executed Non reentrant The data and code (text) address space of each process (user) are different Reentrant The data address space are unique for each process The code (text) is the same for all the processes Inter-process relationships: Competition – processes are trying to get access to different resources of the system, therefore a protection between processes is necessary Cooperation – sometime the processes need to communicate between themselves and exchange information – synchronization is needed

6 Process Manager Functions
Implements CPU sharing (called scheduling) Must allocate resources to processes in conformance with certain policies Implements process synchronization and inter-process communication Implements deadlock strategies and protection mechanisms

7 Process Manager

8 Process – user perspective
Consider an application that monitors a physical industrial process, to record the operation parameters. Program modules: Data acquisition (collect): Reads 3 values from an A/D converter, that can provide a value every T interval, signaling that the new value is ready using an interrupt 1/4T processor time, 3/4T is wait time to finish read op from converter Data storage (log) Writes on the disk the 3 values read by collect, doing two write operations, the end of which being signaled by an interrupt: 1/2 T processor time and 3/2T wait time to finish the write operation Statistical processing (stat) Statistical processing of the three values collected by collect, needs 2T Print results (report): Prints two values resulted from statistical processing (stat) Each print op requires 1/4T processor time and 5/4 wait time to finish print op

9 Sequential implementation
main(){ while (TRUE){ collect(); log(); stat(); report(); } The time required for a cycle is 12T: 4.25T is required for processing time (processor time) 7.75T is wait time between various I/O operations

10 Multitasking implementation
The following processes will be executed in a cvasi-parallel fashion, with the following priorities: Log Collect Report Stat For a correct functionality, the processes need to synchronize to each other; this will be done with directives wait/signal: Wait – wait for a signal from a specific process Signal – send a signal to a specific process

11 void log(){ while(TRUE){ wait(collect); log_disk(); signal (collect); } void report(){ while (TRUE){ wait (stat); report_pr (); signal (stat); } void collect(){ while(TRUE){ wait (log); wait (stat); collect_ad (); signal (log); signal (stat); } void stat(){ while (TRUE){ wait (collect); wait (report); stat_ad (); signal (collect); signal (report) }

12 5.25 T for the execution of a complete cycle
main(){ init_proc(&log(), …); init_proc(&collect(), …); init_proc(&report(), …); init_proc(&stat(),…); signal (collect); signal (collect); signal (stat); start_schedule(); } 5.25 T for the execution of a complete cycle Only 1T lost in waiting time between I/O operations

13 Process – OS perspective
The principal function of a processor is to execute machine instructions residing in main memory Those instructions are provided in the form of programs A processor may interleave the execution of a number of programs over time Program View Its execution involves a sequence of instructions within that program The behavior of individual process can be characterized by a list of the sequence of instructions – trace of the process Processor View Executes instructions from main memory, as dictated by changing values in the program counter register The behavior of the processor can be characterized by showing how the traces of various processes are interleaved

14 Process – OS perspective
Consider three processes: A, B and C that are loaded in memory; In addition there is a small dispatcher program that switches the processor from one process to another No use of virtual memory Process B invokes an I/O operation in the fourth instruction First 12 instructions in process A and B are shown Operating system allows a process to monopolize the processor for only 6 instructions

15 Memory Layout Snapshot at instruction cycle 13

16 Traces of Processes A, B and C (Processes View)

17 Combined Trace of Processes (Processor View)

18 Two State Process Model
Simple two state process model The process can be in one of two states: running or not running When the OS creates a new process, it enters it into the Not Running state; after that, the process exists, is known to the OS and waits for the opportunity to run From time to time, the currently running process will be interrupted and the dispatcher process will select a new process to run The new process will be moved to Running state and the former one to Not Running state The operating system main responsibility is to control the execution of processes. In other words, it is suppose to determine the interleaving pattern for execution and allocating resources to the process. A simple model with two states per process can be used to describe the behavior of a process. The state transition diagram of a process is presented in this slide. When the OS creates a new process, it enters it into the Not Running state; after that, the process exists, is known to the OS and waits for the opportunity to run From time to time, the currently running process will be interrupted and the dispatcher process will select a new process to run; The former process is brought into Not Running state, and the new process will be moved to Running state.

19 Queuing Discipline Each process needs to be represented
Info relating to each process, including current state and location in memory Waiting processes should be kept in some sort of queue List of pointers to processes blocks Linked list of data blocks; each block representing a process Dispatcher behavior: An interrupted process is transfred in waiting queue If process is completed or aborted, it is discarded The dispatcher selects a process from the queue to execute From the two states process model, we already start to appreciate some of design elements of an Operating System. Each of the processes needs to be represented in some way, so the operating system can keep track of it. There must be some information rating to each process, i.e. current state and location in memory. Processes that are not running must be kept in some sort of queue, waiting their turn to execute.

20 Process Creation Creation of new process:
The OS builds the data structures that are used to manage the process The OS allocates space in main memory to the prcess Reasons for process creation: New batch job Interactive logon Created by OS to provide a service i.e. process to control printing Spawned by existing process i.e. to exploit parallelism Regardless of the used model, the life of a process is bounded by its creation and its termination

21 Process Termination Reasons for process termination
Process finished its execution (natural completion) Total time limit exceeded Errors (memory unavailable, aritmetic error, protection error, invalid instruction, privilegied instruction, I/O failure, etc…) Parent termination When the parent terminates, the OS may automatically terminate all of its children Parent request A parent has typically the right to request a child termination

22 Resource manager Reusable resources - resources that can be allocated and must be returned to the system after the process has finished; fixed, finite number (such as a block of memory) Consumable resources – abstract resources such as data input; unbounded number; a process can create consumable resource (such as messages) by releasing one or more units of the resources; a receiver process will be queued on that resource type; if there is no resource of that type, then the resource count is 0 and the process is blocked on the resource; when another process sends an message, the message handling code release a unit of the resource type to the resource manager; the resource manager can allocate the unit of resource to the requesting process so that it can proceed in ready state to compete for the CPU

23 Five State Model Running – The process is currently being executed
For single processor systems, one single process can be in this state at a time Ready – a process that is prepared to execute when given the turn Blocked – a process that can’t execute until some event occurs Such as the completion of an I/O operation New – a process that has been created, but not yet accepted in the pool of executable processes by OS Typically, a new process has not yet been loaded into main memory Exit – a process that has been released from the pool of executable processes by the OS Completed or due to some errors If all processes were always ready to execute, than the queuing discipline suggested so far would be effective. The queue was first in first out and the processor operates in round robin fashion on the available processes (each process in the queue it is given an certain amount of time, in turn, to execute and then returned to the queue). The two state model is in fact not accurate, because some of the processes in the Not Running state are ready to execute, while some others are suspended, waiting for the completion of an I/O operation.

24 Five State Model Process Transition Diagram
The process manager uses the state diagram to determine what type of service to provide to the process. If process is in ready state then it is competing for the CPU time; there is no other transition from this state but to be scheduled (or dispatched) and become active or running If process is active or running, the process can: Complete its execution, transitioning in exit state May request an I/O operation: If the requested resource is available immediately, then the execution can continue If the requested resource is not immediate, the process manager notifies the resource manager that the process requires units of its resource; the process goes in suspended or blocked mode waiting for an resource allocation event; the scheduler/dispatcher is invoked to chose the next process to become active from the ready process list The process may be interrupted due to some external event (i.e. exceeded the allocated CPU time) and is taken into ready state If the process is in blocked state, the process can move into ready state when it is being allocated the requested resource or when an external event occurs; it then competes again for the processor

25 Our Example Movement of each process described earlier (A, B and C) among the states

26 Queuing Discipline (1) There are two queues now: ready queue and blocked queue When the process is admitted in the system, it is placed in ready queue When a process is removed from the processor, it is either placed in ready queue or in blocked queue (depending on circumstances) When an event occurs, all the processes waiting on that event are moved from blocked queue onto ready queue. When an event appears, the operating system has to scan the whole blocked queue and figure out which process is waiting on that event … this can be very inefficient in large operating systems (where the blocked queue can have a few thousands entries)

27 Queuing Discipline (2) Multiple blocked queues; one per each event
When event occurs, the entire list of processes is moved in ready queue

28 Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Swap these processes to disk to free up more memory Blocked state becomes suspend state when swapped to disk

29 Process Description What information does the operating system need to control processes and manage resource for them? Operating System Control Structures Memory Tables I/O Tables File Tables Primary Process Table Process Image User Program, user data, stack and attributes of the process

30 Operating System Control Structures
Memory tables – used to keep track of both main (real) and secondary (virtual) memory. Some of main memory is reserved for use by the operating system; the remainder is available to the processes. I/O tables are used by the operating system to manage the I/O devices. File tables are used to manage the files and accesses to the files. Primary process table is used to keep one entry per each process in the operating system. Each entry contains at least one pointer to a process image. The Process Image contains data, code, stack and process control block (attributes and information about process) associated with a process. Those tables are linked (even if not shown) or cross referenced. Memory, Files and I/O are managed in behalf of processes, so there must be some reference to those resources in the processes tables (direct or indirect references). Those tables are created whenever the operating system is initialized (so the operating system must know about the physical environment it operates in, i.e. how much physical RAM is available, etc…).

31 Memory Tables Used to keep track of both main (real) and secondary (virtual) memory. Some of main memory is reserved for use by the operating system; the remainder is available to the processes. Contain: The allocation of main memory to processes The allocation of secondary memory to processes Any protection attributes of blocks of main or virtual memory (such as which processes can access certain shared memory regions) Any information needed to manage virtual memory

32 I/O tables Are used by the operating system to manage the I/O devices
At any given time, an I/O device may be available or assigned to a particular process. If an I/O is in progress, the OS needs to know the status of the I/O operation and the location in main memory being used as the source or destination of the I/O transfer.

33 File Tables These tables provide information about:
the existence of files their location on secondary memory their current status other attributes Much of this information is maintain and managed by the File Manager, in which case the process manager has little or no knowledge of files.

34 Process Tables Primary process table is used to keep one entry per each process in the operating system. Each entry contains at least one pointer to a process image. The Process Image contains: User Data Program data that can be modified, etc… Code The sequence of instructions (program) to be executed Stack Each process has one or more stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls Process control block Data needed by the operating system to control the process (attributes and information about process)

35 Process Control Block Contains: Process Identification
Processor State Information Process Control Information

36 Process Identification
Identifiers Numeric identifiers that may be stored with the process control block include: Identifier of this process Identifier of the process that created this process (parent process) User identifier

37 Processor State Information
User-Visible Registers A user-visible register is one that may be referenced by means of the machine language that the processor executes. Typically, there are from 8 to 32 of these registers, although some RISC implementations have over 100. Control and Status Registers These are a variety of processor registers that are employed to control the operation of the processor. These include: Program counter: Contains the address of the next instruction to be fetched Condition codes: Result of the most recent arithmetic or logical operation (e.g., sign, zero, carry, equal, overflow bits) Status information: Includes interrupt enabled/disabled flags, execution mode Stack Pointers Each process has one or more last-in-first-out (LIFO) system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls. The stack pointer points to the top of the stack.

38 Process Control Information (1)
Scheduling and State Information This is information that is needed by the operating system to perform its scheduling function. Typical items of information: Process state: defines the readiness of the process to be scheduled for execution (e.g., running, ready, waiting, halted). Priority: One or more fields may be used to describe the scheduling priority of the process. In some systems, several values are required (e.g., default, current, highest-allowable) Scheduling-related information: This will depend on the scheduling algorithm used. Examples are the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running. Event: Identity of event the process is awaiting before it can be resumed

39 Process Control Information (2)
Data Structuring A process may be linked to other process in a queue, ring, or some other structure. For example, all processes in a waiting state for a particular priority level may be linked in a queue. A process may exhibit a parent-child (creator-created) relationship with another process. The process control block may contain pointers to other processes to support these structures. Interprocess Communication Various flags, signals, and messages may be associated with communication between two independent processes. Some or all of this information may be maintained in the process control block. Process Privileges Processes are granted privileges in terms of the memory that may be accessed and the types of instructions that may be executed. In addition, privileges may apply to the use of system utilities and services.

40 Process Control Information (3)
Memory Management This section may include pointers to segment and/or page tables that describe the virtual memory assigned to this process. Resource Ownership and Utilization Resources controlled by the process may be indicated, such as opened files. A history of utilization of the processor or other resources may also be included; this information may be needed by the scheduler.

41 Process Images in Virtual Memory
In this slide, each process appears as contiguous range of addresses. In an actual implementation, this may not be the case. It will depend on the memory management scheme and the way in which control structures are organized by the operating system.

42 More details on Process Creation
Assign a unique process identifier Allocate space for the process Initialize process control block Set up appropriate linkages Ex: add new process to linked list used for scheduling queue Create of expand other data structures Ex: maintain an accounting file

43 Process switch Switching the processor control from one process to another (due to occurrence and service of an event) is known as process switch Process switch stages: Event occurrence Mode switch (user->kernel) Event service (see what event and take appropriate action) Choose the next active process Save context of the current active process (PC, PSW, registers, etc..) Update PCB for current active process Update PCB lists Restore active state for next process Mode switch (kernel -> user) Process switch is very frequent in multiprogramming systems, affecting system performance, so it has to be done fast Hardware support: Multiple registers set, one per mode, no need for save / restore of user registers Existence of an address space that can be automatically mapped in protected mode and not accessible in user mode

44 Possible Reasons for Process Switch
Clock interrupt process has executed for the maximum allowable time slice I/O interrupt Memory fault memory address is in virtual memory so it must be brought into main memory Trap error occurred may cause process to be moved to Exit state

45 Threads and Processes A process is defined sometime a heavyweight process; a thread is defined as a lightweight process; Separate two ideas: Process: Ownership of memory, files, other resources Thread: Unit of execution we use to dispatch Multithreading Allow multiple threads per process

46 Threads (1) It is a unit of computation associated with a particular heavyweight process, using many of the associated process’s resources (has a minimum of internal state and a minimum of allocated resources) A group of threads are sharing the same resources (files, memory space, etc) The process is the execution environment for a family of threads; a process with one thread is a classic process A thread belongs only to one process

47 Threads (2) Individual execution state
Each thread has a control block, with a state (Running/Blocked/etc.), saved registers, instruction pointer Separate stack and hardware state (PC, registers, PSW, etc) per thread Shares memory and files with other threads that are in that process Faster to create a thread than a process Because the a family of threads belonging to the same process have common resources, the thread switch is very efficient Thread switch for threads from different processes is as complex as classic process switch

48 Threads The threads are using many of the associated heavyweight process’s resources (especially the object program code – instructions) Widely used in real time operating systems and modern operating systems

49 Using threads

50 Process management services
create (&process_id, attributes) Creates a new process with implicit or specified attributes delete (process_id) Sometime known as destroy, terminate or exit Finishes the process specified by process_id Whenever the process is terminated, all the files are closed, all the allocated resources are released abort (process_id) same as the delete but for abnormal termination Usually generates an :post mortem dump” which contains the state of the process before the abnormal termination suspend (process_id) Determines the specified process to go in suspended state

51 Process management services…
resume (process_id) Determines the specified process to go from the suspended state in ready state delay (process_id, time) Same with sleep Suspends the specified process for the specified period of time After the delay time elapses, the process is brought to ready state get_attributes (process_id, &buffer_attributes) Used to find out the attributes for the given process set_attributes (process_id, buffer_attributes) Used to set the attributes of the specified process

52 References “Operating Systems”, William Stallings, ISBN 0-13-032986-x
“Operating Systems – A modern perspective”, Garry Nutt, ISBN

53 Additional slides

54 UNIX Process States Unix employs two RUNNING states to indicate whether the process is executing in user mode or kernel mode.

55 Preempted and Ready to run, in memory are nearly identical
A process may be preempted for a higher-priority process at the end of a system call. Preemption can only occur when a process is about to move from kernel mode to user mode. While a process is running in kernel mode, it may not be preempted. This makes UNIX unsuitable for real time processing. Zombie – Saves information to be passed to the parent of this process. Two processes are unique in UNIX: Process 0 – Swapper, created at boot time Process 1 – Init, creates other processes and it is spawned by process 0.

56 UNIX – Process Image User Level Context Register Context
System Level Context

57 Unix – User Level Context
Contains the basic elements of a user’s program and it is usually generated from a compiled object file User Code Read only and is intended to hold the program's instructions User Data Data accessible and processed by this process User Stack Used while executing in user mode for procedure calls and returns and parameter passing Shared Memory Data area shared with other processes; there is just one physical copy of shared area

58 UNIX – Register Context
When a process is not running, the processor status information is stored in the register context area Program Counter May be in either user or kernel space PSW (Processor Status Word) Stack Pointer Points to top of user/kernel stack General Registers

59 UNIX – System Level Context
Contains the remaining information that the operating system needs to manage the process Contains A static part – stays at the same size during a process lifetime Process table entry U area Per process region table Used by the memory management system (contains virtual memory info) A dynamic part Kernel Stack - this stack is used when the process is executing in kernel mode and contains information that must be saved and restored as procedure calls and interrupts occur Process table entry and the U area are detailed next.

60 UNIX - Process Table Entry
Status – Current state of a process Pointers to U area and user code/data Process size Identifiers (real/effective user/group id) Process/Parent ID Event Descriptor Signal – Signals sent but not handled Priority Timers - process execution time, user-set alarm Memory status – Is it swapped out? This process table is maintained by the operating system, with one entry per process. It contains process control information that is accessible to the kernel at all times; hence in a virtual memory system, all process table entries are maintained in the main memory (to be fast accessible).

61 UNIX – U Area Identifiers (real/effective user/group id)
Timers (time spent in user/kernel mode) Signal Handler array Control terminal (if it exists) System call return value System call errors I/O and File parameters File Descriptor information Permission Mode Field Limit on process size The user area contains additional control information that is needed by the kernel when it is executing in the context of this process. The distinction between the process table entry and the U area reflects the fact that UNIX kernel always executes in the context of some process. Much of the time, the kernel will deal with concerns of that process. However, some of the time, such as when scheduling is performed, it will need access to information about other processes.

62 UNIX – Process Creation
By means of system call fork() When a fork is issues, the OS performs the following operations: Allocates a slot in the process table for the new process Assigns a unique process ID to the child process Makes a copy of the process image of the parent With the exception of any shared memory Increments counters for any files owned by the parent Assigns the child process to a Ready to Run state It returns the ID number of the child to the process parent a value 0 to the child process All of this happens in the parent process, in kernel mode. When the kernel has completed those functions, it will one of the following things: Will return to user mode in the parent process, at the point of fork() Transfer control to the child process. The child code will start executing at the return point from the fork call Transfer control to another process. Both parent and the child are left in the Ready to Run stte.

63 Windows 2k Processes Characteristics of Processes
Implemented as objects May contain one or more threads Both processes and threads have built-in synchronization capabilities Characteristics of Processes Implemented as objects May contain one or more threads Both processes and threads have built-in synchronization capabilities

64 Windows 2k Process and Thread Objects
Each process is represented by an object (in the slide). Each process is defined by a number of attributes and encapsulates a number of actions (services) that it may perform. A process will perform a service by receiving a message (the only way of invoking such a service is by means of messages). Each process must contain at least one thread. The thread object structure is represented in the slide. Multithreading Threads in the same process can execute concurrently Symmetric MultiProcessor Support Any thread (including kernel threads) can run on any processor Soft affinity – Try to reschedule a thread on the same processor Hard affinity – Restrict a thread to certain processors

65 Windows Process and Thread Management
Thread States: Ready – Able to run Standby – Scheduled to run Running Waiting – Blocked or suspended Transition – Not blocked, but can’t run (paged out of memory) Terminated Support for O.S. Subsystem Process creation Begins with request from application Goes to protected subsystem Passed to executive, returns handle Win32 use handle to create thread Return process/thread information Win client requests a new thread Thread inherits limits, etc. from parent Windows 2k Thread States

66 Linux Process Management
Task structure maintained for each process State (executing, ready, zombie, etc.) Scheduling information Process, user, group identifiers Inter-process communication info Links to parent, siblings, children Timers (time used, interval timer) File system – Pointers to open files Virtual memory assigned to this process Processor-specific context

67 Linux Process State Running – either executing or ready to execute (made out of two states) Interruptible – this is a blocked state, in which the process is waiting for an event, such as the end of an I/O operation, the availability of a resource or a signal from another process Uninterruptible – this is another blocked state. The difference between this and the previous is that here a process is waiting directly on hardware conditions and therefore will not accept any signals. Stopped – the process has been halted and can be resumed only by positive action from another process (i.e. a process that is being debugged can be stopped by another process- the debugger). Zombie – process has been terminated but still must have its task structure in the task list (process table).

68 Linux Threads Threads are implemented as processes that share files, virtual memory, signals, etc. clone() system call to create a thread pthread library provides more user-friendly thread support


Download ppt "CT213 - Computing systems Organization"

Similar presentations


Ads by Google