Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processes: program + execution state

Similar presentations


Presentation on theme: "Processes: program + execution state"— Presentation transcript:

1 Processes: program + execution state
Pseudoparallelism Multiprogramming Many processes active at once With switching: process execution is not repeatable processes should make no assumptions about timing Process consists of: Process’ core image: program, data, run-time stack Program counter, registers, stack pointer OS bookkeeping information

2 Process State As a process executes, it changes state
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 process. terminated: The process has finished execution.

3 Diagram of Process State

4 Diagram of Process State
X

5 Diagram of Process State
X X

6 Diagram of Process State
X X X

7 Process-Centered Operating System
One way to view system: a scheduler, with processes running “on top”: Some of the processes are system processes Scheduler p r o c e s examples of system-level processes: lpd, init, cron, ftpd (on some systems--on others, invoked as needed), telnetd, …

8 Process Table One entry per process. PCB Pr Pr Pr 2 Pr 4 …..Pr N

9 PCB: Process Management
registers, program counter, program status word, stack pointer process state time process started, CPU time used, children’s CPU time used alarm clock setting pending signal bits pid message queue pointers, other flag bits For MINIX!

10 PCB: Memory-Related Information
pointers to text Data stack segments Pointer to page table For MINIX!

11 PCB: File-Related Information
root, working directory file descriptors User ID Group ID For MINIX!

12 Process Control Block (PCB)

13 Context Switch (or Process Switch)
Currently executing process looses control of CPU Its “context” must be saved (if not terminated) into PCS. New process is chosen for execution. New process context is restored. New process is given control of the CPU.

14 CPU Switch From Process to Process

15 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.

16 Ready Queue And Various I/O Device Queues

17 Representation of Process Scheduling

18 Low Level Interrupt Processing
Each HW device has slot in interrupt vector (IV). On interrupt, HW pushes PC, PSW, one or more registers. Loads in new PC from IV. END OF HW Assembly code to save registers and info on stack (to PCB). Assembly sets up new stack for handling process. Calls C interrupt handling routing to complete interrupt processing. Scheduler is called and determines next process to run. Assembly language restores registers and other necessary items (e.g., memory map) of selected process and begins its execution.

19 Schedulers 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.

20 Schedulers (Cont.) Short-term scheduler is invoked very frequently (milliseconds)  (must be fast). Long-term scheduler is invoked very infrequently (seconds, minutes)  (may be slow). The long-term scheduler controls the degree of multiprogramming.

21 Schedulers (Cont.) Processes can be described as either:
I/O-bound process – spends more time doing I/O than computations, many short CPU bursts. CPU-bound process – spends more time doing computations; few very long CPU bursts.

22 Context Switch 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. Context-switch time is overhead; the system does no useful work while switching. Time dependent on hardware support.

23 Process Creation Parent process create children processes, which, in turn create other processes, forming 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.

24 Process Creation (Cont.)
Address space Child duplicate of parent. Child has a program loaded into it. UNIX examples fork system call creates new process exec system call used after a fork to replace the process’ memory space with a new program.

25 wait(NULL) ; /* Block execution until child terminates */
Unix Fork() #include <stdio.h> main(int argc, char *argv[]) { int pid, j ; j = 10 ; pid = fork() ; if (pid == 0) /*I am the child*/ { Do child things } else /* I am the parent */ wait(NULL) ; /* Block execution until child terminates */ }

26

27 fork()

28 Processes Tree on a UNIX System

29 exec Function calls Used to begin a processes execution.
Accomplished by overwriting process imaged of caller with that of called. Several flavors, use the one most suited to needs. int execv( char *path, char *argvec[]) ;

30 exec Function calls int execv( char *path, char *argvec[]) ;
pathname: Can be an executable program in your directory (application code) or a system program such as ls, cd, date, ………….. argvec: Pointers to NULL terminated strings. First element should always be the name of the program, last element should always be NULL. Assume: a.out b.out x.exe. By.by all in home directory.

31 main (int argc, *argv[])
{ int pid ; char args[2] ; pid = fork() ; if (pid ==0) { args[0] = “./a.out” ; All executed by args[1] = NULL ; child process execv(“./aout”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not a problem!\n”) ; Executed by parent

32 Process Termination Process executes last statement and asks the operating system to decide it (exit). Output data from child to parent (via wait). Process’ resources are deallocated by operating system.

33 Process Termination Parent may terminate execution of children processes (abort). Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting. Operating system does not allow child to continue if its parent terminates. Cascading termination.

34 Cooperating Processes
Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience

35 Producer-Consumer Problem
Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. unbounded-buffer places no practical limit on the size of the buffer. bounded-buffer assumes that there is a fixed buffer size.

36 Constraints Do not over-write an item not yet consumed. Do not write to a full buffer Do not read from a previously read item. Do not read from an empty buffer.

37 Bounded-Buffer – Shared-Memory Solution
Shared data #define BUFFER_SIZE 10 Typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;

38 Bounded-Buffer – Producer Process
item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; }

39 Bounded-Buffer – Consumer Process
item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; }

40 Producer: while (((in + 1) % BUFFER_SIZE) == out) ; Consumer Blocked: while (in == out) ; in out

41 Producer Blocked: while (((in + 1) % BUFFER_SIZE) == out) ; Consumer: while (in == out) ; O O O O O out in

42 Producer: while (((in + 1) % BUFFER_SIZE) == out) ; Consumer Blocked: while (in == out) ; X X X X X out in

43 Medium Term Scheduling


Download ppt "Processes: program + execution state"

Similar presentations


Ads by Google