文章目录
- Modern Operating System
- Chapter1 Introduction
- Chapter2 Processes and Threads
- Processes
- Threads
- Scheduling
- Interprocess communiction
- Classical IPC Problems
Modern Operating System
Chapter1 Introduction
What is an operating system?
- Top-down view : An extended machine
- Bottom-up view: A resource manager
History of OS
- 1945-55 vacuum tubes
- 1955-65 Transistors and Batch systems
- 1965-1980 ICs and Multiprogramming 集成电路和多道程序设计
- 1980-Present Personal Computers
- 1990-Present Mobile Computers
OS Zoo
- Mainframe OS
- Server OS
- Multiprocessor OS
- PC OS
- Real-Time OS
- Embedded OS 嵌入式系统
Parallelism vs Concurrency 并行和并发
- Parallelism : An application whose tasks are divided into smaller sub-tasks that are processed seemingly simultaneously or parallel.
- Concurrency : An application that is processing more than one task at the same time.
- 说人话: 并发:宏观上同步 微观上交替 并行: 多个任务完全同时发生,没有宏微观区分
Interrupts 中断
-
An interruption of normal sequence of execution
-
Allow the processor to execute other instructions
-
Classes of Interrupts
- Program
- arithmetic overflow
- division by zero
- execute illegal instruction
- reference outside user’s memory space
- Time
- I/O
- Hardware failure
- Program
-
other contents will be diccussed in other chapters in details , so not list here
Chapter2 Processes and Threads
Processes
- A process is just an instance of an executing program,including the current values of the program counter,registers, and variables. P86 the definition of process
- This chapter assume only one CPU. CPU switch back and forth among processes. Multicores and Multiprocessors will discuss in chapter8 in detail.
- It is worth noting that if a program is running twice,it counts as two processes.
Process Creation
- System initialization
- Execution of a process-creation system call by a running process System call fork()
- A user request to create a new process
- Initiation of a batch job
Process Termination
-
Normal exit (voluntary主动)
execute all the instructions and exit
-
Error exit (voluntary)
-
Fatal error (involuntary)
-
Killed by another process (involuntary)
In Linux kill -9 + PID
Process States
-
Running (using the CPU right now)
-
Ready (runnable; get all its resources except CPU)
-
Blocked (unable to run waiting something to happen)
for example : waiting for user’s input or waiting for the printer which is used by other process
-
other version
When to switch a process?
-
Clock interrupt
P390 5.5.2 One function of clock is preventing processes from running longer than they are allowed to. When a process use up the time being assigned, the clock send a interrupt to let other process to run.
-
I/O interrupt
when waiting for I/O,CPU is idle. And user input is exceedingly slow. So just switch
-
Memory fault
-
Trap
- error occurred
- may cause process to be moved to Exit state
-
Supervisor call
Implementation of Processes
-
The OS maintain a table, called Process table, one entry per process. these entry also called PCB (Process control blocks).
-
This entry contains information about the process’ state, its program counter, stack pointer, memory allocation, the status of its open files, its accounting and scheduling information, and everything else about the process that must be saved when the process is switched from running to ready or blocked state so that it can be restarted later as if it had never been stopped.
-
-
Program Counter
usually called PC. always store the address of next instruction
-
Program status word
If you have learned Computer Organization or you used to write Assembly language, you must be familiar with this . It consists of CF(carry flag) ZF(zero flag) OF(overflow flag) …
-
Process ID
short in PID. In Linux ,use command ps aux to look up. In windows ,use Task Manager
-
Threads
-
Three reason to have thread :
- By decomposing application into multiple sequential threads that run in quasi-parallel.
- lighter weight than process,and easier.
- performance argument
-
Thread consists of :
- PC
- registers
- stack
-
Processes are used to group resources together,threads are the entities scheduled for execution on the CPU
-
- All threads share process resources.
Implementing threads
In user space
-
-
Advantages:
- A user-level threads package can be implemented on an OS that does not support threads
- Allow each process to have its own customized scheduling algorithm
In the kernel
-
-
The kernel has a thread table that keeps track of all the threads in the system
-
When a thread blocks, the kernel can run either another thread from the same process, or a thread form a different process.
Hybrid implementations
Scheduling
- Multiple processes competing for the CPU at the same time
- Scheduler: choose which process to run next
- Many issues that apply to process scheduling also apply to thread scheduling
two types of process
-
-
A CPU-bound process An I/O-bound process
When to schedule?
- A new process is created
- A process exit
- A process block on I/O, on a semaphore
- An I/O interrupt occurs.
Two types of scheduling
- nonpreemptive
- preemptive
Scheduling Algorithm Goals
- All systems
- Fairness
- Policy enforcement
- Balance
- Batch systems
- Throughput - maximize jobs per hour
- Turnaround time
- CPU utilization - keep CPU busy all the time
- Interactive systems
- Response time - respond to requests quickly
- Proportionality 均衡性 - meet users’ expectations
- Real-time systems tasks must be done before deadline
- Meeting deadlines - avoid losing data
- Predictability
Scheduling in batch systems
FCFS First come First served
- Advantage : easy to understand and easy to program
- Dis: poor CPU utilization
Shortest Job First
-
-
a Turnaround time: A(8), B(12), C(16), D(20) Ave: 14s
b Turnaround time: A(20), B(4), C(8), D(12) Ave: 11s
Shortest remaining time next scheduling 最短剩余时间优先
- A preemptive version of Shortest job first
Scheduling in Interactive systems
Round-Robin Scheduling
-
one of the oldest , simplest , fairest , and most widely used
-
Each process is assigned a time interval ,called its quantum 时间片
-
setting the quantum too short causes too many process switches and lowers the CPU efficiency ,but setting it too long may cause poor response to short requests .A quantum around 20-50 ms is often a reasonable compromise
Priority scheduling
- each process is assigned a priority, and the runnable process with the highest priority is allowed to run.
- Priority can be assigned to process statically or dynamically
Shortest process next
- Shortest job first always produces the min average response time
- The problem is to figure out which one is the shortest one
- One approach is to make estimates based on past behavior and run the process with the shortest estimated running time
- also called aging Algorithm
- P178 49 u can practice
Multiple queues
-
Guaranteed scheduling
-
Lottery scheduling
-
Fair-share scheduling
-
When to analyze scheduling , use Gantt chart!!!
Scheduling in Real-time systems
-
-
e.g. Three periodic events, with periods of 100, 200 and 500 msec, respectively. If these events require 50, 30, and 100 msec of CPU time per event. The system is schedulable because 0.5+0.15+0.2<1
-
Thread Scheduling
Interprocess communiction
-
Communicate with others not using interrupts
-
Three issues
- How one process can pass information to another
- Making sure two or more processes do not get into each other’s way when engaging in critical activities.
- proper sequencing read after write
Race conditions
-
In some OS,processes that are working together may share some common storage that each one can read and write.
-
-
Spooler directory has a number of slots . out points the next file to be printed . in points the next free slot.
-
problem
- More or less simultaneously, process A and B decide they want to queue a file to print
- A reads in and store the value, 7, in a local variable. A clock interrupt occurs and switch to B
- B read in and also store 7. B put its file name to slot 7 and update in to be 8 . then do other tasks
- eventually A come back. A reads local variable and find it 7 .write its file name to slot 7.erasing what B put in.
Critical Regions
- Mutual exclusion : prohibit more than one process from reading and writing the shared data at the same time.
- The part of the program where the shared momory is accessed is called the critical region
- Four conditions to provide Mutual exclusion
- No two processes may be simultaneously inside their critical region
- No assumptions may be made about speeds or the number of CPUs
- No process running outside its critical region may block any process
- No process should have to wait forever to enter its critical region
Proposals for achieving mutual exclusion
Disabling interrupts
- On a single-processor system, Let each process disable all interrupts just after entering its critical regions and re-enable them just before leaving it.
- unwise to give user processes the power to turn off interrupts
- Not work in multiple CPU system
Lock Variables
- software solution . Having a shared variable .Initially 0. V=0 get in .V=1,wait
- Fatal flaw : one process reads the lock and see it is 0.Before setting it to 1,another process is scheduled.
Strict Alternation
-
// Process 0 while(1){ while(turn !=0); //loop critical_region(); turn =1; noncritical_region(); } //process 1 while(1){ while(turn != 1); //loop critical_region(); turn =0; noncritical_region(); }
-
Continuously testing a variable until some value appears is called busy waiting
-
A lock that uses busy waiting is called a spin lock.自旋锁
-
This situation violates condition 3. A fast process have to wait another
Peterson’s Solution
-
#define False 0 #define TRUE 1 #define N 2 //number of processes int turn; //whose turn int interested[N]; //initially 0 void enter_region(int process) { int other; other=1-process; interested[process]=TRUE; turn = process; while(turn==process&&interested[other]==TRUE); } void leave_region(int process){ interestedp[process]=FALSE; }
-
assume both processes call enter_region almost simultaneously. assume process1 last execute turn = process ,turn is 1; process 0 jump out while and enter critical region .process 1 loops and does not enter its critical region until process 0 leaves its critical region.
TSL instruction
-
TSL : Test and set lock
-
;use TSL enter_region: TSL REGISTER,LOCK ;copy lock to register and set lock to 1 CMP REGISTER,#0 ;compare to 0 JNZ enter_region ;if lock is 1 .loop RET leave_region: MOVE LOCK,#0 ;set lock 0 RET
-
The CPU executing the TSL instruction locks the memory bus to prohibit other CPUs from accessing memory until it is done.
-
;use XCHG enter_region: MOVE REGISTER,#1 ;put 1 into register XCHG REGISTER,LOCK ;swap contents of register and LOCK CMP REGISTER,#0 ;was LOCK zero? JNZ enter_region ;if it was not zero,lock was set.so loop RET
Sleep and Wakeup
- Summary : both Peterson and TSL instruction are correct, but both have defect of busy waiting.
- Waste CPU time
- lower priority process can not quit critical region
- IPC primitives:
- sleep : Sleep is a system call that causes the caller to block. Be suspended until another process wakes it up.
- wakeup : The wakeup call has one parameter,the process to be awakened .
The Produce-Consumer problem
-
#define N 100 //number of slots in the buffer int count =0; //number of items in the buffer void produce(void){ int item; while(TRUE){ item = produce_item(); //produce item if(count == N) sleep(); //buffer if full , go to sleep insert_item(item); //put item into buffer count=count+1; if(count==1) wakeup(consumer);//if not empty,wakeup consumer } } void consumer(void){ int item; while(TRUE){ if(count==0) sleep(); //if empty ,sleep item=remove_item(); count=count-1; if(count==N-1) wakeup(produce); consume_item(item); } }
-
Race condition:
- The buffer is empty, the consumer has just read count to see if it is 0.
- At that time, the scheduler decides to stop consumer and start producer.
- producer insert one item into buffer, call wakeup to wake consumer up. but consumer is not asleep.
- wakeup signal lost. eventually both consumer and produce sleep forever.
-
probable solution : wakeup waiting bit
Semaphores
-
Dijkstra suggested using a integer variable to count the number of wakeups saved for future use.
-
Atomic action:
-
//P(S) S=S-1; if(s>=0) 该进程继续运行. else 排入等待队列 //V(S) S=S+1; if(S>0) 该进程继续运行 else 释放等待队列的第一个进程
-
Using semaphores solving produce-consumer problem
-
#define N 100 //slots in buffer typedef int semaphore; //semaphore = int semaphore mutex = 1; //control access to critical region semaphore empty = N; //counts empty buffer slots semaphore full = 0; //count full buffer slots void produce(void){ int item; while(TRUE){ item = produce_item(); //produce item down(&empty); //decrement empty count down(&mutex); //enter critical region insert_item(item); //put item into buffer up(&mutex); //leave critical region up(&full); //increment full slots } } void ci=onsumer(void){ int item; while(TRUE){ down(&full); //decrement full count down(&mutex); //enter critical region item=remove_item(); //take item from buffer up(&mutex); //leave critical region up(&empty); //increment empty slot consumer_item(item); //consume item } }
Mutexes
-
A mutex is a variable that can be in one of two states: unlocked or locked
-
mutex_lock: TSL REGISTER,MUTEX CMP REGISTER,#0 JZ ok ;if mutex is unlocked .so return CALL thread_yield JMP mutex_lock ok: ret ;enter critical region mutex_unlock: MOVE MUTEX,#0 //set mutex 0 RET
Monitors 管程
-
A monitor is a collection of procedures, variables, and data structures that are all grouped together in a special kind of module or package.
-
Monitors have an important property that makes them useful for achieving mutual exclusion only one process can be active in a monitor at any instant.
-
Java supports user-level threads . By adding the keyword synchronized to a method declaration,Java guarantees that once an thread has started executing that method, no other thread will be allowed to start executing any other synchronized method of that object.
-
// a solution to Producer-consumer problem in JAVA public class ProduceConsumer{ static final int N=100; static producer p = new producer(); static consumer c= new consumer(); static our_monitor mon= new our_monitor(); public static void main(String[] args){ p.start(); c.start(); } static class producer extends Thread{ public void run(){ int item; while(true){ item=produce_item(); mon.insert(item); } } private int produce_item(){ //... } } static class consumer extends Thread{ public void run(){ int item; while(true){ item=mon.remove(); consume_item(item); } } private void consume_item(int item){ //... } } static class our_monitor{ private int buffer[] =new int[N]; private int count=0,lo=0,hi=0; public synchronized void insert(int val){ if(count==N) go_to_sleep(); buffer[hi]=val; hi=(hi+1)%N; count=count+1; if(count==1) notify(); } public synchronized int remove(){ int val; if(count==0) go_to_sleep(); val=buffer[lo]; lo=(lo+1)%N; count=count-1; if(count==N-1) notify(); return val; } private void go_to_sleep(){ try{ wait(); }catch(InterruptedException E){ } } } }
-
Drawback
- Monitor is a language mechanism. C, Pascal and other language do not have monitors.
- Multiple CPUs,each with its own private memory,these primitives become inapplicable.
Message Passing
-
Use two primitives , send and receive, are system calls.
-
send(destination , &message); receive(source, &messge);
-
Design issues
- messages can be lost by the network.
- acknowledgement message to the sender is lost. The sender retransmit message.
- How processes are named
- Authentication.
-
Mailbox
- A mailbox is a place to buffer a certain number of messages.
- When mailbox is used, the parameter destination and source are mialboxes.
- The producer send messages to consumer’s mailbox. The consumer send messages to producer’s mailbox.
Barriers
- make groups of processes synchronized
Classical IPC Problems
The Dining Philosophers Problem
-
-
Five philosophers are seated around a circular table. Each philosopher has a plate of spaghetti.
-
When a philosopher is hungry, he tries to get his left and right forks,one at a time.
-
The life of a philosopher consists of alternating periods of eating and thinking.
-
// a nonsolution to the dining philosopher problem #define N 5 void philosopher(int i){ while(true){ think(); take_fork(i); take_fork((i+1)%N); eat(); put_fork(i); put_fork((i+1)%N); } }
-
Suppose that all five philosophers take their left forks simultaneously. None will be able to take their right forks,and there will be a deadlock.
-
All programs continue to run indefinitely but fail to make any progress,is called Starvation
-
// a solution to the dining philosopher problem #define N 5 #define LEFT (i+N-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 1 #define EATING 2 typedef int semaphore; int state[N]; semaphore mutex =1; semaphore s[N]; void philosopher(int i){ while(true){ think(); take_forks(i); eat(); put_forks(i); } } void take_forks(int i){ down(&mutex); state[i]=HUNGRY; test(i); up(&mutex); down(&s[i]); } void put_forks(i){ down(&mutex); state[i]=THINKING; test(LEFT); test(RIGHT); up(&mutex); } void test(i){ if(state[i]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!= EATING){ state[i]=EATING; up(&s[i]); } }