Chapter 3 Process Description and Control The concept

  • Slides: 33
Download presentation
Chapter 3 Process Description and Control The concept of process is fundamental to the

Chapter 3 Process Description and Control The concept of process is fundamental to the structure of modern computer operating systems. Its evolution in analyzing problems of synchronization, deadlock, and scheduling in operating systems has been a major intellectual contribution of computer science. Research Study, MIT Press, 1980

CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview

CS 345 Stalling’s Chapter # Project 1: Computer System Overview 2: Operating System Overview 4 P 1: Shell 3: Process Description and Control 4: Threads 4 P 2: Tasking 5: Concurrency: ME and Synchronization 6: Concurrency: Deadlock and Starvation 6 P 3: Jurassic Park 7: Memory Management 8: Virtual memory 6 P 4: Virtual Memory 9: Uniprocessor Scheduling 10: Multiprocessor and Real-Time Scheduling 6 P 5: Scheduling 11: I/O Management and Disk Scheduling 12: File Management 8 P 6: FAT Student Presentations 6 BYU CS 345 Chapter 3 - Processes 2

Chapter 3 Learning Objectives n n n n Define the term process and explain

Chapter 3 Learning Objectives n n n n Define the term process and explain the relationship between processes and process control blocks. Explain the concept of a process state and discuss the state transitions the processes undergo. List and describe the purpose of the data structures and data structure elements used by an OS to manage processes. Assess the requirements for process control by the OS. Understand the issues involved in the execution of OS code. Assess the key security issues that relate to operation systems. Describe the process management scheme for UNIX SVR 4. BYU CS 345 Chapter 3 - Processes 3

Large Building Project Identify Builder (General Contractor) Operating System Materials Scheduling Workers Resources Building

Large Building Project Identify Builder (General Contractor) Operating System Materials Scheduling Workers Resources Building Threads Process What needs to be Managed? BYU CS 345 Computer Data Context Programs Interrupts Chapter 3 - Processes 2 State 5 state States Tools Libraries New Working Waiting Blocked Suspended Completed 4

Questions… 1. 2. 3. 4. 5. What is a process? When is a process

Questions… 1. 2. 3. 4. 5. What is a process? When is a process created? When is a process terminated? What is a 2 -state scheduling model? What additional states are added with a 5 -state model? 6. How does a suspended process differ from a blocked process? 7. What task variables are found in a Task Control Table? BYU CS 345 Chapter 3 - Processes 5

Process 1. What is a Process (or Task)? n Sequence of instructions that executes

Process 1. What is a Process (or Task)? n Sequence of instructions that executes n n n The entity that can be assigned to and executed on a processor A unit of activity characterized by a single sequential thread of execution Can be traced Associated data needed by the program Context n n All information the operating system needs to manage the process A current state and an associated set of system resources BYU CS 345 Chapter 3 - Processes 6

Process OS Process Support? n Assist the execution of a process n n Allocate

Process OS Process Support? n Assist the execution of a process n n Allocate resources to processes n n n interleave the execution of several processes maximize processor utilization provide reasonable response time fairness avoid starvation / deadlock Support interprocess activities n n communication user creation of processes BYU CS 345 Chapter 3 - Processes 7

Process Implementation Process Control Blocks Active Context Process BYU CS 345 Chapter 3 -

Process Implementation Process Control Blocks Active Context Process BYU CS 345 Chapter 3 - Processes 8

Process Trace An instruction trace reveals the overhead required to multi-process. BYU CS 345

Process Trace An instruction trace reveals the overhead required to multi-process. BYU CS 345 Chapter 3 - Processes 9

Process Creation 2. When is a Process Created? n n Submission of a batch

Process Creation 2. When is a Process Created? n n Submission of a batch job User logs on Created to provide a service such as printing Process creates another process n n Modularity Parallelism Parent – child relationship Deciding how to allocate the resources is a policy that is determined by the OS BYU CS 345 Chapter 3 - Processes 10

Process Creation Decisions n Resource Allocation n Execution n Treat as a new process

Process Creation Decisions n Resource Allocation n Execution n Treat as a new process Divide parent’s resources among children child runs concurrently with parent waits until some or all children terminate Address Space n n copy of parent new program loaded into address space BYU CS 345 Chapter 3 - Processes 11

Process Creation Example: Unix Process Creation n n A new process is created by

Process Creation Example: Unix Process Creation n n A new process is created by the fork call Child and parent are identical n n Both parent and child execute next line Often the child executes an exec n n child returns a 0 parent returns nonzero creates a brand new process in its space Parent can execute a wait BYU CS 345 Chapter 3 - Processes 12

Process Creation Unix Example fork() returns: -1 = error 0 = child >0 =

