Lecture 4 Processes Operating System Spring 2007 Chapter

  • Slides: 57
Download presentation
Lecture 4: Processes Operating System Spring 2007 Chapter 4 of textbook 1

Lecture 4: Processes Operating System Spring 2007 Chapter 4 of textbook 1

Major Requirements of an Operating System n n n Interleave the execution of several

Major Requirements of an Operating System n n n Interleave the execution of several processes to maximize processor utilization while providing reasonable response time Allocate resources to processes in conformance with a specific policy while at the same time avoiding deadlock Support interprocess communication and user creation of processes, both of which may aid in the structuring of applications 2

Contents n n n Process Process Definition States Scheduling Description Control and Operations 3

Contents n n n Process Process Definition States Scheduling Description Control and Operations 3

Contents n n n Process Process Definition States Scheduling Description Control and Operations 4

Contents n n n Process Process Definition States Scheduling Description Control and Operations 4

Processes - Definition n Also called a job Execution of an individual program Process

Processes - Definition n Also called a job Execution of an individual program Process components: n n An executable program – text section The associated data needed by the program n n Stack – temporary data (e. g. function parameters, return addresses, and local variables) Data section – global variables Heap – memory which is dynamically allocated during process run time The execution context of the program n n All information the operating system needs to manage the process Including the value of program counter and the contents of the processor’s registers 5

Process in memory 6

Process in memory 6

Process Trace n Processes can be traced n n For a program to be

Process Trace n Processes can be traced n n For a program to be executed, a process is created for that program. We can characterize the behavior of an individual process by listing the sequence of instructions that execute for that process. Such a listing is called a trace of the process. We can characterizing behavior of the processor by showing how the traces of the various processes are interleaved. 7

Example for processes tracing 8

Example for processes tracing 8

Process A OS Process B OS Process C OS Process A OS Process C

Process A OS Process B OS Process C OS Process A OS Process C time 9

Example for processes tracing (cont. ) 10

Example for processes tracing (cont. ) 10

Contents n n n Process Process Definition States Scheduling Description Control and Operations 11

Contents n n n Process Process Definition States Scheduling Description Control and Operations 11

Process State - Two-State Process Model n Process may be in one of two

Process State - Two-State Process Model n Process may be in one of two states n n Running Not-running 12

Process State n Not-running n n Waiting (also called blocked) n n ready to

Process State n Not-running n n Waiting (also called blocked) n n ready to execute waiting for I/O Dispatcher cannot just select the process that has been the longest in the queue because it may be blocked 13

Process State - A Five-State Model n n n New: The process is being

Process State - A Five-State Model n n n New: The process is being created Running: Instructions are being executed Waiting (blocked): 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 14

Process State - A Five-State Model 15

Process State - A Five-State Model 15

Queueing-Diagram Representation of Five-State Model 16

Queueing-Diagram Representation of Five-State Model 16

Queueing-Diagram Representation of Five-State Model 17

Queueing-Diagram Representation of Five-State Model 17

Suspended Process – The Need for Swapping (not required in this class) n n

Suspended Process – The Need for Swapping (not required in this class) n n n The three principal states just described (Ready, Running, Waiting/Blocked) provide a systematic way of modeling the behavior of processes and guide the implementation of the OS. However, there is good justification for adding other states to the model – the need for swapping 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 Waiting(Blocked) state becomes suspend state when swapped to disk Two new states n n Waiting(Blocked), suspend Ready, suspend 18

Contents n n n Process Process Definition Trace States Scheduling Description Control and Operations

Contents n n n Process Process Definition Trace States Scheduling Description Control and Operations 19

Process Scheduling Queues n n Job queue – set of all processes in the

Process Scheduling Queues n n 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 migrate among the various queues 20

Schedulers n n n Long-term scheduler (or job scheduler) – selects which processes should

Schedulers n n n Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU Not required in this class: Medium-term scheduler – corresponds to suspended state (swapping out of the memory) 21

Schedulers (Cont. ) n Short-term scheduler is invoked very frequently (milliseconds) (must be fast)

Schedulers (Cont. ) n Short-term scheduler is invoked very frequently (milliseconds) (must be fast) Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of n Processes can be described as either: n n multiprogramming n n 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 22

Contents n n n Process Process Definition States Scheduling Description Control and Operations 23

Contents n n n Process Process Definition States Scheduling Description Control and Operations 23

What information does the OS need to control processes and manage resources for them?

