Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE Operating Systems

Similar presentations


Presentation on theme: "CSSE Operating Systems"— Presentation transcript:

1 CSSE 332-01 Operating Systems
Processes

2 Processes 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

3 Trace of Process Sequence of instructions that execute for a process
Dispatcher switches the processor from one process to another

4 Time slice ~= 1/10th of a second

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.

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

7 Queuing Diagram Dispatcher
Processor Queue Enter Exit Pause (a) Queuing diagram Dispatcher Program that assigns the processor to one process or another Prevents a single process from monopolizing processor time

8 Reason for Process Creation
In response to a submission of a job in a batch process control stream. A user attempts to logon in an interactive system Created by OS to service a request, e.g. a process to manage a print request Spawned by a process (“fork”) parent and child processes.

9 Reasons for Process Termination

10 Reasons for Process Termination

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.

12 Diagram of Process State

13 Example for 3 Processes

14 OS Control Structures

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)

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 How is a process maintained by the OS? The OS uses a special data structure called a process control block (PCB) Process = program code + set of data associated with that code + PCB

17 Suspending Processes

18 Two Suspended States

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.

20 Ready Queue, I/O Device Queues

21 Process Scheduling

22 Schedulers Long-term scheduler: job scheduler Medium-term 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)

23 Process Switching Definition Actions
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

24 Process Switching Process-switch time is overhead Types
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

25 CPU Process Switch

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 ICE Question 1

27 Modes of execution User mode: Kernel 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 Example of privileged instructions fileopen() for I/O memory management instructions

28 Mode, Space and Context Application System calls Exceptions X
(user space) System calls Exceptions (user + system space) Process Context X not allowed Interrupts, System tasks (user space) Kernel Context User Mode Kernel Mode More privileges System call Interrupt occurs

29 Execution of the OS

30 Execution within user processes

31 Unix Process Creation Parents create children Resource sharing
results in a tree of processes Resource sharing Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate

32 Unix Process Creation Address space UNIX examples
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

33 Unix Process Creation (fork)
Allocate new PID and proc structure Init child’s process control block (PCB) Copy parent’s registers to child’s hardware context Copy parent’s process image to child’s Mark child runnable and give to scheduler Return PID = 0 to child Return child PID to parent See for a discussion of proc structure fork() is called once but returns twice (in the child and in the parent).

34 Unix Process Creation (fork)
Effects of fork Child gets a copy of its parent’s 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() See Advanced programming in the Unix Environment, second edition, pages 212, 213.

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()

36 exec Functions Six exec functions: execl execv execle execve execlp
execvp Argument[0] is, as a convention, the name of the executable file, but could be anyting. l  list of arguments v  argv[] vector p  file name e  envp[] vector instead of inheriting environment of parent

37 waitpid pid_t waitpid(pid_t pid, int *status, int options);
Unlike wait(2), waitpid doesn’t have to block wait(2) blocks until one of its children terminates Returns process ID id if OK, returns -1 if error, returns 0 (see later)

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) Returns process ID id if OK, returns -1 if error, returns 0 (see later)

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 didn’t catch signal Ex: printf(“signo = %d\n”, WTERMSIG(s)); WIFSTOPPED(status) True if child is STOPPED Ex: printf(“signo = %d\n”, WSTOPSIG(s));

40 waitpid – options parameter
No special handling options = WNOHANG Don’t 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 }

41 waitpid options options = WUNTRACED
Return status of child if child stopped and status has not already been returned (assumes job control support)


Download ppt "CSSE Operating Systems"

Similar presentations


Ads by Google