View
219
Download
0
Tags:
Embed Size (px)
Citation preview
Lecture 6 & 7:PROCESSES
• Process states (PS)
• Data structures to represent the PS
• Resource Ownership
• Execution
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
Block Diagram of Kernel
Hardware
Hardware controlKernel Level Hardware Level
libraries
System call interface
File subsystem
Buffer cache
Character | block
Device drivers
process
control
subsystem
Inter-process communication
scheduler
Memory management
User programs
User Level
Kernel Level
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
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:a. Logical stack of frames that are pushed when calling a function
and popped when returningb. Stack pointer: indicates the current stack depthc. Stack frame: parameters to a function, local variables, data
necessary to recover the previous stack frame
User and Kernel Stack for copy program
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
Vars
Addr of Frame 1 Addr of Frame 1
Ret addr after copy call Ret addr after func2 call
Parms to copy (old new) Frame 2 Frame 2 Parms to kernel func2
Local fdold
Vars fdnew
call copy() call func2() Local
Vars
Addr of Frame 0 Addr of Frame 0
Ret addr after main call Ret addr after func1 call
Parms to main (argc argv) Frame 1 Frame 1 Parms to kernel func1
User Stack Kernel Stack
call main()
Frame 0
Start
call func1()
Frame 0
System call interface
Data structures for processes
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
Main memoryProcess table
Per process region table
region table
Process states
1
2
34
User running
Sys call or interrupt
return Interrupt, interrupt return
Schedule process
Ready to runWake up
sleep
Asleep
Context switch permissible
Process states (cont.)
1. The process is currently executing in user mode
2. The process is currently executing in kernel mode
3. 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
4. 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
Process states and Transitions(on a logical level)
1. The process is executing in user mode2. The process is executing in kernel mode3. The process is not executing but is ready to run as soon as the kernel schedules it4. The process is sleeping and resides in main memory5. 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 execute6. The process is sleeping and the swapper has swapped the process to secondary
storage to make room for other processes in main memory7. The process is returning from the kernel to user mode, but the kernel preempts it and
does a context switch to schedule another process.8. 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.
9. 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.
Process states1
2
34
User running
Sys call or interrupt
return
Interrupt, interrupt return
reschedule process
Ready to run in memory
Wake up
sleep
Asleep in memory
7
Return to user
Kernel running
preempt
preempted
6 5 8
Swap out
Sleep, Swapped
Wake up
Swap out
Swap in
Not enough memory (swapping system
only)
Enough memory
Created
fork
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
General structure of operating-system control tables
Memory
Devices
Files
Processes
Memory Tables
I/O Tables
File Tables
Process 1
Process 2
…
Process n
Process Tables
Process 1
Process n
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.
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
Reasons for Process creation:
• New batch job
• Interactive log on
• Created by the OS to provide a service
• Spawned by existing process
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
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
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
Process State Diagram
Non-existent
SuspendedBlocked
SuspendedReady
Ready
Running
Blocked
signal
signal
wait
delete
deletedelete
deletecreate
susp
end
resu
me
dele
te
suspendschedule
susp
end
resu
me
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
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
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
Milk Problem
10:0010:0510:1010:1510:2010:25
Check the fridge; out of milkLeave for the shopArrive to the shop; buy milkArrive home; put milk in ...
Check the fridge; out of milkLeave for the shopArrive to the shop; buy milkArrive home; OH NO!
Person A Person B
Milk Problem (ctd)
• Mutual Exclusion– 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
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
Milk Problem (1st Attempt)
• Does this program work?• What happens if we put the note at the beginning?
1234567
If (No_Milk) {if (No_Note) { Put a Note; Buy Milk; Remove the Note;}
}
Both process A
and process Bexecute this
program
Milk Problem (2nd Attempt)
• Does this work?• Assume B goes on vacation, what will happen?
Change the meaning of the note:
123456
If (No_Note) {if (No_Milk) { Buy Milk;}
Put a Note;}
If (Note) {if (No_Milk) { Buy Milk;} Remove the Note;
}
Process A Process B
Milk Problem (3rd Attempt)
• How this solution works?• Solution is “ok”. Need to decide who buys when both they leave notes…
Use two notes:
1234567
Put Note_A;If (No_Note_B) {
if (No_Milk) { Buy Milk;
}}Remove Note_A;
Put Note_B;If (No_Note_A) {
if (No_Milk) { Buy Milk;
}}Remove Note_B;
Process A Process B
Milk Problem (4th Attempt)
12345678910
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
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
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
Message System (ctd)• Operations
– 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
Message System (ctd)
Process A Process B
Messages
MailboxSend Receive
Message System (Example)
Process A Process B
Characters
Pipewrite read
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
Message Format• Depends on the objectives
of the message facility:
– Fixed-length: minimise processing and storage overhead
– variable-length: more flexible approach for operating systems
Source
Destination
Message Length
Control Info.
Message Type
Message Contents
Hea
der
Bod
y
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
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
Block Diagram of Kernel
Hardware
Hardware controlKernel Level Hardware Level
libraries
System call interface
File subsystem
Buffer cache
Character | block
Device drivers
process
control
subsystem
Inter-process communication
scheduler
Memory management
User programs
User Level
Kernel Level
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
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.
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
Processes and Regions
8K
16K
32K
4K
8K
32K
Per Proc Region Tables (Virtual Addresses)
Text
Data
Stack
Text
Data
Stack
Process A
Process B
b
a
d
c
e
Regions
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
Mapping Virtual Addresses to Physical Addresses
8K
32K
64K
Text
Data
Stack
Per Proc Region Tables (Virtual Address)
541K
783K
986K
…………
87K
552K
727K
…………
empty
137K
852K
…………
Page Tables (Physical Address)
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.
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
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.
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
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
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
No tutorials for Week 5