What information does the OS need to control processes and manage resources for them? 24

Memory Tables n n Used to keep track of both main(real) and secondary memory.

Memory Tables n n Used to keep track of both main(real) and secondary memory. Must include the following information: n n Allocation of main memory to processes. Allocation of secondary memory to processes. Protection attributes of blocks of main or virtual memory, such as which processes may access certain shared memory regions. Information needed to manage virtual memory. 25

I/O Tables n n Used by the OS to manage the I/O devices. May

I/O Tables n n Used by the OS to manage the I/O devices. May include the following information: n n n I/O device is available or assigned Status of I/O operation Location in main memory being used as the source or destination of the I/O transfer 26

File Tables n Provide information about the existence of files: n n n Existence

File Tables n Provide information about the existence of files: n n n Existence of files Location on secondary memory Current Status Attributes Sometimes this information is maintained by a file-management system 27

Process Tables n n Used to manage processes Include the following information: n Where

Process Tables n n Used to manage processes Include the following information: n Where process is located n n n Depend on the memory management scheme being used. In the simplest case, the process image is maintained as a contiguous block of memory. This block is maintained in secondary memory, usually disk. Attributes necessary for its management n n n Process ID Process state Location in memory 28

Typically Elements of a Process Image n User Data n n n User Program

Typically Elements of a Process Image n User Data n n n User Program n n The program to be executed System Stack n n n The modifiable part of the user space. May include program data, a user stack area, and programs that may be modified. Each process has one or more system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system calls. Process Control Block n Data needed by the OS to control the process. 29

Process Control Block n n n Process Identification Processor State Information Process Control Information

Process Control Block n n n Process Identification Processor State Information Process Control Information 30

Process Control Block n n n Process Identification Processor State Information Process Control Information

Process Control Block n n n Process Identification Processor State Information Process Control Information 31

Process identification n Numeric identifiers that may be stored with the process control block

Process identification n Numeric identifiers that may be stored with the process control block include n n n Identifier of this process Identifier of the process that created this process (parent process) User identifier 32

Process Control Block n n n Process Identification Processor State Information Process Control Information

Process Control Block n n n Process Identification Processor State Information Process Control Information 33

Processor State Information n n User-Visible Registers Control and Status Registers n n n

Processor State Information n n User-Visible Registers Control and Status Registers n n n PC PSW Stack Pointers n Each process has one or more system stacks associated with it. 34

Process Control Block n n n Process Identification Processor State Information Process Control Information

Process Control Block n n n Process Identification Processor State Information Process Control Information 35

