processes. a process is a program in execution; process execution must progress in sequential...

41
CSSE 332-01 Operating Systems Processes

Post on 21-Dec-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • Processes
  • Slide 2
  • A process is a program in execution; process execution must progress in sequential fashion. Can be characterized by its trace. The OS interleaves the execution of several processes to maximize processor utilization A process requires resources, which are managed by the operating system OS supports Inter-Process Communication (IPC) and user creation of processes 2
  • Slide 3
  • Trace of Process Sequence of instructions that execute for a process Dispatcher switches the processor from one process to another 3
  • Slide 4
  • 4 Time slice ~= 1/10 th of a second
  • Slide 5
  • Process state models The OS is responsible for interleaving the execution of the processes. It is important to understand the possible behaviors of all the processes and how to react to them. The behavior of processes is described by means of the process state model. 5
  • Slide 6
  • Two-State Process Model Process may be in one of two states Running Not-running 6
  • Slide 7
  • Queuing Diagram Dispatcher Program that assigns the processor to one process or another Prevents a single process from monopolizing processor time 7 Processor Enter Queue Dispatch Exit Pause (a) Queuing diagram
  • Slide 8
  • Reason for Process Creation 8
  • Slide 9
  • Reasons for Process Termination 9
  • Slide 10
  • 10
  • Slide 11
  • 5 Process States As a process executes, it changes state New: The process is being created. Running: Instructions are being executed. Waiting or blocked: The process is waiting for some event to occur. Ready: The process is waiting to be assigned to a process. Terminate or Exit: The process has finished execution. 11
  • Slide 12
  • Diagram of Process State 12
  • Slide 13
  • Example for 3 Processes 13
  • Slide 14
  • OS Control Structures
  • Slide 15
  • Process Control Block (PCB) Information associated with each process. Process ID Process state Program counter (PC) CPU registers (Context data) CPU scheduling information (Priority) Memory-management information (Memory pointer) Accounting information (Time, acct. #) I/O status information (requests, devices assigned) 15
  • Slide 16
  • Process Control Block Contains the process attributes Created and manage by the operating system Allows support for multiple processes Process = Program code + Data + PCB 16
  • Slide 17
  • Suspending Processes 17
  • Slide 18
  • Two Suspended States 18
  • Slide 19
  • Process Scheduling Queues 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. Process migration between the various queues. 19
  • Slide 20
  • Ready Queue, I/O Device Queues 20
  • Slide 21
  • Process Scheduling 21
  • Slide 22
  • Schedulers Long-term scheduler: job scheduler selects which processes should be brought into the ready queue. invoked infrequently (seconds, minutes) controls the degree of multiprogramming Medium-term scheduler allocates memory for process. invoked periodically or as needed. Short-term scheduler: CPU scheduler selects which process should be executed next and allocates CPU. invoked frequently (ms) 22
  • Slide 23
  • Process Switching Definition The activity that occurs when the OS kernel switches between processes in an effort to share the CPU among competing, runable processes Actions Save contents of hardware registers (PC, SP,...) Load PC with location of resume point Load hardware registers with new context if full context switch 23
  • Slide 24
  • Process Switching Process-switch time is overhead CPU utilization affected by quantum and process-switch time Quantum: Time-slice (max CPU interval) given to a process Time dependent on hardware support Types Voluntary: Process blocks Involuntary: End of time quantum or higher-priority, runable process gets control of CPU 24
  • Slide 25
  • CPU Process Switch 25
  • Slide 26
  • Mode switch vs Process switch When a mode switch occurs, the PCB of the process that was running is still marked Running and is not placed in the Blocked or Ready queue However, depending on what caused the mode switch, a process switch could follow 26
  • Slide 27
  • Modes of execution User mode: Some parts of virtual address space can not be accessed Some instructions (e.g., memory management) can not be executed Kernel mode: Can access kernel address space Is a fixed part of the virtual address space of every process System call puts user into kernel mode 27
  • Slide 28
  • Mode, Space and Context 28 Application (user space) System calls Exceptions (user + system space) X not allowed Interrupts, System tasks (user space) Kernel Mode More privileges User Mode Process Context Kernel Context System call Interrupt occurs
  • Slide 29
  • Execution of the OS 29
  • Slide 30
  • Execution within user processes 30
  • Slide 31
  • Unix Process Creation Parents create children results in a tree of processes Resource sharing Parent and children share all resources Children share subset of parents resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate 31
  • Slide 32
  • Unix Process Creation Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork system call creates new process exec family of system calls used after a fork to replace the process memory space with a new program 32
  • Slide 33
  • Unix Process Creation (fork) Allocate new PID and proc structure Init childs process control block (PCB) Copy parents registers to childs hardware context Copy parents process image to childs Mark child runnable and give to scheduler Return PID = 0 to child Return child PID to parent 33
  • Slide 34
  • Unix Process Creation (fork) Effects of fork Child gets a copy of its parents memory BUT changes made by child are not reflected in parent EXCEPT a child can affect the I/O state of parent File descriptors can point to same file table entry WARNING: Parent should fflush(stdout) before fork if using printf() 34
  • Slide 35
  • exec Operations Use to overlay process image with new program Find executable file (argv[0]) Copy exec args and env variables to kernel space Free memory currently allocated to process image Allocate new memory for process image Copy exec args and env variables into new space Begin executing in main() 35
  • Slide 36
  • exec Functions Six exec functions: execl execv execle execve execlp execvp 36 l list of arguments v argv[] vector p file name e envp[] vector instead of inheriting environment of parent
  • Slide 37
  • waitpid pid_t waitpid(pid_t pid, int *status, int options); Unlike wait(2), waitpid doesnt have to block wait(2) blocks until one of its children terminates 37
  • Slide 38
  • waitpid pid parameter pid == -1: Wait for any child to terminate pid > 0: Wait for child whose PID equals pid pid == 0: Wait for any child whose PGID equals the PGID of process pid pid < -1: Wait for any child whose PGID = abs(pid) 38
  • Slide 39
  • waitpid status parameter Contains exit status, signal number, and flags WIFEXITED(status) True if child terminated normally Ex: printf(exit = %d\n, WEXITSTATUS(status)); WIFSIGNALED(status) True if child terminated and didnt catch signal Ex: printf(signo = %d\n, WTERMSIG(s)); WIFSTOPPED(status) True if child is STOPPED Ex: printf(signo = %d\n, WSTOPSIG(s)); 39
  • Slide 40
  • waitpid options parameter options = 0 No special handling options = WNOHANG Dont block if child is not available and return 0 Ex: rc = waitpid(pid, &status, WNOHANG); if (rc == -1) {... error... } else if (rc == 0) {... no child available...} else {... rc should equal pid... } 40
  • Slide 41
  • waitpid options options = WUNTRACED Return status of child if child stopped and status has not already been returned (assumes job control support) 41