Download presentation
1
Lecture 6 & 7: PROCESSES Process states (PS)
Data structures to represent the PS Resource Ownership Execution
2
Lecture 6 & 7: PROCESSES The major requirements that the operating system must meet can all be expressed with reference to processes: The operating system must interleave the execution of a number of processes to maximize processor use while providing reasonable response time The operating system must allocate resources to processes in conformance with a specific policy (e.g. certain functions or applications are of higher priority) while at the same time avoiding deadlock The operating system may be required to support interprocess communication and user creation of processes, both of which may aid in the structuring of applications
3
Block Diagram of Kernel
User programs libraries User Level Kernel Level System call interface process control subsystem File subsystem Inter-process communication scheduler Buffer cache Memory management Character | block Device drivers Hardware control Kernel Level Hardware Level Hardware
4
Definition Mainly, a process can be in one of the following states:
Running: using a processor to execute instructions Ready: executable, but all the processors are currently in use Blocked: waiting for an event to occur For the OS A process state is represented by a data structure called Process Control Block (PCB) PCB describes the status of all resources the process uses The set of all PCB’s defines the current computation activities of the OS
5
Process Process is the execution of a program and consists of a pattern of bytes that the CPU interprets as machine instructions, data and stacks. The loaded process consists of three parts called regions: text, data ( correspond to sections of executable file) and the stack ( automatically created and its size is dynamically adjusted by the kernel at run time) Stack: Logical stack of frames that are pushed when calling a function and popped when returning Stack pointer: indicates the current stack depth Stack frame: parameters to a function, local variables, data necessary to recover the previous stack frame
6
User and Kernel Stack for copy program
User Stack Kernel Stack Local not Vars shown Direction of stack growth Addr of Frame 2 Ret addr after write call Parmss to write (new buffer count) Frame 3 Call write() Frame 3 Local count Vars Local Addr of Frame 1 Ret addr after copy call Ret addr after func2 call Parms to copy (old new) Frame Frame 2 Parms to kernel func2 Local fdold Vars fdnew call copy() call func2() Addr of Frame 0 Ret addr after main call Ret addr after func1 call Parms to main (argc argv) Frame Frame 1 Parms to kernel func1 call main() Frame 0 Start call func1() Frame 0 System call interface
7
Data structures for processes
Per process region table The u-area contains: A pointer to the process table slot of the currently executing process Parameters of the current system call, return values or error codes File descriptors for all open files Internal I/O parameters Current directory and current root Process and file size limits u area region table Process table Main memory
8
Process states 1 2 3 4 User running return Interrupt, interrupt return
Sys call or interrupt 2 Schedule process sleep 3 Asleep Context switch permissible 4 Ready to run Wake up
9
Process states (cont.) The process is currently executing in user mode
The process is currently executing in kernel mode The process is not executing but it is ready to run as soon as the schedule chooses it. Many processes may be in this state and the scheduling algorithm determines which one will execute next The process is sleeping. A process puts itself to sleep when it can no longer continue executing, such as when it is waiting for I/O to complete
10
Process states and Transitions (on a logical level)
The process is executing in user mode The process is executing in kernel mode The process is not executing but is ready to run as soon as the kernel schedules it The process is sleeping and resides in main memory The process is ready to run, but the swapper (process 0) must swap the process into main memory before the kernel can schedule it to execute The process is sleeping and the swapper has swapped the process to secondary storage to make room for other processes in main memory The process is returning from the kernel to user mode, but the kernel preempts it and does a context switch to schedule another process. The process is newly created and is in a transition state; the process exists, but it is not ready to run, nor is it sleeping. This state is the start state for all processes except process 0. The process executed the exit system call and is in the zombie state. The process no longer exists, but it leaves a record containing an exit code and some timing statistics for its parent process to collect. The zombies state is the final state of a process.
11
Process states 1 2 7 4 3 6 5 8 User running Return to user
Sys call or interrupt return Interrupt, interrupt return Kernel running 2 7 preempt preempted sleep reschedule process 4 3 Wake up Ready to run in memory Asleep in memory Swap out Enough memory Swap in Swap out Created 6 5 8 fork Wake up Not enough memory (swapping system only) Sleep, Swapped
12
Content of PCB The PCB contains the following information:
the name of the process the processor state (PC, register contents, Interrupt masks) the current process state the relative priority of a process for acquiring resources information about protection (e.g. main storage access control) virtual memory address translation maps information about other resources owned by the process accounting information
13
General structure of operating-system control tables
Memory Tables Process 1 Memory Devices Files Processes I/O Tables File Tables Process Tables Process 1 Process 2 … Process n Process n
14
Process table The state field identifies the process state
The process table entry contains fields that allow the kernel to locate the process and its u area in main memory or in secondary storage Several user identifiers determine various process privileges Process identifiers specify the relationship of processes to each other The process tabel entry contains an event descriptor when the process is in the “sleep” state Scheduling parameters allow the kernel to determine the order in which processes move to the states “kernel running” and “user running” A signal field enumerates the signals sent to a process but not yet handled Various time give process execution time and kernel resource utilisation, used for process accounting and for the calculation of process scheduling priority. One field is a user-set timer used to send an alarm signal to a process.
15
Process Operations Processes are fundamental OS-provided objects
The OS operations on processes include: create a process delete a process suspend a process inter-process communication inter-process synchronisation create/delete a sub-process
16
Reasons for Process creation:
New batch job Interactive log on Created by the OS to provide a service Spawned by existing process
17
Reasons for process termination:
Normal completion Time limit exceeded Memory unavailable Bounds violation Protection error Arithmetic error Time overrun I/O failure Invalid instruction Privileged instruction Data misuse Operator or OS intervention Parent request
18
Process Operations Somme special operations on processes can be performed by the OS and other processes to change the process status or other information in a PCB: Create: adds a new PCB with appropriate initial information Delete: removes the identified process from the system Signal: indicates that a specific event has occurred Wait: causes execution to cease until a specific event occurs Schedule: reassigns runnable process to the available processors Change-priority: alters the relative priority of a process Suspend: makes a running, ready, or blocked process suspended Resume: changes a suspended process to unsuspended one
19
Process Operations (ctd)
Remarks An event is a change in the status of any entity that a process uses to execute Schedule operation is often performed as a result of a change in system state that may influence which processes should be currently running The change-priority operation is often called short-term scheduling or dispatching
20
Process State Diagram signal resume suspend Non- existent delete
Suspended Blocked Ready Running signal wait delete create suspend resume schedule
21
Independent Processes
Those that can neither affect nor be affected by the rest of the system Example: processes running on different non-networked computers Properties: No sharing state and data between processes Deterministic: only the input state determines the results Reproducible Can be stopped and restarted with no bad effects
22
Co-operative Processes
Processes that share state. Not necessarily “co-operating” Example: Processes that share a single file system Properties Behaviour is not deterministic May be irreproducible (very bad: consider testing & debugging) Subject to race conditions
23
Co-operating Processes
Why allowing processes to co-operate? Want to share resources one computer, many users one checking account file, many tellers Want to do things faster read next block while processing current one divide jobs into sub-jobs, execute them in parallel Want to design systems in modular way Unix example: tbl | eqn | troff
24
Milk Problem Person A Person B 10:00 Check the fridge; out of milk
10:05 10:10 10:15 10:20 10:25 Check the fridge; out of milk Leave for the shop Arrive to the shop; buy milk Arrive home; put milk in ... Check the fridge; out of milk Leave for the shop Arrive to the shop; buy milk Arrive home; OH NO!
25
Milk Problem (ctd) Mutual Exclusion Critical Section
mechanism that ensure that only one process at a time holds a resource or modifies shared information only one person goes to the shop Critical Section a section of code in a process in which some shared resources are referenced shopping is a set of operations (choose milk, pay milk, …) There are many ways in achieving mutual exclusion. Most involve some sort of locking mechanisms: prevent someone from doing something. Before shopping, leave a note on the fridge
26
Milk Problem (ctd) Three elements of locking
must lock before using -- leave note must unlock when done -- remove note must wait if locked -- no shopping if note ME should satisfy the following requirements Any process that requests entry to a free CS must be permitted to enter without delay The non selected process cannot be postponed indefinitely No deadlock or starvation can be allowed
27
Milk Problem (1st Attempt)
2 3 4 5 6 7 If (No_Milk) { if (No_Note) { Put a Note; Buy Milk; Remove the Note; } Both process A and process B execute this program Does this program work? What happens if we put the note at the beginning?
28
Milk Problem (2nd Attempt)
Change the meaning of the note: 1 2 3 4 5 6 If (No_Note) { if (No_Milk) { Buy Milk; } Put a Note; If (Note) { Remove the Note; Process A Process B Does this work? Assume B goes on vacation, what will happen?
29
Milk Problem (3rd Attempt)
Use two notes: 1 2 3 4 5 6 7 Put Note_A; If (No_Note_B) { if (No_Milk) { Buy Milk; } Remove Note_A; Put Note_B; If (No_Note_A) { Remove Note_B; Process A Process B The second attempt stores the name of the process that may enter its CS, but not the state information about the processes. Each process needs its own fridge key, so that if one fails the other can still access its CS. Each process may examine the other note, but cannot alter it. Suppose that the process B goes on vacation, the process In the beginning A will be able to buy milk and leave the note and will be blocked until the process B returns (removes the note). This solution is dependent of relative speeds of the process execution. How this solution works? Solution is “ok”. Need to decide who buys when both they leave notes…
30
Milk Problem (4th Attempt)
1 2 3 4 5 6 7 8 9 10 Put Note_A; If (No_Note_B) { if (No_Milk) Buy Milk; else { While (Note_B) {}; if (No_Milk) Buy Milk; } Remove Note_A; Process A Put Note_B; If (No_Note_A) { if (No_Milk) { Buy Milk; } Remove Note_B; Process B In A3, when deadlock appears there is no chance to take off this position. Because a process sets its state without knowing the state of the other. To fix this we try to make each process more deferential. Process B is the same as in the 3rd attempt. While the process A is waiting, it is time consuming for the processor.
31
Mutual Exclusion We need to add synchronisation, so the access to a shared resource can be controlled Synchronisation section is called Critical Section Critical Section an atomic operation controls the access to it only one process at a time can be inside the critical section the others are forced to wait
32
Message System Message system is a mechanism for inter-process communication Until now, processes communicate by using shared data Message system provide communication without shared data Message: information that can be exchanged between two (or more) processes Mailbox: a place where messages are stored between the time they are sent and the time they are received
33
Message System (ctd) Operations Addressing
Send: place a message in a mailbox. If the mailbox is full, wait until there is enough space Receive: remove a message from a mailbox. If the mailbox is empty, wait until a message has arrived Addressing Send: specify the process that is being to receive the message Receive: indicate the source of a message to be received
34
Message System (ctd) Process A Process B Messages Mailbox Send Receive
35
Message System (Example)
Process A Process B Characters Pipe write read
36
Synchronisation There are two levels of synchronisation between 2 processes The receiver cannot receive a message until it has been sent by another process The need to specify what happens to a process after it issues a send or receive primitive
37
Message Format Source Destination Message Length Control Info.
Message Type Message Contents Header Body Depends on the objectives of the message facility: Fixed-length: minimise processing and storage overhead variable-length: more flexible approach for operating systems
38
Mutual Exclusion Processes share a mailbox
We assume a blocking receive and non-blocking send If more than one process performs the receive primitive concurrently, then if the mailbox is not empty, the messages are delivered to the processes according the process queuing policy if the mailbox is empty, all processes are blocked. Each message arriving will activate one process
39
Mutual Exclusion (ctd)
Parent process Create Mailbox(M); send(M, null); Parallel P(1); P(2); …; P(N); End Parallel; End Process P(int) Loop receive(M, msg); <Critical section>; send(M, null); End Loop
40
Block Diagram of Kernel
User programs libraries User Level Kernel Level System call interface process control subsystem File subsystem Inter-process communication scheduler Buffer cache Memory management Character | block Device drivers Hardware control Kernel Level Hardware Level Hardware
41
The u area A pointer to the process table identifies the entry that corresponds to the u-area The real and effective user Ids determine various privileges allowed the process, such as file access rights Timer fields record the time the process spent executing in user more and in kernel mode An array indicates how the process wishes to reactto signals The control terminal field identifies the “login terminal” associated with the process, if one exists An error field records errors encountered during a system call A return value field contains the result of system calls I/O parameters describe the ammount of data to transfer, the address of the source data array in user space, file offsets for I/O, and so on The current directory and current root describe the file system environment of a process The user file descriptor table records the files the process has open Limit fields restrict the size of a process and the size of a file it can write A permission modes field masks mode settings on files the process creats
42
Layout of system memory
Assume that the physical memory of a machine is addressable, starting at byte offset 0 and going up to a byte offset equal to the ammount of memory of the machine UNIX system contains threee logical sections: text, data and stack The text section contains the set of instructions the machine executes of a process; address in the text section include text addresses, data addresses and stack addresses The compiler generates addresses for a virtual address space with a given address range and the machine’s memory management unit translates the virtual addresses generated by the compiler into address locations in physical memory. The subsystems of the kernel and the hardware that cooperate to translate virtual to physical addresses comprise the memory management subsystem.
43
Regions The system V kernel divides the virtual address space of a process into logical regions. A region is a contigious area of the virtual adress space of a process that can be treated as a distinct object to be shared or protected. (thus text, data and stacks form separate regions) Assume the region table contains the information to determine where its contents are located in physical memory Pregion: Private per process region table! Each pregion entry points to a region table entry and contains the starting virtual address of the regioni n the process and a permission field that indicates the type of access allowed to a process The concept of the regions is independent of the memory management policies implemented by the operating system
44
Processes and Regions 8K 16K 32K 4K 8K 32K
Per Proc Region Tables (Virtual Addresses) Regions Text 8K 16K 32K b Process A Data c Stack a Text 4K 8K 32K e Data Process B Stack d
45
Pages and Page tables In a memory management architecture based on pages, the memory management hardware divides physical memory into a set of equal sized blocks called pages Typical pages range from 512 bytes to 4K bytes and are defined by hardware Every addressable location in memory is contained in a page and consequently every memory location can be addressed by a: (page number, byte offset in page) pair Example: if a machine has 232 bytes of physical memory and a page size of 1K bytes, it has 222 pages of physical memory; every 32-bit address can be treated as a pair consisting of a 22-bit page number and a 10-bit offset into the page When the kernel assigns physical pages of memory to a region, it need not assign the pages contiguously or in a particular order. The purpose of paged memory is to allow greater flexibility in assigning physical memory, analogous to the assignment of disk blocks to files in a file system The kermel correlates the virual address of a region to their physical machine addresses by mapping the logical page numbers in the region to physical page numbers on the machine The region table entry contains a pointer to a table of physical page numbers called a page table
46
Mapping Virtual Addresses to Physical Addresses
Per Proc Region Tables (Virtual Address) Page Tables (Physical Address) Text 8K 32K 64K empty 137K 852K ………… Data Stack 87K 552K 727K ………… 541K 783K 986K …………
47
Process management within LINUX!
A process is the basic context within which all user-requested activity is serviced within the operating system. To be compatible with other UNIX systems, Linux must use a process model similar to those of other versions of UNIX. Linux Operates differently from UNIX in a few key places.
48
The Fork/Exec Process Model
The basic principle of UNIX process management is to separate two distinct operations: the creation of processes (fork system call) and the running of a new program (execve) Under LINUX, we can break, we can break down this context into a number of specific sections. Broadly, process properties fall into three groups: the process identity, environment and context
49
Process Identity Process ID(PID): specify processes to the operating system when an application makes a system call to signal, modify or wait for another process Credentials: Each process must have an associated user ID and one or more group IDs that determine the rights of a process to access system resource and files Personality: Not found in UNIX systems, but under LINUX each process has an associated personality identifier that can modify slightly the semantics of certain system calls.
50
Process Environment A process’ environment is inherited from its parent and is composed of two vectors: Argument vector: simply lists the command line arguments used to invoke the running program and conventionally starts with the name of the program itself Environment vector: is a list of “NAME=VALUE” pairs that associates named environment variables with arbitrary textual values
51
Process context It is the state of the running program at any one time; it changes constantly Scheduling context Accounting File table File system context Signal handler table Virtual memory context
52
Processes and Threads! What is the difference?
How LINUX kernel deal with Processes and Threads? “fork” and “clone” system call Process synchronization using a credit-based algorithm
53
No tutorials for Week 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.