Process Control Information n Scheduling and State Information: n n n Process State (e.

Process Control Information n Scheduling and State Information: n n n Process State (e. g. running, ready, waiting, etc. ) Priority Scheduling-related information n Depend on the scheduling algorithm used. e. g. amount of time it has already run, how long it has waited Event n Identity of event the process is awaiting 36

Process Control Information (cont. ) n Data Structuring n n Interprocess Communication n n

Process Control Information (cont. ) n Data Structuring n n Interprocess Communication n n A process may be linked to other processes, e. g to its parent Various flags, signals, and messages may be associated with communication between two independent processes. Process Privilege n Processes are granted privileges in terms of the memory that may be accessed and the types of instructions that may be executed. 37

Process Control Information (cont. ) n Memory Management n n Including pointers to segment

Process Control Information (cont. ) n Memory Management n n Including pointers to segment and/or page tables that describe the virtual memory assigned to this process. Resource Ownership and Utilization n n 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. 38

39

39

Contents n n n Process Process Definition States Scheduling Description Control and Operations 40

Contents n n n Process Process Definition States Scheduling Description Control and Operations 40

Process Creation n When a new process is to be added to those currently

Process Creation n When a new process is to be added to those currently being managed, the OS builds the date structures that are used to manage the process and allocates address space in main memory to the process. These actions constitute the creation of a new process. 41

Reasons for Process Creation n Submission of a new batch job n n Interactive

Reasons for Process Creation n Submission of a new batch job n n Interactive logon n n A user at a terminal logs on to the system Created by OS to provide a service such as printing n n The OS is provided with a batch job control stream usually a tape or disk The OS can create a process to perform a function on behalf of a user program Spawned by existing process n n For purposes of modularity or to exploit parallalism, a user program can dictate the creation of a number of processes. When one process spawns another process, the former is called the parent and the spawned process as the child. 42

Procedure for Process Creation n Once the OS decides to create a new process,

Procedure for Process Creation n Once the OS decides to create a new process, it can proceed as follows: (a) (b) (c) (d) Assign a unique process identifier to the new process Allocate space for the process. Initialize the process control block Set the appropriate linkage n (e) e. g. if the OS maintains each scheduling queue as a linked list, then the new process must be put in the ready or ready/suspend list Create or expand other data structures n e. g. the OS may maintain an accounting file on each process to be used subsequently for billing and/or performance assessment purpose. 43

Process Creation n n Parent process create children processes, which, in turn create other

Process Creation n n Parent process create children processes, which, in turn create other processes, forming a tree of processes Resource sharing n n Execution n Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Parent and children execute concurrently Parent waits until children terminate Address space n n Child duplicate of parent Child has a program loaded into it 44

Process Creation Example for UNIX examples: fork system call creates new process exec system

Process Creation Example for UNIX examples: fork system call creates new process exec system call used after a fork to replace the process’ memory space with a new program 45

Process Creation Example for UNIX C Program Forking Separate Process int main() { Pid_t

Process Creation Example for UNIX C Program Forking Separate Process int main() { Pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } } 46

A tree of processes on a typical Solaris 47

A tree of processes on a typical Solaris 47

Process Termination n n A batch job should include a Halt instruction or an

Process Termination n n A batch job should include a Halt instruction or an explicit OS service call for termination. User logs off For an interactive application, there are commands to terminate a process. Control C will terminate a process. Error and fault conditions 48

Reasons for Process Termination n Normal completion n n Time limit exceeded n n

Reasons for Process Termination n Normal completion n n Time limit exceeded n n The process has run longer than the specified total time limit. Memory unavailable n n The process executes an OS service call to indicate that it has completed running. The process requires more memory than the system can provide. Bounds violation n The process tries to access a memory location that it is not allowed to access. 49

Reasons for Process Termination (cont. ) n Protection error n n n Arithmetic error

Reasons for Process Termination (cont. ) n Protection error n n n Arithmetic error n n process waited longer than a specified maximum for an event I/O failure n n The process tries a prohibited computation, such as division by zero, or arithmetic overflow. Time overrun n n The process attempts to use a resource or a file that it is not allowed to use, or tries to use it in an improper fashion. Example: write to read-only file An error occurs during input or output. Privileged instruction n The process attempts to use an instruction reserved for the OS 50

Reasons for Process Termination (cont. ) n Invalid instruction n Data misuse Operating system

Reasons for Process Termination (cont. ) n Invalid instruction n Data misuse Operating system intervention n n such as when deadlock occurs Parent terminates so child processes terminate n n The process attempts to execute a nonexistent instruction (often as a result of branching into a data area and attempting to execute the data) When a parent process terminates, the OS may automatically terminate all of the child processes. Parent request n A parent process may terminate any of its child process. 51

Process Switching n A running process is interrupted and the OS assigns another process

Process Switching n A running process is interrupted and the OS assigns another process to the Running state and turns control over to that process 52

When to Switch a Process n Interrupt n Clock interrupt n n n I/O

When to Switch a Process n Interrupt n Clock interrupt n n n I/O interrupt Memory fault n n memory address is in virtual memory so it must be brought into main memory Trap n n n process has executed for the maximum allowable time slice error occurred may cause process to be moved to Exit state Supervisor call n such as file open 53

Comparison between interrupt, trap and supervisor call Mechanism Cause Use Interrupt External to the

Comparison between interrupt, trap and supervisor call Mechanism Cause Use Interrupt External to the execution of the current instruction Reaction to an asynchronous external event Trap Associated with the execution of the current instruction Explicit request Handling of an error or an exception condition Call to an OS function Supervisor call 54

Change of Process State n n Save context of processor including program counter and

Change of Process State n n Save context of processor including program counter and other registers Update the process control block of the process that is currently in the running state n n n Change the state of the process to one of the other states (Ready, waiting, or Exit, etc. ) Other relevant fields must also be updated, including the reason for leaving the Running state and accounting information Move process control block to appropriate queue Ready, Waiting, etc. 55

Change of Process State (cont. ) n n Select another process for execution Update

Change of Process State (cont. ) n n Select another process for execution Update the process control block of the process selected n n Update memory-management data structures n n Change the state of this process to Running This may be required, depending on how address translation is done Restore context of the selected process 56

End of lecture 4 Thank you! 57

End of lecture 4 Thank you! 57