TDC 311 OS Process Description and Control OS States An operating system (as any program) is always in a current state It moves from one state to the next as it performs operations It is very important that you don’t move into a state that is a “black hole” Likewise, “miracle” states should not exist Let’s consider a very simple two-state model for an OS (on the whiteboard) Reasons for Process Creation New batch job Interactive log on Created by OS to provide a service Spawned by an existing process (for purposes of modularity or to exploit parallelism) Reasons for Process Termination Normal completion Time limit exceeded Memory unavailable Bounds violation Protection error Arithmetic error Time overrun (process has waited too long for an event to happen) I/O failure Reasons for Process Termination Invalid instruction Privileged instruction Data misuse Operator or OS intervention Parent termination Parent request Five-state Model What if the process has to wait for something to finish (like I/O)? Let’s consider a five-state model (on the whiteboard) Five-state Model with Suspend Let’s add suspend states to our previous five-state model Use a suspend state when all processes are blocked When moved to Suspend, process is swapped out to secondary storage, so a new process can be brought in Suspend Ready – has been suspended and event has occurred Suspend Blocked – blocked and suspended Let’s examine this model on the whiteboard UNIX Process State Transition Diagram Let’s see how UNIX models its states (on the whiteboard) Process Description What is a process made of? Control structures Memory tables – keep track of what memory is assigned to what process I/O tables File tables Process tables – keeps a list of pointers to process images Process Description A process is also described by its process control block (PCB) What goes into a PCB? Process attributes Identifier of this process Identifier of the process that created this process User identifier User-visible registers Stack pointers Process state indicator Scheduler priority indicator Scheduling-related information Process Description What else goes into a PCB? (some more process attributes) Event (that this process is currently waiting for, if any) Data structuring (this process may be linked to other processes in a queue, ring, or some other structure) Various flags, signals, and messages that may be associated with communication between two independent processes Process privileges (does this process have access to certain system utilities and services?) Memory management (pointers to segment/page tables) Resource ownership and utilization (any opened files?) Process Control Or, what kinds of “things” can a process do? But first, a process can be in either user mode or supervisor mode. This determines what a process can or can’t do. Process can “create” or “spawn” another process If a process can spawn another process, OS probably does the following: Assigns a unique PID to new process Entry added to process table Allocate space for process image PCM is initialized Appropriate linkages are set Miscellaneous data structures created, accounting updated Process Control What else can a process do? Process can “terminate” another process Process can “switch” execution to another process When does a process switch execution? When an interrupt occurs. Currently executing process is switched out, and an interrupt handler or supervisor process is switched in. Process Switching What happens when one process switches to another? Save the context of the current process, including program counter and other registers Update the PCB of the current process to denote Blocked Ready or whatever Move the PCB of this process to the appropriate queue Select the next process for execution Update the PCB of the selected process Update memory management structures Restore the context of the processor to that which existed at the time the selected process was last switched out of the Running state by loading in the previous values of the program counter and other registers Context Switching Sometimes it is not necessary to perform a process switch when an interrupt occurs What if you just want to change the settings on a flag or two? Don’t do a complete process switch – just do a context switch Some OSes don’t differentiate between process switching and context switching Threads Like lite beer, a thread is a lite process Each thread may contain: Thread ID Program counter Stack Register set Child threads State How can it be so lite? Threads A thread shares with other threads belonging to the same process For example, a thread may share: Code Data Other OS resources such as open files and signals Thus, a group of peer threads share code, address space, and OS resources One process may have multiple threads Why a thread? A thread takes far less time to create and move, and much less space Threads If threads share things, is security a problem? No, because the threads do not belong to unrelated competing processes, but to the same process Threads also make inter-communication easier since multiple threads share the same files and storage Thread operate pretty much like processes – they have a state, they can get blocked, and they can spawn new threads There are kernel threads and there are user threads Common Thread Examples Database server on a LAN – as each new query arrives, a new thread is spawned to service the request (one process, multiple threads). When one thread gets blocked requesting disk I/O, another thread can start up almost immediately and begin its operation Web browser – might have one thread display images or text while another thread retrieves data from the network Word processor – one thread for displaying graphics, another for reading keystrokes from the user, and a third for performing spelling and grammar checking in the background The Process in VAX/VMS Let’s consider a more complete example from a real OS (from VAX/VMS OS Concepts by Miller) “Only way a process can be created is by the action of another process.” Always some processes running on a system These processes can be in one of the following states: SUS – suspended (can be outswapped) COM – computable (waiting in ready queue)(can be outswapped) HIB – hibernating (can be outswapped) LEF – waiting for a local event flag CUR – currently running; and there are others The Process in VAX/VMS Each process has a priority assigned to it 0 – 31 31 is the highest 4 is typical interactive user Let’s examine what happens when a user logs in User Login Procedure Terminal device reports via interrupt that a key has been pressed 2. Terminal driver attempts to associate the keystroke to an existing process using the translation table 3. Since no association exists (new LOGIN), JOB_CONTROL is awakened 4. JOB_CONTROL calls function $CREPRC which creates a process image which will eventually become the user’s. For now, the new process is just like JOB_CONTROL and has the following information: 1. ACB (AST Control Block) Process ID PCB Vector Forward Link Backward Link Housekeeping ACB Links Physical PCB Ptr Quota Limits Local Event Flags EF Cluster Ptrs PID PHD Ptr JIB Ptr Privileges ACL Links State Priority Name PCB Asynchronous System Traps PHD (Process Header) Accounting Physical PCB Memory Info (for the code) Job Info Block Access Rights Event Flags, etc. JIB ACL User LOGIN Procedure continued 5. 6. 7. 8. All quotas, privileges, priorities are temporarily the same as JOB_CONTROL Image of LOGINOUT is placed into its PHD Memory Info $CREPRC marks new process as computable outswapped (COMO) and informs VMS to insert it into the appropriate queue $CREPRC returns to JOB_CONTROL JOB_CONTROL hibernates Eventually the new process gets a chance to execute, which runs LOGINOUT procedure User LOGIN Procedure continued LOGINOUT prompts for Username and Password using $QIOW routine 10. LOGINOUT takes Username and Password and searches file SYSUAF.DAT for valid account 9. User LOGIN Procedure continued 11. If account if valid, LOGINOUT extracts following info from user account info: Process name (usually user name) b) Default disk drive and directory info c) User privileges d) User quotas e) Run-time restrictions f) Process priority g) Default CLI (command line interpreter, or shell) and updates the PCB accordingly a) User LOGIN Procedure continued 12. LOGINOUT initializes the CLI, which is usually DCL program (the standard VAX shell) 13. DCL eliminates LOGINOUT 14. DCL executes LOGIN.COM file 15. DCL puts $ prompt on screen via $QIOW