html5-img
1 / 62

Process Description and Control

Process Description and Control. Module 1.1. When to Switch a Process ?. A process switch may occur whenever the OS has gained control of CPU. ie when: Supervisor Call explicit request by the program (ex: file open). The process will probably be blocked Trap

cili
Télécharger la présentation

Process Description and Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Process Description and Control Module 1.1

  2. When to Switch a Process ? • A process switch may occur whenever the OS has gained control of CPU. ie when: • Supervisor Call • explicit request by the program (ex: file open). The process will probably be blocked • Trap • An error resulted from the last instruction. It may cause the process to be moved to the Exit state • Interrupt • the cause is external to the execution of the current instruction. Control is transferred to IH

  3. Examples of interrupts • Clock • process has expired his time slice and is transferred to the ready state • I/O • first move the processes that were waiting for this event to the ready (or ready suspend) state • then resume the running process or choose a process of higher priority • Memory fault • memory address is in virtual memory so it must bring corresponding block into main memory • thus move this process to a blocked state (waiting for the I/O to complete)

  4. Mode Switching • It may happen that an interrupt does not produce a process switch • The control can just return to the interrupted program • Then only the processor state information needs to be saved on stack • This is called mode switching (user to kernel mode when going into IH) • Less overhead: no need to update the PCB like for process switching

  5. Steps in Process (Context) Switching • Save context of processor including program counter and other registers • Update the PCB of the running process with its new state and other associate info • Move PCB to appropriate queue - ready, blocked • Select another process for execution • Update PCB of the selected process • Restore CPU context from that of the selected process

  6. Execution of the Operating System • Up to now, by process we were referring to “user process” • If the OS is just like any other collection of programs, is the OS a process? • If so, how it is controlled? • The answer depends on the OS design.

  7. Nonprocess Kernel (old) • The concept of process applies only to user programs • OS code is executed as a separate entity in privilege mode • OS has its own memory regions and stack • OS code never gets executed within a process

  8. Execution within User Processes • Virtually all OS code gets executed within the context of a user process • On Interrupts, Traps, System calls: the CPU switch to kernel mode to execute OS routine within the context of user process (mode switch) • Control passes to process switching functions (outside processes) only when needed

  9. Execution within User Processes • OS code and data are in the shared address space and are shared by all user processes • Separate kernel stack for calls/returns when the process is in kernel mode • Within a user process, both user and OS programs may execute (more than 1)

  10. Process-based Operating System • The OS is a collection of system processes • major kernel functions are separate processes • small amount of process switching functions is executed outside of any process • Design that easily makes use of multiprocessors

  11. UNIX SVR4 Process management • Most of OS executes within user processes • Uses two categories of processes: • System processes • run periodically in kernel mode for administrative and housekeeping functions (memory allocation, process swapping...) • User processes • run in user mode for user programs • run in kernel modes for system calls, traps, and interrupts

  12. UNIX SVR4 Process States • Similar to our 7 state model • 2 running states: User and Kernel • transitions to other states (blocked, ready) must come from kernel running • Sleeping states (in memory, or swapped) correspond to our blocking states • A preempted state (timeout) is distinguished from the ready state (but they form 1 queue) • In the same ready queue, there are two states • Preemption can occur only when a process is about to move from kernel to user mode • Kernel code is not preemptable • To simplify switching, when the process resumes running, it runs in the user running (with empty kernel stack)

  13. UNIX Process State Diagram

  14. Context Switch • Context Switch Mechanism • The kernel permits it under four circumstances: • When a process puts itself to sleep • When it exits • When it returns from a system call to user mode • When it returns to user mode after interrupt handling • The kernel ensures integrity and consistency of internal data structures by prohibiting arbitrary context switches. • The procedure for a context switch is similar to the procedures for handling interrupts and system calls, except that the kernel restores the context layer of a different process instead of the previous context layer of the same process. • Steps for a Context Switch

  15. UNIX Process Image • User-level context • Process Text (ie: code: read-only) • Process Data • User Stack (calls/returns in user mode) • Shared memory (for IPC) • only one physical copy exists but, with virtual memory, it appears as it is in the process’s address space • Register context

  16. UNIX Process Image • System-level context • Process table entry • the actual entry concerning this process in the Process Table maintained by OS • Process state, UID, PID, priority, event awaiting, signals sent, pointers to memory holding text, data... • U (user) area • additional process info needed by the kernel when executing in the context of this process • effective UID, timers, limit fields, files in use ... • Kernel stack (calls/returns in kernel mode) • Per Process Region Table (used by memory manager) • Page tables defining code, stack, data regions

  17. UNIX System Processes • Process 0 is created at boot time and becomes the “swapper” after forking process 1 (the INIT process) • When a user logs in: process 1 creates a process for that user

  18. Processes in UNIX System • User Process • most processes on typical system • associated with users at a terminal • Daemon Process • not associated with any users • do system-wide functions • administration and control of networks, execution of time-dependent activities, line printer spooling, and so on • run at user mode and make system calls to access system service like user process • Kernel Process • execute only in kernel mode • process 0 spawns kernel process, such as page-reclaiming process vhand, and then becomes the swapper process • extremely powerful, not flexible

  19. A tree of processes on a typical Solaris

  20. UNIX Process Creation • Every process, except process 0, is created by the fork() system call • fork() allocates entry in process table and assigns a unique PID to the child process • child gets a copy of process image of parent: both child and parent are executing the same code following fork() • but fork() returns the PID of the child to the parent process and returns 0 to the child process

  21. C Program Forking Separate Process int 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); } } Note that system(“/bin/ls”) standard C library function is equivalent to fork() and then exec(“bin/ls”).

  22. Virtual Memory – Review • The memory referenced by a logical address is called virtual memory • is maintained on secondary memory (ex: disk) • pieces are brought into main memory only when needed • For better performance, the file system is often bypassed and virtual memory is stored in a special area of the disk called the swap space • larger blocks are used and file lookups are not used.

  23. Paging – Review • Typically, each process has its own page table. • Page tables are variable in length (depends on process size). Stored in main memory instead of registers. A single register holds the starting physical address of the page table of the running process.

  24. Address Translation in a Paging System – Review

  25. Sharing Pages of a text editor – Review

  26. Translation Lookaside Buffer – Review

  27. TLB Comments – Review • TLB use associative mapping hardware to simultaneously interrogates all TLB entries to find a match on page number • The TLB must be flushed each time a new process enters the Running state • The CPU uses two levels of cache on each virtual memory reference • first the TLB: to convert the logical address to the physical address • TLB is a special on-chip cache (other than L1,L2, L3 caches) • If no on-chip TLB, L1 will typically have it. • once the physical address is formed, the CPU then looks in the cache for the referenced word in L1, L2 and L3 Caches • L1 is the fastest and the most expensive, followed by L2, followed by L3

  28. L1 & L2 Caches -- Review

  29. Referencing a memory word -- Review

  30. Unix Processes & Region Virtual addresses of the regions are connected Only 4K bytes are shared, and not the whole region

  31. Layout of the Unix Kernel • Although the kernel executes in the context of a process, the virtual memory mapping associated with the kernel is independent of all processes. • The code and data for the kernel reside in the system permanently, and all processes share it. • The kernel page tables are analogous to the page tables associated with a process. • In many machines, the virtual address space of a process is divided into several classes, including system and user, and each class has its own page tables. • When executing in kernel mode, the system permits access to kernel addresses, but it prohibits such access when executing in user mode.

  32. Example of the virtual addresses of the kernel and a process regions • Kernel text/data (first two triples) • Kernel stack within process (third triple) • Process (text/data,stack)

  33. Ref count of processes accessing the current directly. Used when opening a local filenename, not starting with “/”. In case parent process or one of its ancestors did chroot This is logical copy. Text is shared and only reference count is copied.

  34. Parent Process File Table U Area Per Process Region Table Parent Data Open Files Current Directory Changed Root Parent User Stack . . . . . Kernel Stack Shared Text Inode Table U Area Per Process Region Table Child Data Open Files Current Directory Changed Root Child User Stack . . . . . Kernel Stack Child Process Fork Creating a New Process Context

  35. Inode of the executable file, with region type “Data”. Done also for code and stack.

  36. If the shell recognizes the input string as a built-in command (e.g., cd, for, while, …), it executes the command internally without creating new process; otherwise, it assumes the command is the name of an executable file. For commands with &, the shell does not wait but immediately restarts the loops and reads the next command line.

  37. We need first to understand system calls for the file systems.

  38. Contains also link counts and reference counts Introduction to Unix file system • Unix is an indexed file system • Every file on a UNIX system has a unique indode that contains the information necessary to for a process to access a file by a well defines system calls. • Processes specify the file by its path name. • Internally, the algorithm namei converts a user-level path to inode number. • Inodes are stored in a linear array on disk.

  39. Byte Capacity of a File • System V UNIX. Assume that • Run with 13 entries • 1 logical block : 1K bytes • Block number address : a 32 bit (4byte) integer • 1 block can hold up to 256 block number (1024byte / 4byte) • 10 direct blocks with 1K bytes each=10K bytes • 1 indirect block with 256 direct blocks= 1K*256=256K bytes • 1 double indirect block with 256 indirect blocks=256K*256=64M bytes • 1 triple indirect block with 256 double indirect blocks=64M*256=16G • Size of a file : 4G (232), if file size field in inode is 32bits

  40. A directory is a file whose data is a sequence of entries, each consisting an inode number and the name of a file. • If inode number is 0, it indicates that the entry is empty (i.e., available for use again).

  41. namei

  42. link count – hard links counts representing file names in directory hierarchy reference count (will see shortly) -- for opening and referencing files by processes

More Related