Lecture 2 Code examples on csserver in directory /home/hwang/cs470/lecture02 Questions?

advertisement

Lecture 2

Code examples on csserver in directory

/home/hwang/cs470/lecture02

Questions?

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 1

Outline

What is a process?

Process states

Process control blocks

Transitions between process states

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 2

What is a Process?

Informally, a process is a program in execution

For now, assume process is sequential and single-threaded. I.e., only one instruction at a time is executed on behalf of a process. We will look at threads later.

A process is active . There can be > 1 process per program. Also called a job .

3 Wednesday, January 12 CS 470 Operating Systems - Lecture 2

What is a Process?

Process is more than just its code (text section).

Includes the current activity context:

 program counter registers

 stack & heap

(temporary data)

 globals (data section)

Wednesday, January 12 CS 470 Operating Systems - Lecture 2

Stack

Heap

Data

Text

4

Process State

Each process has a state that may change as it executes. States have arbitrary names, but all states shown below are represented in all modern OS's

NEW admitted

Wednesday, January 12

READY dispatch

RUNNING

TERMINATED exit interrupt

I/O or event completion

I/O or event

WAITING

CS 470 Operating Systems - Lecture 2 wait

5

Process Control Block (PCB)

What does a process need to keep track of?

Unique ID (UID), parent's UID (PID), children's UIDs

Process state

Program counter and registers

CPU scheduling information, e.g., priority

Memory management information, e.g., stack pointer

Other activity information, e.g., time used

I/O status, e.g., open files

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 6

Process Control Block (PCB)

Process information is stored in a process control block (PCB)

Each PCB is stored in some "queue" in the system.

Many systems have a hardware register dedicated to pointing to the current running process's PCB.

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 7

NEW  READY

Look closer at the state transitions. A newly created process is in the NEW state.

It transitions to the READY state when it is admitted into the system. Process is said to be put on the Ready Queue .

NEW admitted

READY

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 8

NEW  READY

In batch systems, processes must wait to be admitted. Two phases of scheduling:

Long-term (batch) scheduler determines the group to be admitted

Short-term (process) scheduler determines which process in admitted group is dispatched to the CPU to be run.

In time-sharing systems, i.e., most modern operation systems, new processes are admitted immediately, so there is only one (process) scheduler

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 9

NEW  READY

Processes are created via a system call (e.g. fork( ) in Unix). The creating process is the parent ; created process is the child .

What does a new process need?

PCB, memory, files, …

Where does it get these resources from?

Directly from the OS

Inherit from parent

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 10

NEW  READY

What happens to the parent process?

Continue to execute concurrently with child

Waits for some or all of children to terminate

What program does the child run?

Duplicate of parent

Specified program

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 11

NEW  READY

Unix - parent runs concurrently and child is a duplicate by default. Parent can execute wait( ) . Child can execute exec( ) .

DEC VMS - parent runs concurrently and loads a specific program into the child.

WinNT - parent runs concurrently and child can either be a duplicate or a specified program.

12 Wednesday, January 12 CS 470 Operating Systems - Lecture 2

NEW  READY

Code examples:

Unix fork( ): newproc-posix.c (Figure 3.10)

 fork( ) returns 0 in child, child UID in parent child starts executing the statement after the fork uses execlp( ) and wait( )

Win32 CreateProcess( ): newproc-win32.c

(Figure 3.12)

10 parameters, including program image to load uses WaitForSingleObject

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 13

READY  RUNNING

Process transitions to the RUNNING state when it is dispatched to the CPU to be run.

READY dispatch

RUNNING

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 14

READY  RUNNING

If there is a currently running process that is not exiting, its information must be saved (usually in its PCB) before the newly dispatched process can take over the CPU. This process is moved to an appropriate queue.

The newly dispatched process information is loaded from its PCB, then starts executing

Called a context switch .

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 15

READY  RUNNING

The process scheduler chooses a process on the Ready Queue to dispatch. It must support two objectives:

 use multiprogramming to maximize CPU utilization switch CPU between processes often enough so the users can interact with processes

Scheduling decisions must be fast, and context switches must be fast.

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 16

RUNNING Transitions

A running process can leave the RUNNING state in one of three ways:

READY interrupt

RUNNING

TERMINATED exit

WAITING

I/O or event wait

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 17

RUNNING  READY/WAITING

A running process may be interrupted . Since it is still able to be run, it goes back to the

READY state.

A running process may need to wait for some

I/O or other event. The event ID (EID) is recorded in the process PCB and the process transitions to the WAIT state and is said to be put on the Wait Queue .

In both these transitions, there is a context switch.

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 18

RUNNING  TERMINATED

A running process may exit to the

TERMINATED state.

Normal termination usually is done by calling an exit system call that releases resources and notifies parent.

Exceptional termination include:

Parent terminating child explicitly

Cascading termination when an OS does not allow a child to survive a parent exit. Example: DEC VMS

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 19

RUNNING  TERMINATED

Example: Unix process termination

Process uses exit (status) system call

Parent process uses wait (&status) , which causes the parent process to wait until some child exits.

Can also use waitpid(uid, &status, options) to wait on a particular child's exit.

Parent must wait on a child for the child process to complete termination. Until this happens, the child process is a zombie . If parent dies first, the init process (UID 1) "adopts" the process and automatically issues a wait.

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 20

WAITING  READY

When I/O or other event completes , the CPU is interrupted, and the process waiting for the event transitions back to the READY state and is put on the Ready Queue. Note: this interruption may or may not cause the currently running process to give up the CPU.

READY

Wednesday, January 12

I/O or event completion WAITING

CS 470 Operating Systems - Lecture 2 21

Other Process States

Some systems allow active processes to be suspended and swapped out (usually to free up resources) and then resumed later.

This decision usually is made by a mid-term scheduler (between the batch and process schedulers) and adds two states (SUSPENDED and WAIT SUSPENDED) to the process state diagram as shown on the next slide.

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 22

Other Process States

NEW admitted

READY

I/O or event completion resume wait resume

TERMINATED dispatch exit

RUNNING interrupt

WAITING

I/O or event wait

WAIT

SUSPENDED

I/O or event completion wait suspend

SUSPENDED suspend

Wednesday, January 12 CS 470 Operating Systems - Lecture 2 23

Download