Unformatted text preview:

11CMSC 412Fall 2004Processes and ThreadsAnnouncements• Project #1– Due Friday• Reading– Chapter 4 (parts), 5 (parts)– Chapter 7 (for Monday)22Processes• What is a process?– A program in execution• Either sequentially or with multiple “threads ofcontrol.”• What’s not a process?– A program on a disk - a process is an activeobject, but a program is just a fileComputation AbstractionsCPU 1 CPU 2p3p1 p2 p2t1t2t1t2t3t1t4t5A dual-processor computerProcessesThreads33Processes vs. Threadsint x;foo() {…x…}int x;foo() {…x…}int x;foo() { …x…}foo() { …x…}Processes do notshare dataThreads share datawithin a processMore on threads later …Process State• Processes switch between different statesbased on internal and external events• Each process is in exactly one state at a time• Typical States of Processes (varies with OS)– New: just been created– Running: instructions are being executed• only one process per processor may be running– Waiting: waiting for an event to occur• examples: I/O events, signals– Ready: waiting to be assigned the CPU– Terminated: finished execution44Process State TransitionsnewreadyrunningwaitingterminatedadmittedinterruptdispatchI/O request or event waitI/O request orevent wait doneKillexitComponents of a Process• Memory Segments– Program - often called the text segment– Data - global variables– Stack - contains activation records– Heap - contains dynamically-allocated data• Processor Registers– Control registers• program counter - next instruction to execute• stack pointer• processor status word (from cmp instructions)– General purpose registers– Floating-point registers55Scheduling• OS must decide when a process is allowedto run; I.e. it must schedule processes• Long-term scheduler (or job scheduler) –selects which processes should be broughtinto the ready queue.• Short-term scheduler (or CPU scheduler) –selects which process should be executednext and allocates CPU. A.k.a thedispatcher.Schedulers• Short-term scheduler is invoked veryfrequently (milliseconds) ⇒ (must be fast).• Long-term scheduler is invoked veryinfrequently (seconds, minutes) ⇒ (may beslow).• The long-term scheduler controls thedegree of multiprogramming.• Processes can be described as either:– I/O-bound process – spends more time doing I/Othan computations, many short CPU bursts.– CPU-bound process – spends more time doingcomputations; few very long CPU bursts.66Scheduling Policy• How should the scheduler choose a processto run?• Can simply pick the first item in the queue– called round-robin scheduling– is round-robin scheduling fair?• Various scheduling criteria– Process class (I/O bound vs. CPU bound)– Priority– Resources consumed– Etc.Scheduling Implementation• Use alarm interrupts to switchbetween processes– when time is up, a process is put back onthe end of the ready queue– frequency of these interrupts (a.k.a. thequantum) is an important parameter• typically 3-10ms on modern systems• need to balance overhead of switching vs.responsiveness77Context Switch• When the OS switches a CPU to anotherprocess, it is called a context switch.• The system must save the state of the oldprocess and load the saved state for thenew process.• Context-switch time is overhead; thesystem does no useful work while switching.– Total time depends on hardware support.– Writing context-switch routines almost alwaysrequires some assembly languageOS Process Control Block• Stores all of the information about a process• PCB contains– Process state: new, ready, etc.– (saved) processor registers– Memory Management Information• page tables, limit registers for segments– CPU scheduling information• process priority; pointers to process queues– Accounting information• time used (and limits); files used; program owner;parent process id– I/O status information• list of open files; pending I/O operations88Sample PCBContext Switch using PCBs99GeekOS PCB (part I)• struct Kernel_Thread, contains– Process Identifier (PID)– Scheduling criteria (priority)– Accounting info (CPU clock ticks)– Kernel stack pointer• Context-switch information stored here; I.e.general-purpose registers, program counter,etc.• User processes in GeekOS are a specialkind of kernel threadGeekOS PCB (part II)• struct User_Context, contains– Pointer to process (physical) memory• Includes all the segments you set up inProject 1; I.e. code segment, data segment,etc.– Pointers to initial program entry point,initial argument, and initial stack.– Information for managing address spaceprotection• Ix86 descriptor tables and selectors• You’ll set this up in Project 21010Storing PCBs• Track which processes are in which states– Collection of PCBs is called a process table• How to store the process table?• First Option:– Simple, but slow to find processes– Also need additional datastructures for fairness P1P2P2P3P4P5Ready WaitingWaitingReadyNewTermQueues of Processes• Store processes in state-based queuesP1P2QueueReadyP3P4DiskQueueP5P6NetworkQueue1111State Transitions with Queuesnew termForking a New Process• New process is called the child; theprocess that created it is the parent• Create a PCB for the new process– copy most entries from the parent– clear accounting fields– buffer pending I/O– allocate a PID for the new process1212Forking a New Process• Allocate memory for it– Might copy all of the parents’ segments– Text segment could be shared• rarely changes– Use memory mapping hardware to help• will talk more about this in the memorymanagement part of the class• Add it to the ready queueProcess Termination• Process can terminate itself– via the exit system call• One process can terminate another process– use the kill system call– can any process kill any other process?• No, that would be bad.• Normally an ancestor can terminate a descendant• OS kernel can terminate a process– exceeds resource limits– tries to perform an illegal operation1313Orphan Processes• What if a parent terminates beforethe child?– the child called an orphan process• in UNIX - becomes child of the root process• in VMS - terminatedUNIX example• Terminated process– signals parent of its death (SIGCHILD)– is called a zombie in UNIX– remains around waiting to be reclaimed• Parent process– wait system call


View Full Document

UMD CMSC 412 - Processes and Threads

Documents in this Course
Security

Security

65 pages

Deadlocks

Deadlocks

22 pages

Set 2

Set 2

70 pages

Project 2

Project 2

21 pages

Load more
Download Processes and Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Processes and Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Processes and Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?