how processes are created creating processes
play

How Processes are Created Creating Processes The first process is - PowerPoint PPT Presentation

CSE 120Principles of Processes/Threads Operating Systems Multiprocessing: Multiprogramming: One CPU switching quickly between various programs Multiprocessor: Multiple CPUs each running a program simultaneously All Software Organized


  1. CSE 120—Principles of Processes/Threads Operating Systems Multiprocessing: � Multiprogramming: One CPU switching quickly between various programs � Multiprocessor: Multiple CPUs each running a program simultaneously All Software Organized as Processes � Program + state information – Code + register values, stack, memory, … � Analogy July 11, 2006—Day 2 – Program � Class – Process � Object Kernel switches between processes Processes � Cooperative: Each process yield s when willing to give up control � Preemptive: Timer goes off every so often. (How often?) Threads � Context Switch : switching from one process to another: – Swap registers - including Program Counter (PC) and Stack Pointer (SP) – Change memory map Instructor: Neil Rhodes Each process has the illusion that it runs continuously, independent of other processes � At times, the programmer must be aware that it’s just an illusion 2 Advantages of Multiprogramming Memory Model of a Process Easier to organize/abstract Text: The code � Imagine if entirety of GNU/Linux was one giant program! Better Throughput � If process A is waiting for a read from disk to complete, process B can Data: profitably use the CPU � The global variables Potential Problems � The dynamically allocated memory (heap) � Process that have real-time requirements (deadlines) – Video, Audio good examples Stack � activation records (stack frames). One per in-progress subroutine – Parameters – Saved program counter – Pointer to previous activation record – Any saved registers – Local variables All are memory addresses in the process’s address space 3 4

  2. How Processes are Created Creating Processes The first process is created by hand (at boot time) When process A creates a process B � Process A is the parent process � Process B is the child process Remaining processes are spawned from existing processes � Two possibilities: � Unix: fork() – Process A continues running concurrently with B � Windows: CreateProcess(…) – Process A stops until B is finished � Two possibilities with respect to address space – Child process has a duplicate of the parent process (same code, data, stack, etc.) – Child process has new program loaded into it. 5 6 Process Creation on Unix Sharing the CPU int main() Three processes: their illusion { int pid; pid = fork(); if (pid < 0) { // error occurred Three processes: the reality exit(1); } else if (pid == 0) // child process exec(“/bin/ls”, “ls”, NULL); } else { // parent process wait(NULL); // wait until child completes } Each process gets a slice of time (the quantum or timeslice ) } 7 8

  3. How Time is Shared (Cooperative) How Processes are Destroyed Normal exit—voluntary Cooperative multiprogramming � return from main() Process A calls Yield() � next process in list of waiting process now gets to run. Error exit—voluntary � exit(error_code) Fatal error—involuntary What does Yield do? � Divide-by-zero � Must do Context Switch � Segmentation violation � Switch registers � Bus error � Switch memory map � … Killed by another process—involuntary Yield must cause execution to begin in Process B � % kill -SIGKILL 1234 # sends SIGKILL signal (9) to process ID 1234 � Where? � or, kill(SIGKILL, 1234) When Process B calls yield, Process A runs � From A’s point of view, it called Yield, which just returned (even though B executed in the meantime). 9 10 Details of Context Switch Context-switch Example How does Yield work? A: B: Yield() { int y = 1; int x = 10; volatile int magic = 0; void main() { void main() { One implementation … Yield(); Yield() Save context of current process � Yield(); x = 11; { if (magic == 1) volatile int magic = 0; return; y = 2; Yield(); magic = 1; } x = 12; Save context of current process (memory map, general registers, SP, PC) restore context of next process } if (magic == 1) } return; magic = 1; Text: Text: restore context of next process (memory map, general registers, SP, PC) Data: Data: } Stack: Stack: Saved Saved PC: PC: SP: SP: 11 12

  4. How is Time Shared? (Preemption) Possible States of a Process Preemptive Running � Advantages: � Actually executing on the CPU � Only one process can be in this state – Doesn’t require cooperation from all applications – Doesn’t require special code in an application Ready � Disadvantages: � Ready to use the CPU (in a queue). – OS might not have as much information as application � What is the ordering of the queue? – Context switch occurs anytime, rather than at well-defined locations in code Blocked How implemented? � Not able to use the CPU (waiting for something) � Kernel starts hardware timer � If process finishes before timer expires, no problem – Makes blocking I/O call running – Gives up CPU explicitly (yield) – Process destroyed � Otherwise, timer interrupt occurs – Hardware dispatches to interrupt handler – Interrupt handler saves state of process A – Figures out which is the next eligible process ready blocked – Restores state of process B – When process A runs again, it’ll continue from where it left off 13 14 Context-switching/Scheduling Process table Context-switching Kernel stores information about each process in a table � Switching from one process to another Entries in a table: Process Control Block (PCB) � Has overhead because � Program Counter – Requires system call � Stack Pointer – Change memory map � Other registers – May flush cache � Open files/sockets � Scheduling information Scheduling � Memory map information � Deciding which process should next run � Priority � We’ll see more about this next class � History � Accounting information – CPU time spent by this process – CPU time spent by the kernel on behalf of this process 15 16

  5. Kernel vs. Process Inter-Process Communication Kernel supports processes (is not a process itself) Cooperating processes must communicate Need way to � Transfer information Well-defined entry to the kernel � Synchronize � Trap: system call by application � Interrupt: generated by hardware (timer, I/O device, etc.) Different types of IPC � Shared Memory How does kernel get control? – Synchronization carried out via semaphores or monitors � Hardware (on trap or interrupt) � Message passing – Switches from user mode to supervisor mode � Remote Procedure Call (RPC) – Hardware determines new PC using a vector (Stored at predefined location) - 68K: 16 traps. x86: 256 software traps � Software (trap or interrupt handler) – Stores state – Calls appropriate Kernel routine (based on specific trap or interrupt) – Restores state (if context switch, does not restore state). User mode/supervisor mode � Usually stored in Processor Status Word (PSW) � In user mode, some instructions not allowed – Write PSW (Read PSW?) – Change memory map – Others that could breach security 17 18 Message Passing Remote Procedure Call (RPC) One process sends a message, another receives it Make a procedure call that invokes procedure in remote process � Variable-size / fixed-size message � Could be process on the same machine, or process across a network � Direct or indirect communication – Directly specify other process - Symmetric: Send(P, message) / Receive(Q, &message) - Asymmetric: Send(P, message) / Receive(&processID, &message) How it works: – Indirect: use mailbox or port � Local stub marshal s parameters - send(MboxA, message) / Receive(MboxA, &message) � May need to convert to machine-independent format � Synchronous or asynchronous � Sends message with procedure ID and parameters – Blocking send � Waits for receipt of message with return result – Nonblocking send – Blocking receive – Nonblocking receive Remote Method Invocation (RMI) – Combo of blocking send and blocking receive yields a rendezvous � Buffering: zero, bounded, or unbounded � Used in Java as a way to cause a method to be executed on a remote object – Zero: sender blocks until receiver receives message – Bounded: If the buffer is full, sender blocks Corba (Common Object Request Broker Architecture) – Unbounded: sender never blocks � Language- and platform-neutral way to execute methods on objects 19 20

Recommend


More recommend