Lecture 4: Process Management (Chapters 2-3) Process: execution context of running program. A process does not equal a program! Process is an instance of a program Many copies of same program can be running at same time OS manages a variety of activities User programs Batch jobs and scripts System programs (e.g., print spool, file server, network daemons, ) Each activity is encapsulated in a process. All activity is either in the OS or in a process.
OS manages processes: OS creates, deletes, suspends, and resumes processes OS schedules processes to manage CPU allocation ( scheduling ) OS manages inter-process communication and synchronization OS allocates resources to processes Cooperating processes banking example ATM transaction manager Web ASP calculating current account balance Monthly statement generator process Question: If all of these processes try to access the same account information simultaneously, what can happen? Process state consists of: Memory state: code, data, heap, stack Processor state: PC, registers, etc. Kernel state:» Process state: ready, running, etc.» Resources: open files/sockets, etc.» Scheduling: priority, cpu time, etc. Address space consists of: Code Static data (data and BSS) Dynamic data (heap and stack) See: Unix size command Special pointers: PC: current instruction being executed HP: top of heap (explicitly moved) SP: bottom of stack (implicitly moved) 0xFFFFFFFF SP HP PC 0x00000000 Stack Heap (Dynamically allocated) Uninitialized data (BSS segment) Static data (Data segment) Code (Text segment)
On a Unix machine, try: ps Af : lots of info on all running processes kill 9 547 : immediately terminates process with PID 547 top : displays dynamic info on top running jobs Write a program that calls:» getpid() : returns current process s PID» fork(),exec(),wait(): creates new process» sleep() : puts current process to sleep for specified time Similar commands work on Mac OS X (BSD Unix!) On Windows Task managed (CTL-ALT-DEL) Each process has a state: new: OS is setting up process ready: runnable, but not running running: executing instructions on CPU waiting: stalled for some event (e.g., IO) terminated: process is dead or dying OS invariant: At most one running process at a time As program executes, it moves from state to state as a result of program, OS, or extern actions Program: sleep(), IO request, OS action: scheduling External: interrupts, IO completion create process New Ready I/O done schedule deschedule Waiting Terminated exit process Running block on timer, I/O, page fault,
Process control block (PCB) One per process, allocated in kernel memory Tracks state of a process, typically including:» Process state (running, waiting, )» PID» PC, SP, registers» Memory management info» Open file table (open socket table)» Queue pointers (waiting queue, I/O, sibling list, parent, )» Scheduling info (e.g., priority, time used so far, ) When process created, new PCB allocated, initialized, and put on ready queue (queue of runnable processes) When process terminates, PCB deallocated and process state cleaned up (e.g., files closed, parent informed of death, ) OS tracks PCBs using queues Ready processes on readyq Each I/O device has a queue Queue traversed when I/O interrupt handled Processes linked to parents and siblings Needed to support wait() Need to be comfortable managing data on multiple queues (pointers galore!) Ready Queue Disk Wait Queue head tail PID=532 PID=12 PID=119 PID=73 head tail PID=48
Context switch: change from one process to another Select another process to execute ( scheduling ) Store CPU state of running process (PC, SP, regs, ) in its PCB» Requires extreme care: some values from exception stack Load most of CPU state for next process s PCB in to CPU» What can you not just load directly? Set up pseudo-exception stack containing state you want loaded for next process (e.g., PC, SP, PSW, ) Perform (privileged) return from exception instruction» Restores sensitive CPU state from exception stack frame Context switches are expensive Time sharing systems do 100-1000 context switches per second When? Timer interrupt, packet arrives on network, disk I/O completes, user moves mouse, In Windows, CreateProcess(): Creates new process running specified program In Unix, fork(): Creates new process that is near-clone of forking parent Return value of fork() differs: 0 for child, child_pid for parent Many kernel resources are shared, e.g., open files and sockets To spawn new program, use some form of exec() Question: Where does first process (init) come from? Question: Why fork/exec versus CreateProcess?
Stack: Stack: SP COPY COPY COPY SP HP PC Data: pid: 334 Code: pid=fork(); This is the only difference between parent and child! HP PC Data: pid: 0 Code: pid=fork(); Fork(), exit(), and exec() are weird! fork() returns twice once in each process exit() does not return at all exec() usually does not return: overwrites current process with new one! int main (int argc, char** argv) { while (1) { pid_t pid = fork(); if (pid == 0) { char** arguments = 0; for (int i = 1; i < argc; i++) { cout << argv[i] << endl; arguments[i-1] = argv[i]; cout << arguments[i-1]; else { What will happen when you run this program? What might a sysadmin do to prevent this?
When process dies, OS reclaims its resources On Unix: A process can terminate itself using exit() system call A process can kill its child using the kill() system call #include <signal.h> #include <unistd.h> #include <stdio.h> int main(void) { int parentid = getpid(); int cid = fork(); if (cid == 0) { printf( Child exiting!\n ); exit(0); printf( Impossible!\n ); else { printf( Type to kill child\n ); char answer[10]; gets(answer); if (!kill(cid,sigkill)) { printf( Child dead!\n ); How can you speed up fork()? Hint: Think about high cost of copying large address space Homework: Read about different fork/exec variants.
Sign up for cs5460@cs already! Project one assignment out: Simple shell with I/O redirection Due Sept 10 th (one week from today at midnight Weds-Thu) INDIVIDUAL project not a group effort! For next time, read Chapter 5 (finish Chapter 4)! Threads vs processes Brief discussion of concurrency control issues Intro to dispatching and scheduling Processes are heavyweight Memory mappings: expensive to swap Cache/TLB state: flushing expensive, especially side effects Lots of kernel state Context switching between processes is EXPENSIVE Threads are lightweight Multiple threads share process state» Same address space» Same open file/socket tables Goal: make context switching between threads CHEAP Not all OSes support threads efficiently (e.g., older Unixes)» Had to fake it using things like select() and signal()» Good news for you your OS will not support threads
Address space shared by threads Code Data and heap Thread private state: Registers (inc. pc, sp, psw) Stack Key issue: How do you safely access shared state?» Read-only (e.g., code) easy» Writable hard Whole section of course dedicated to this» Synchronization» Concurrency control Context switch between threads Save/restore registers (tricky) SP0 SP1 HP PC1 PC0 Stack: Stack: Data: Code: Kernel threads: OS is aware of thread abstraction Process: address space, privileges, kernel resources Thread: execution state, scheduling entity (PC, SP, registers) Each thread has OS state (ready, blocked, ) Kernel context switches between threads (and processes) User threads: OS is unaware of thread abstraction Thread context switch performed entirely within user space Requires cooperation between threads to ensure fairness What issues arise? Kernel threads: overhead of context switches, User threads: blocking I/O,
Concurrency control How do you write programs/threads that can cooperate to solve a problem? How do you ensure safe access to shared resources? What kinds of problems arise? Dispatching and scheduling Dispatching: operations involved in performing a context switch Scheduling: deciding what process/thread to run next (Backup slides follow )
fork/forkl: Creates a new process that is almost identical copy of current one Only difference is return code from fork call (parent: PID, child: 0) In Unix, only way to make a new process! exec/execv/execl/ : Replaces running program with one named as argument Typically loads new program from disk into address space Calling context is LOST (if successful) READ THE Shares open file table with original process (important!) MANUAL wait: PAGES!!! Waits for any child process to exit() Returns child process s exit code, along with some other status exit (val): Terminates running process with specified exit code