What Is A Process? Process Concept Process States Process ...

33 downloads 401 Views 202KB Size Report
6 Sep 2013 ... Process Control Block (PCB). OS bookkeeping information associated with each process: • Process state. • Program counter. • CPU registers.
9/6/2013

What Is A Process? A process is a program in execution. #include  int main(int argc, char*argv[]) { int v; printf(“hello world\n”); scanf(“%d”, &v); return 0; }

Process Fundamentals

%gcc ‐o test‐process test‐process.c %./test‐process & hello world %ps ‐ef | grep test‐process xmeng    22807  4288  0 10:08 pts/0    00:00:00 ./test‐process

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors  Silberschatz, Galvin, and Gagne

CSCI 315 Operating Systems Design

Program test‐process is running

1

Process States

Process Concept • A process includes:

As a process executes, it changes state:

– program counter – stack – data section

heap

• Process execution must  progress in sequential progress in sequential  fashion. • Program Counter (PC)  designates the next instruction to be  program counter executed

new: The process is being created. running: Instructions are being executed. waiting: 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.

– – – –

stack

data

code

9/6/2013

CSCI 315 Operating Systems Design

3

9/6/2013

OS bookkeeping information associated  with each process:

created exit

admitted

interrupt

terminated

• • • • • • •

running

ready scheduler dispatch I/O or event completion

I/O or event wait

waiting

Process state Program counter CPU registers CPU registers CPU scheduling information Memory‐management information Accounting information I/O status information

CSCI 315 Operating Systems Design

5

9/6/2013

process id process state program counter registers memory limits list of open files





9/6/2013

4

Process Control Block (PCB)

Process State Transition Diagram new

CSCI 315 Operating Systems Design

CSCI 315 Operating Systems Design

6

1

9/6/2013

A Sample Linux PCB

CPU Switching

• http://www.eecs.harvard.edu/~margo/cs161/ videos/sched.h.html • http://www.tldp.org/LDP/tlk/ds/ds.html • Look for struct kf task_struct k i h in the source code  d distribution of Linux.

9/6/2013

Process Scheduling Queues

CSCI 315 Operating Systems Design

8

Processes and OS 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. Processes migrate between the various queues.

9/6/2013

CSCI 315 Operating Systems Design

9

9/6/2013

Process Scheduling

10

Schedulers CPU

ready queue

I/O

CSCI 315 Operating Systems Design

I/O queue

• 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 p

I/O request time slice expired

9/6/2013

child executes

fork a child

interrupt occurs

wait for interrupt

CSCI 315 Operating Systems Design

11

9/6/2013

CSCI 315 Operating Systems Design

12

2

9/6/2013

Schedulers

Context Switch

• Short‐term scheduler is invoked very frequently (milliseconds)   (must be fast) • Long‐term scheduler is invoked very infrequently (seconds,  minutes)  (may be slow; controls the degree of  multiprogramming) • Processes can be described as either:

• When CPU switches to another process, the system  must save the state of the old process and load the  saved state for the new process. – What is in context? (Information in PCBs)

– I/O‐bound process – spends more time doing I/O than computations,  many short CPU bursts

• Context‐switch time is overhead; the system does no  useful work while switching.

– CPU‐bound process – spends more time doing computations; few very  long CPU bursts

• Time dependent on hardware support.

9/6/2013

CSCI 315 Operating Systems Design

13

9/6/2013

CSCI 315 Operating Systems Design

Operations on Processes

14

Process Creation • Parent process create children processes, which, in turn can  create other processes, forming a tree of processes.

• Creation • Termination

• 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 may wait until children terminate.

9/6/2013

Process Creation (Cont.)

CSCI 315 Operating Systems Design

16

Process Tree (Linux Example)

• Address space: – Child has duplicate of parent’s address space, or – Child can have a program loaded onto it.

• UNIX examples: – fork system call creates new process and returns with a pid  (0 in child, > 0 in the parent), – exec system call can be used after a fork to replace the  process’ memory space with a new program. See example by running “ps –aef | less” 9/6/2013

CSCI 315 Operating Systems Design

17

3

9/6/2013

Process Termination

Create a Process in Linux #include  #include  int main(int argc, char *argv[])  {   pid_t pid;    if ((pid = fork())