Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.

Similar presentations


Presentation on theme: "CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute."— Presentation transcript:

1 CSC 501 Lecture 2: Processes

2 Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute instruction Write back result

3 Process Process is a running program a program in execution an “instantiation” of a program We want to have multiple running programs However, we only have a few CPUs So, limit the number of programs you are running

4 Time to invent CPU virtualization OS virtualizes the CPU By time sharing it Mechanisms <- this lecture Policies <- next lecture

5 A Process A running program includes: Memory Registers I/O, opened files APIs: create, destroy, wait, control, status Code Static data Heap Stack

6 Process Creation Loading: code and static data Eagerly vs lazily Allocate memory for stack, heap Initialize file descriptors Jump to the main

7 Process states As a process executes, it changes state new: The process is being created running: Instructions are being executed blocked: 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

8 Admitted Exit I/O: done Scheduled I/O: initiate Descheduled Process Lifecycle New Ready Running Terminated Blocked

9 Data Structures Process list Common elements in process structure Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information

10 Example PCB in XINU

11 Process API On Unix: fork, exec, wait, … Why: they are essential in building a Unix shell

12 Process Creation UNIX examples fork system call creates new process exec system call used after a fork to replace new process’ memory space with a new program

13 Exmaple int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); }

14 Process Termination Possible scenarios for process termination Exit (by itself) Abort (by parent) Kill (by sysadmin)

15 Process Termination Exit (by itself) Process executes last statement and asks operating system to delete Abort (by parent) Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination Kill (by sysadmin) Administration purpose

16 Process Suspension Temporarily ‘‘stop’’ a process Prohibit from using the CPU Why? What should be done? Change its state in PCB Save its machine states for later resumption Process table entry retained Complete state saved

17 Limited Direct Execution Time sharing the CPU: Run one process for a little while, then run another one, and so forth Performance and control The “direct execution” part of the idea is simple: Just run the program directly on the CPU.

18 Protocol without limits Restricted Operations How to take over control

19 Restricted Operations The need to perform restricted operations Let processes do whatever they want Two modes: user mode and kernel mode How to perform restricted operations System calls, and they are procedure calls

20 How to execute system call Trap instruction The program executes trap, jump into kernel and raise privilege Kernel does the work Kernel calls return-from-trap, return into the calling user program and reducing the privilege level Save caller’s registers On x86, PC, flags, and a few others saved to a per-process kernel stack

21

22 Which code to run Let the calling process specify the address Set up a trap table at boot time The hardware remembers the locations of trap handlers

23 Switching Between Processes Since OS is not running, how can it do anything? A cooperative approach Used in early version of the Macintosh OS Wait for systems calls which include yield Wait if illegal actions which generate trap What if a process gets stuck in an infinite loop

24 A Non-Cooperative Approach: The OS Takes Control Timer interrupt Boot time: Set interrupt handler Start timer

25 Context Switch When CPU switches to another process System must Save the state of the old process (suspend) and Load the saved state for the new process (resume) Context-switch time is overhead System does no useful work while switching Time dependent on hardware support

26 Saving and Restoring Context Save state of currently executing process Copy all “live” registers to process control block Restore state of process to run next Copy values of live registers from process control block to registers How to get into and out of the context switching code?

27

28 Next Process scheduling CPU scheduling Multi-level feedback Lottery scheduling


Download ppt "CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute."

Similar presentations


Ads by Google