Title: Process, thread and multitasking
1Process, thread and multitasking
2What OS does?
- Process management
- Memory management
- File management
- Input and output control
- Networking
- And more.
3Process management
- What is a process?
- Process state
- Process scheduling
- Process communication
- Process creation and termination
4What is a process
- A process is a program in execution
- It has an environment which has
- Text (code), data, and stack
- It has a state running, waiting, ready, etc
- It may open a number of files, sockets, etc.
- The data it is using may be in registers
- PC register points to the next instruction
5Process in Memory
6Diagram of Process State
7Process Control Block (PCB)
- Information associated with each process
- Process state
- Program counter
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
8Process Control Block (PCB)
9CPU Switch From Process to Process
10How to create processes
- Using system calls
- Fork create an identical twin
- Exec load an executable to overwrite the caller
- All processes form a family tree
- Parent can control children
- How is the first process created??
11Example
If(idfork()) running parent code
else child code
If(idfork()) parent code else
child code
Fork()
If(idfork()) parent code else
running child code
12Process Creation
13Process communication
- Apart from socket, processes can communicate by
files (pipe is a special file) - Parent and child can communicate by
- Wait() and exit()
14Schedulers
- Scheduler (or job scheduler) selects which
processes should be brought into the ready queue. - If there are multiple processes ready, scheduler
decide which process should be executed next and
when to allocates CPU to it.
15Scheduling Criteria
- CPU utilization keep the CPU as busy as
possible - Throughput of processes that complete their
execution per time unit - Turnaround time amount of time to execute a
particular process - Waiting time amount of time a process has been
waiting in the ready queue - Response time amount of time it takes from when
a request was submitted until the first response
is produced, not output (for time-sharing
environment)
turnaround
resp.
arrival
First select
complete
16Optimization Criteria
- Max CPU utilization
- Max throughput
- Min turnaround time
- Min waiting time
- Min response time
17Scheduling methods
- FIFO --- First in first out
- Shortest job first
- Time sharing
- Priority scheduling
- Real-time scheduling
- Earliest deadline first
18First-Come, First-Served (FCFS) Scheduling
- Process Burst Time
- P1 24
- P2 3
- P3 3
- Suppose that the processes arrive in the order
P1 , P2 , P3 The Gantt Chart for the schedule
is - Waiting time for P1 0 P2 24 P3 27
- Average waiting time (0 24 27)/3 17
19FCFS Scheduling (Cont.)
- Suppose that the processes arrive in the order
- P2 , P3 , P1 .
- The Gantt chart for the schedule is
- Waiting time for P1 6 P2 0 P3 3
- Average waiting time (6 0 3)/3 3
- Much better than previous case.
- Convoy effect short process behind long process
20Shortest-Job-First (SJR)Scheduling
- Associate with each process the length of its
next CPU burst. Use these lengths to schedule
the process with the shortest time. - Two schemes
- nonpreemptive once CPU given to the process it
cannot be preempted until completes its CPU
burst. - preemptive if a new process arrives with CPU
burst length less than remaining time of current
executing process, preempt. This scheme is know
as the Shortest-Remaining-Time-First (SRTF). - SJF is optimal gives minimum average waiting
time for a given set of processes.
21Example of Non-Preemptive SJF
- Process Arrival Time Burst Time
- P1 0.0 7
- P2 2.0 4
- P3 4.0 1
- P4 5.0 4
- SJF (non-preemptive)
- Average waiting time (0 6 3 7)/4 4
22Context Switch
- When CPU switches to another process, the system
must save the state of the old process and load
the saved state for the new process. - Context-switch time is overhead the system does
no useful work while switching. - Time dependent on hardware support.
23Process Synchronization
- Processes can communicate with each other by
shared variables - If using shared variables, there must have
synchronization facility -- hard
24Synchronization problem
If two processes (A and B) want to update
count A wants to count may be implemented
in machine language asregister1
counter register1 register1 1counter
register1 B wants to count may be
implemented asregister2 counterregister2
register2 1counter register2
25Synchronization problem (cont)
- Assume counter is initially 5. One interleaving
of statements isA register1 counter
(register1 5)A register1 register1 1
(register1 6)B register2 counter
(register2 5)B register2 register2 1
(register2 4)A counter register1 (counter
6)B counter register2 (counter 4) - The value of count may be either 4 or 6, where
the correct result should be 5.
26Solution
- Disable interrupt
- Using monitor
- Using semaphore
- Wait(sema) --- lock
- Signal(sema) unlock
27Be aware of deadlock
- public static Object cacheLock new Object()
- public static Object tableLock new
Object()  ... Â
- public void oneMethod() Â Â Â Â synchronized
(cacheLock) Â Â Â Â Â Â synchronized (tableLock)
        doSomething()           Â
28Deadlock (cont)
- public void anotherMethod() Â Â Â Â synchronized
(tableLock) Â Â Â Â Â Â synchronized (cacheLock)
        doSomethingElse()            - Â
29Single and Multithreaded Processes
30Benefits
- Responsiveness
- Resource Sharing
- Economy
- Utilization of MP Architectures
31User Threads
- Thread management done by user-level threads
library - Examples
- - POSIX Pthreads
- - Mach C-threads
- - Solaris threads
32Kernel Threads
- Supported by the Kernel
- Examples
- - Windows 95/98/NT/2000
- - Solaris
- - Tru64 UNIX
- - BeOS
- - Linux
33Multithreading Models
- Many-to-One
- One-to-One
- Many-to-Many
34Many-to-One
- Many user-level threads mapped to single kernel
thread. - Used on systems that do not support kernel
threads.
35Many-to-One Model
36One-to-One
- Each user-level thread maps to kernel thread.
- Examples
- - Windows 95/98/NT/2000
- - OS/2
37One-to-one Model
38Many-to-Many Model
- Allows many user level threads to be mapped to
many kernel threads. - Allows the operating system to create a
sufficient number of kernel threads. - Solaris 2
- Windows NT/2000 with the ThreadFiber package
39Many-to-Many Model