Process, thread and multitasking - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Process, thread and multitasking

Description:

Process, thread and multitasking. Computer technology. What OS does? Process management ... It has a state running, waiting, ready, etc. It may open a number ... – PowerPoint PPT presentation

Number of Views:1012
Avg rating:3.0/5.0
Slides: 40
Provided by: ITEE
Category:

less

Transcript and Presenter's Notes

Title: Process, thread and multitasking


1
Process, thread and multitasking
  • Computer technology

2
What OS does?
  • Process management
  • Memory management
  • File management
  • Input and output control
  • Networking
  • And more.

3
Process management
  • What is a process?
  • Process state
  • Process scheduling
  • Process communication
  • Process creation and termination

4
What 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

5
Process in Memory
6
Diagram of Process State
7
Process 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

8
Process Control Block (PCB)
9
CPU Switch From Process to Process
10
How 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??

11
Example
If(idfork()) running parent code
else child code

If(idfork()) parent code else
child code
Fork()
If(idfork()) parent code else
running child code
12
Process Creation
13
Process communication
  • Apart from socket, processes can communicate by
    files (pipe is a special file)
  • Parent and child can communicate by
  • Wait() and exit()

14
Schedulers
  • 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.

15
Scheduling 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
16
Optimization Criteria
  • Max CPU utilization
  • Max throughput
  • Min turnaround time
  • Min waiting time
  • Min response time

17
Scheduling methods
  • FIFO --- First in first out
  • Shortest job first
  • Time sharing
  • Priority scheduling
  • Real-time scheduling
  • Earliest deadline first

18
First-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

19
FCFS 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

20
Shortest-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.

21
Example 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

22
Context 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.

23
Process Synchronization
  • Processes can communicate with each other by
    shared variables
  • If using shared variables, there must have
    synchronization facility -- hard

24
Synchronization 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
25
Synchronization 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.

26
Solution
  • Disable interrupt
  • Using monitor
  • Using semaphore
  • Wait(sema) --- lock
  • Signal(sema) unlock

27
Be aware of deadlock
  • public static Object cacheLock new Object()
  • public static Object tableLock new
    Object()  ...  
  • public void oneMethod()     synchronized
    (cacheLock)       synchronized (tableLock)
            doSomething()            

28
Deadlock (cont)
  • public void anotherMethod()     synchronized
    (tableLock)       synchronized (cacheLock)
            doSomethingElse()            
  •  

29
Single and Multithreaded Processes
30
Benefits
  • Responsiveness
  • Resource Sharing
  • Economy
  • Utilization of MP Architectures

31
User Threads
  • Thread management done by user-level threads
    library
  • Examples
  • - POSIX Pthreads
  • - Mach C-threads
  • - Solaris threads

32
Kernel Threads
  • Supported by the Kernel
  • Examples
  • - Windows 95/98/NT/2000
  • - Solaris
  • - Tru64 UNIX
  • - BeOS
  • - Linux

33
Multithreading Models
  • Many-to-One
  • One-to-One
  • Many-to-Many

34
Many-to-One
  • Many user-level threads mapped to single kernel
    thread.
  • Used on systems that do not support kernel
    threads.

35
Many-to-One Model
36
One-to-One
  • Each user-level thread maps to kernel thread.
  • Examples
  • - Windows 95/98/NT/2000
  • - OS/2

37
One-to-one Model
38
Many-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

39
Many-to-Many Model
Write a Comment
User Comments (0)
About PowerShow.com