View
226
Download
2
Embed Size (px)
Citation preview
1
Last Time:OS & Computer Architecture
Modern OS Functionality (brief review)
Architecture Basics Hardware Support for OS
Features
2
Last Time: Services & Hardware Support
OS Service Hardware Support
Protection
Interrupts
Traps
I/O
Synchronization
Virtual Memory
Scheduling
5
Layered OS Design
+ Modular, simple, portable, easy to design/debug
- Communication overhead, copying, bookkeeping
“THE” operating system Dijkstra
7
Microkernel: Motivation
Minimize contents of kernel: Reduces risk of crashing OS
Put as much functionality as possible in user-level processes
Simplifies extension, modification & customization
First μ-kernel: Hydra (CMU) Current systems: Mach (also CMU),
by Rick Rashid et al. (now head of MSR)
8
μ-kernels vs. Monolithic Kernels
Past conventional wisdom: (1990’s) Mach – beautiful research idea but “failed”
in practice Too slow!
Linux – ugly, monolithic, but fast
Today: much faster computers Mach: fast enough (Mac OS X)
Reliability, simplicity, robustness now more important than performance
10
Process
OS manages variety of activities: User programs, batch jobs, command
scripts Daemons: print spoolers, name
servers, file servers, etc.
Each activity encapsulated in process: Context (PC, registers, etc.) required
for activity to run
11
Process != program
Process is a running program One program may comprise
many processes Many processes may run same
program
12
OS and Processes
OS manages & schedules processes Creation, deletion Resource allocation (e.g., CPU,
memory) Suspension & resumption Inter-process communication Synchronization
13
Processes
Process Concept Process States Process Scheduling Process Management Interprocess Communication
14
Process Concept
Process – program in execution Process includes:
text section: program code current activity: program counter,
contents in registers stack data section heap
15
Processes
Process Concept Process States Process Scheduling Process Management Interprocess Communication
16
Process States
New Process being created
Running Instructions being executed
Waiting Process waiting for some event to occur
Ready Process waiting to be assigned to a
processor Terminated
(duh)
17
Process State Diagram Transitions:
Program actions (system calls)
OS actions (scheduling)
External events (interrupts)
18
Process Execution Example
void main() { printf (“Hello world\n”);}
1. New2. Ready3. Running4. Waiting for I/O5. Ready6. Running7. Terminated
19
Process Data Structures Process Control Block:
Tracks state OS allocates, places on
queue OS deallocates on
termination
Lots of info: Process state Program counter CPU registers Memory-management
information Accounting information I/O status information
20
Processes
Process Concept Process States Process Scheduling Process Management Interprocess Communication
21
Process Scheduling Queues
Job queue Set of all processes in system
Ready queue Set of processes residing in main memory
ready & waiting to execute Device queues
Set of processes waiting for I/O device One per device
Process migration between the various queues.
24
PCBs and Hardware State
Switching processes: context switch Relatively expensive
Time between switches (quantum) must be long enough to amortize this cost
Start: OS picks ready process Loads register values from PCB
Stop: OS saves registers into PCB
26
Processes
Process Concept Process States Process Scheduling Process Management Interprocess Communication
27
Process Creation
One process can create other processes Creator = parent, new processes = children Parent can wait for child to complete,
or continue in parallel
UNIX: fork() used to create child processes Copies variables & registers from parent to
child Only difference between parent & child: return
value Parent: returns process id of child Child: returns 0
28
Process Creation Example
Logging into UNIX creates shell process Every command typed into shell:
Child of shell process (spawned by fork) Executes command via exec
Example: Type “emacs” OS forks new process exec executes emacs If followed by “&”, runs in parallel;
otherwise, waits until done
30
Fork Example: What happened?
#include <unistd.h>#include <sys/wait.h>#include <stdio.h>int main(){
int parentID = getpid();char prgname[1024];gets(prgname);int cid = fork();if(cid == 0){
execlp(prgname, prgname, 0); printf(“I did not find program %s\n“, prgname);}else{ sleep(1);
waitpid(cid, 0, 0); printf("Program %s is finished.
\n");}return 0; }
Parent process:#include <unistd.h>#include <sys/wait.h>#include <stdio.h>int main(){
int parentID = getpid();char prgname[1024];gets(prgname);int cid = fork();if(cid == 0){
execlp(prgname, prgname, 0); printf(“I did not find program %s\n“, prgname);}else{ sleep(1);
waitpid(cid, 0, 0); printf("Program %s is finished.
\n");}return 0; }
Child process:
31
Parent & Child Process#include <unistd.h>#include <stdio.h>
int main(){int pid;pid = fork();if(pid == 0){
printf("Child: My PID = %d\n", getpid());printf("Child: Running...\n");sleep(20);printf("Child: Done sleeping, returning.\n");
}else{
printf("Parent: My PID = %d\n", getpid());printf("Parent: Running...\n");sleep(10);printf("Parent: Done sleeping, returning.\n");
}return 0;
}
32
Process Termination
On termination, OS reclaims all resources assigned to process
UNIX processes: Can terminate self via exit system
call Can terminate child via kill system
call
34
Processes
Process Concept Process States Process Scheduling Process Management Interprocess Communication
35
Cooperating Processes
Cooperating processes: one process can affect or be affected by other processes
Advantages: Can improve performance by overlapping
activities or performing work in parallel Can enable simpler program design Simplifies distribution
Processes can live on different machines
36
Interprocess Communication
Processes communicate in one of two ways:
Message passing: Send and receive information Numerous means: sockets, pipes, etc.
Shared memory: Establish mapping to named memory
object Use mmap Fork processes that share this structure
37
Process Summary
Process = unit of execution Represented by Process Control Blocks
Contain process state, registers, etc. Process state:
New, Ready, Waiting, Running, or Terminated One running process at a time (on a
uniprocessor) Context switch when changing process
executing on CPU Communicate by message passing or shared
memory