csc 501 lecture 2: processes. von neumann model both program and data reside in memory execution...
TRANSCRIPT
Von Neumann Model
• Both program and data reside in memory
• Execution stages in CPU:• Fetch instruction• Decode instruction• Execute instruction• Write back result
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
Time to invent CPU virtualization• OS virtualizes the CPU• By time sharing it
• Mechanisms <- this lecture• Policies <- next lecture
A Process
• A running program includes:• Memory• Registers• I/O, opened files
• APIs: create, destroy, wait, control, status
CodeStatic data
Heap
Stack
Process Creation
• Loading: code and static data• Eagerly vs lazily
• Allocate memory for stack, heap• Initialize file descriptors• Jump to the main
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
AdmittedAdmitted ExitExit
I/O: doneI/O: done
ScheduledScheduled
I/O: initiateI/O: initiate
DescheduledDescheduled
Process Lifecycle New
Ready Running
Terminated
Blocked
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
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
Exmapleint 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);}
}
Process Termination
• Possible scenarios for process termination• Exit (by itself)• Abort (by parent)• Kill (by sysadmin)
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
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
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.
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
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
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
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
A Non-Cooperative Approach:The OS Takes Control• Timer interrupt• Boot time:• Set interrupt handler• Start timer
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
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?