Process Creation Unix Example fork() returns: -1 = error 0 = child >0 = parent switch (child 1 = fork()) { case – 1: printf("Can't fork"); exit(-1); case 0: child 1 P(one 2 two, two 2 one); // child 1 exit(-2); default: switch (child 2 = fork()) // parent { case – 1: printf("Can't fork"); exit(-1); case 0: child 2 P(one 2 two, two 2 one); waitpid() joins exit(-3); parent & child default: // Wait for child two waitpid(child 2, status 2, options); } /* Wait for child one */ waitpid(child 1, status 1, options); fflush(stdout); } BYU CS 345 Chapter 3 - Processes 13

Process Creation Windows NT process creation n n Windows subsystem has no understanding of

Process Creation Windows NT process creation n n Windows subsystem has no understanding of parent / child relationship. Process created by Create. Process system call n n n Similar to fork-execve model No process group concept Child executes concurrently with parent Loads a program into the address space of the child process Processes are objects and have handles n Parent uses Wait. For. Single. Object() function to wait for child process termination. BYU CS 345 Chapter 3 - Processes 14

Process Creation Windows NT Example // Now create the child process. PROCESS_INFORMATION pi. Proc.

Process Creation Windows NT Example // Now create the child process. PROCESS_INFORMATION pi. Proc. Info; STARTUPINFO si. Start. Info; // Set up members of STARTUPINFO structure. Zero. Memory( &si. Start. Info, sizeof(STARTUPINFO) ); si. Start. Info. cb = sizeof(STARTUPINFO); // Create the child process. Create. Process(NULL, program, // command line NULL, // process security attributes NULL, // primary thread security attributes TRUE, // handles are inherited 0, // creation flags NULL, // use parent's environment NULL, // use parent's current directory &si. Start. Info, // STARTUPINFO pointer &pi. Proc. Info); // receives PROCESS_INFORMATION BYU CS 345 Chapter 3 - Processes 15

Process Termination 3. When is a Process Terminated? n n n n User logs

Process Termination 3. When is a Process Terminated? n n n n User logs off Normal completion Batch issues Halt Time limit exceeded Memory unavailable Bounds violation Protection error n n event timeout BYU CS 345 I/O failure Invalid instruction n n n tried to execute data Privileged instruction Data misuse Operating system intervention n example write to read-only file Arithmetic error Time overrun n n such as when deadlock occurs Parent terminates so child processes terminate Parent request Chapter 3 - Processes 16

2 -State Model 4. What is a 2 -State Scheduling Model? n Process may

2 -State Model 4. What is a 2 -State Scheduling Model? n Process may be in one of two states n n Running Not-running BYU CS 345 Chapter 3 - Processes 17

2 -State Model Two-state Model Problems n What does not-running mean? n n n

2 -State Model Two-state Model Problems n What does not-running mean? n n n n Ready to execute? Blocked? Suspended? Terminated? Priority? Scheduler/dispatcher cannot just select the process that has been in the queue the longest. Challenge to make overall system as “efficient” and “fair” as possible. BYU CS 345 Chapter 3 - Processes 18

5 -State Model 5. What is a 5 -State Scheduling Model? BYU CS 345

5 -State Model 5. What is a 5 -State Scheduling Model? BYU CS 345 Chapter 3 - Processes 19

5 -State Model Five-state Model Transitions n n n n n Null ® New:

5 -State Model Five-state Model Transitions n n n n n Null ® New: Process is created New ® Ready: O. S. is ready to handle another process (Admit) Ready ® Running: Select another process to run (Dispatch) Running ® Exit: Process has terminated (Release) Running ® Ready: End of time slice or higher-priority process is ready (Timeout) Running ® Blocked: Process waiting for event (Event Wait) Blocked ® Ready: The event a process is waiting for has occurred, can continue (Event Occurs) Ready ® Exit: Process terminated by O. S. or parent Blocked ® Exit: Same reasons BYU CS 345 Chapter 3 - Processes 20

5 -State Model Multiple Blocked Queues BYU CS 345 Chapter 3 - Processes 21

5 -State Model Multiple Blocked Queues BYU CS 345 Chapter 3 - Processes 21

P 2 - Tasking 5 -State Scheduler SWAP SEM_WAIT(s) SEM_SIGNAL(s) SEM_TRYLOCK(s) create. Task() New

P 2 - Tasking 5 -State Scheduler SWAP SEM_WAIT(s) SEM_SIGNAL(s) SEM_TRYLOCK(s) create. Task() New swap. Task(); sem. Wait(s); sem. Signal(s); sem. Try. Lock(s); dispatch() Ready Queue kill. Task() Running Exit swap. Task() l() na ig m. S se Blocked Queues s semem. W Try ait( Lo ) ck () #define Project 2 - Tasking BYU CS 345 Chapter 3 - Processes 22

Suspended Process 6. What is a Suspended Process? n n Processor is faster than

Suspended Process 6. What is a Suspended Process? n n 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 suspended state when swapped to disk Two new states n n Blocked, suspend Ready, suspend BYU CS 345 Chapter 3 - Processes 23

Suspended Process Suspended State Scheduling One Suspend State Two Suspend State BYU CS 345

Suspended Process Suspended State Scheduling One Suspend State Two Suspend State BYU CS 345 Chapter 3 - Processes 24

Suspended Process Reasons for Process Suspension n Swapping n n Other OS reason n

Suspended Process Reasons for Process Suspension n Swapping n n Other OS reason n n A user may wish to suspend execution of a program for purposes of debugging or in connection with the use of a resource Timing n n The OS may suspend a background or utility process or a process that is suspected of causing a problem Interactive user request n n The OS needs to release sufficient main memory to bring in a process that is ready to execute A process may be executed periodically and may be suspended while waiting for the next time interval Parent process request n A parent process may wish to suspend execution of a descendent to examine or modify the suspended process BYU CS 345 Chapter 3 - Processes 25

Chapter 3 Learning Objectives n n n n Define the term process and explain

Chapter 3 Learning Objectives n n n n Define the term process and explain the relationship between processes and process control blocks. Explain the concept of a process state and discuss the state transitions the processes undergo. List and describe the purpose of the data structures and data structure elements used by an OS to manage processes. Assess the requirements for process control by the OS. Understand the issues involved in the execution of OS code. Assess the key security issues that relate to operation systems. Describe the process management scheme for UNIX SVR 4. BYU CS 345 Chapter 3 - Processes 26

Control Tables 7. Operating System Control Tables OS Tables Task Control Blocks BYU CS

Control Tables 7. Operating System Control Tables OS Tables Task Control Blocks BYU CS 345 Chapter 3 - Processes 27

Control Tables n Memory Tables n n n Allocation of main memory to processes

Control Tables n Memory Tables n n n Allocation of main memory to processes Allocation of secondary memory to processes Protection attributes for access to shared memory regions Information needed to manage virtual memory I/O device is available or assigned n n Status of I/O operation Location in main memory being used as the source or destination of the I/O transfer BYU CS 345 Chapter 3 - Processes 28

Control Tables n File Tables n n n Existence of files Location on secondary

Control Tables n File Tables n n n Existence of files Location on secondary memory Current Status Attributes Usually maintained by a file management system Process Table n n n Process ID Process state Location in memory BYU CS 345 Chapter 3 - Processes 29

Control Tables Process Location n Process includes set of programs to be executed n

Control Tables Process Location n Process includes set of programs to be executed n n Process Control Block (PCB) n n Data locations for local and global variables Any defined constants Stack Collection of attributes Process image n Collection of program, data, stack, and attributes BYU CS 345 Chapter 3 - Processes 30

P 2 - Tasking Task Control Block (tcb) // task control block typedef struct

P 2 - Tasking Task Control Block (tcb) // task control block typedef struct { char* name; int (*task)(int, char**); int state; int priority; int argc; char** argv; int signal; void (*sig. Cont. Handler)(void); void (*sig. Int. Handler)(void); void (*sig. Term. Handler)(void); void (*sig. Tstp. Handler)(void); TID parent; int RPT; int cdir; Semaphore *event; void* stack; jmp_buf context; } TCB; BYU CS 345 // // task task control block name address state priority (P 2) argument count (P 1) argument pointers (P 1) // // // task task signals (P 1) my. SIGCONT handler (P 1) my. SIGINT handler (P 1) my. SIGTERM handler (P 1) my. SIGTSTP handler (P 1) // // // task parent task root page table (P 4) task directory (P 6) blocked task semaphore (P 2) task stack (P 2) task context pointer (P 2) Chapter 3 - Processes 31

P 2 - Tasking Step 1: Priority Queue n Create a priority queue: n

P 2 - Tasking Step 1: Priority Queue n Create a priority queue: n n n typedef uint 8_t TID; // task ID (0 -255) typedef int 16_t PQEntry; // task priority/ID typedef PQEntry* PQueue; // priority queue PQueue rq; // ready queue rq = (PQEntry*)malloc(MAX_TASKS * sizeof(PQEntry)); rq[0] = 0; // init ready queue Suggested priority queue functions: n TID en. Q(PQueue q, PQEntry pqe); q pqe TID n priority queue (# | pr 1/tid 1 | pr 2/tid 2 | …) (task priority << 8) + task id returned task id PQEntry de. Q(PQueue q, TID tid); q tid priority queue if (0 xff) then {de. Q q[q[0]]} else {de. Q tid} PQEntry returned de. Q’d entry if (-1) then {tid not found or empty q} BYU CS 345 Chapter 3 - Processes Priority/TID # of entries rq[5] rq[4] 10 / 3 rq[3] 5/2 rq[2] 5/0 rq[1] 2/1 rq[0] 4 32

BYU CS 345 Chapter 3 - Processes 33

BYU CS 345 Chapter 3 - Processes 33