SlideShare a Scribd company logo
Lecture Plan UNIT-II
Lecture Topic                               Slide
No.                                         No.
1            Process concepts threads       2-11
2            scheduling-criteria alg        12-19
3            scheduling-criteria alg        20-27
4            Algorithms evaluation          28-34
5            Thread scheduling              35-46
6            Case studies UNIX,LINUX        47-49
7            Windows                        50-51
8            REVISION


    Unit-2              OS              1
Process Concept
• An operating system executes a variety of
  programs:
  – Batch system – jobs
  – Time-shared systems – user programs or tasks
• Textbook uses the terms job and process
  almost interchangeably
• Process – a program in execution; process
  execution must progress in sequential fashion
• A process includes:
  – program counter
  – stack
  Unit-2
  – data section OS                   2
Process State

• As a process executes, it changes state
    – new: The process is being created
    – running: Instructions are being executed
    – waiting: The process is waiting for some event
      to occur
    – ready: The process is waiting to be assigned
      to a processor
    – terminated: The process has finished
      execution


Unit-2             OS                       3
Diagram of Process State




Unit-2         OS           4
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

Unit-2         OS                5
Process Control Block (PCB)




Unit-2          OS            6
CPU Switch From Process to Process




Unit-2           OS               7
Process Scheduling Queues
• Job queue – set of all processes in the
  system
• Ready queue – set of all processes
  residing in main memory, ready and waiting
  to execute
• Device queues – set of processes waiting
  for an I/O device
• Processes migrate among the various
  queues
 Unit-2          OS               8
Ready Queue And Various I/O Device Queues




Unit-2            OS                 9
Representation of Process Scheduling




Unit-2         OS                   10
Schedulers
• Long-term scheduler (or job scheduler) –
  selects which processes should be brought
  into the ready queue
• Short-term scheduler (or CPU scheduler) –
  selects which process should be executed
  next and allocates CPU




 Unit-2       OS               11
Producer-Consumer Problem
• Paradigm for cooperating processes,
  producer process produces information that
  is consumed by a consumer process
   – unbounded-buffer places no practical limit on
     the size of the buffer
   – bounded-buffer assumes that there is a fixed
     buffer size




Unit-2           OS                    12
Bounded-Buffer – Shared-Memory Solution
• Shared data
          #define BUFFER_SIZE 10
          typedef struct {
             ...
          } item;

          item buffer[BUFFER_SIZE];
          int in = 0;
          int out = 0;
• Solution is correct, but can only use
  BUFFER_SIZE-1 elements
 Unit-2             OS                13
Bounded-Buffer – Producer
 while (true) {
       /* Produce an item */
           while (((in = (in + 1)
 % BUFFER SIZE count) == out)
         ;   /* do nothing -- no
 free buffers */
        buffer[in] = item;
        in = (in + 1) % BUFFER
 SIZE;
Unit-2        OS          14
Bounded Buffer – Consumer
while (true) {
         while (in == out)
                ; // do nothing
-- nothing to consume

       // remove an item from the
 buffer
       item = buffer[out];
       out = (out + 1) % BUFFER
 SIZE;
Unit-2      OS             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)


 Unit-2          OS                   16
Scheduling Algorithm Optimization
              Criteria
   •     Max CPU utilization
   •     Max throughput
   •     Min turnaround time
   •     Min waiting time
   •     Min response time




Unit-2             OS          17
First-Come, First-Served (FCFS) Scheduling


               ProcessBurst 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:
                      P1              P2         P3


             0                   24        27         30
Unit-2              OS                          18
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
               P2       P3             P1


           0        3        6                   30




  Unit-2                OS                  19
Shortest-Job-First (SJF)
               Scheduling
• Associate with each process the length of
  its next CPU burst. Use these lengths to
  schedule the process with the shortest
  time
• SJF is optimal – gives minimum average
  waiting time for a given set of processes
    – The difficulty is knowing the length of the next
      CPU request

Unit-2            OS                     20
Determining Length of Next CPU Burst
• Can only estimate the length
• Can be done by using the length of previous CPU bursts,
  using exponential averaging




  τ n = 1 = α t n + ( 1 − α )τ n .
           1. t n = actual length of n th CPU burst
           2. τ n +1 = predicted value for the next CPU burst
           3. α , 0 ≤ α ≤ 1
           4. Define :
  Unit-2                  OS                           21
Examples of Exponential Averaging
∀ α =0
    τn+1 = τn
  – Recent history does not count
∀ α =1
  – τn+1 = α tn
  – Only the actual last CPU burst counts
• If we expand the formula, we get:
    τn+1 = α tn+(1 - α)α tn -1 + …
              +(1 - α )j α tn -j + …
              +(1 - α )n +1 τ0


• Since both α and (1 - α) are less than or equal
  Unit-2         OS                   22
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
  (smallest integer ≡ highest priority)
   – Preemptive
   – nonpreemptive
• SJF is a priority scheduling where priority is the predicted next
  CPU burst time
• Problem ≡ Starvation – low priority processes may never
  execute
• Solution ≡ Aging – as time progresses increase the priority of
  the process




Unit-2                OS                           23
Round Robin (RR)
•   Each process gets a small unit of CPU time (time quantum), usually 10-100
    milliseconds. After this time has elapsed, the process is preempted and
    added to the end of the ready queue.
•   If there are n processes in the ready queue and the time quantum is q, then
    each process gets 1/n of the CPU time in chunks of at most q time units at
    once. No process waits more than (n-1)q time units.
•   Performance
      – q large ⇒ FIFO
      – q small ⇒ q must be large with respect to context switch, otherwise
         overhead is too high
    Process         Burst Time
                    P1      24
                    P2       3
                                     P1       P2       P3        P1        P1     P1    P1    P1
                    P3      3
                                 0        4        7        10        14        18 22    26    30
    The Gantt chart is:
    Typically, higher average turnaround than SJF, but better response
    Unit-2                  OS                                                   24
Time Quantum and Context Switch Time




Unit-2           OS                25
Multilevel Queue Scheduling




Multilevel Feedback Queues   NUMA and CPU Scheduling




Unit-2              OS                         26
Algorithm Evaluation
• Deterministic modeling – takes a particular
  predetermined workload and defines the
  performance of each algorithm for that
  workload
• Queueing models
• Implementation




Unit-2         OS                  27
Evaluation of CPU schedulers by
                    Simulation




Unit-2          OS             28
Dispatch Latency




Unit-2     OS           29
Time-Slicing
  Since the JVM Doesn’t Ensure Time-
    Slicing, the yield() Method
  May Be Used:

       while (true) {
          // perform CPU-intensive task
          ...
          Thread.yield();
Unit-2
       }           OS                30
Thread Priorities

  Priority         Comment
Thread.MIN_PRIORITY        Minimum Thread
  Priority
Thread.MAX_PRIORITY           Maximum Thread
  Priority
Thread.NORM_PRIORITY          Default Thread
  Priority

Priorities May Be Set Using setPriority() method:
  setPriority(Thread.NORM_PRIORITY + 2);
  Unit-2            OS                    31
Solaris 2 Scheduling




Unit-2       OS            32
User Threads
• Thread management done by user-level
  threads library

• Three primary thread libraries:
     – POSIX Pthreads
     – Win32 threads
                  Kernel
     – Java threads                  Threads
 •    Supported by the Kernel

 •    Examples
       –   Windows XP/2000
       –   Solaris
       –   Linux
       –   Tru64 UNIX
       –   Mac OS X
     Unit-2                     OS             33
Thread Scheduling
• Distinction between user-level and kernel-level
  threads
• Many-to-one and many-to-many models, thread
  library schedules user-level threads to run on
  LWP
  – Known as process-contention scope (PCS) since
    scheduling competition is within the process
Kernel thread scheduled onto available CPU is
 system-contention scope (SCS) –
 competition among all threads in system
  Unit-2         OS                 34
Pthread Scheduling
• API allows specifying either PCS or
  SCS during thread creation
    – PTHREAD SCOPE PROCESS schedules
      threads using PCS scheduling
    – PTHREAD SCOPE SYSTEM schedules
      threads using SCS scheduling.



Unit-2        OS                 35
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
    int i;
  pthread t tid[NUM THREADS];
  pthread attr t attr;
  /* get the default attributes */
  pthread attr init(&attr);
  /* set the scheduling algorithm to PROCESS or
  SYSTEM */
  pthread attr setscope(&attr, PTHREAD SCOPE
  SYSTEM);
  /* set the scheduling policy - FIFO, RT, or
  OTHER */
  pthread attr setschedpolicy(&attr, SCHED OTHER);
Unit-2 create the threads */
  /*              OS                 36
  for (i = 0; i < NUM THREADS; i++)
Pthread Scheduling API
    /* now join on each thread */
    for (i = 0; i < NUM THREADS; i++)
       pthread join(tid[i], NULL);
}
  /* Each thread will begin control in
   this function */
void *runner(void *param)
{
   printf("I am a threadn");
   pthread exit(0);
}
Unit-2        OS              37
Multiple-Processor Scheduling
• CPU scheduling more complex when multiple CPUs are
  available
• Homogeneous processors within a multiprocessor
• Asymmetric multiprocessing – only one processor accesses
  the system data structures, alleviating the need for data sharing
• Symmetric multiprocessing (SMP) – each processor is self-
  scheduling, all processes in common ready queue, or each has
  its own private queue of ready processes
• Processor affinity – process has affinity for processor on
  which it is currently running
    – soft affinity
    – hard affinity

   Unit-2              OS                        38
Thread Libraries
• Thread library provides programmer with
  API for creating and managing threads
• Two primary ways of implementing
    – Library entirely in user space
    – Kernel-level library supported by the OS




Unit-2            OS                   39
Pthreads
• May be provided either as user-level or
  kernel-level
• A POSIX standard (IEEE 1003.1c) API for
  thread creation and synchronization
• API specifies behavior of the thread library,
  implementation is up to development of the
  library
• Common in UNIX operating systems
  (Solaris, Linux, Mac OS X)

 Unit-2          OS                  40
Threading Issues
• Semantics of fork() and exec() system calls
• Thread cancellation of target thread
     – Asynchronous or deferred
•   Signal handling
•   Thread pools
•   Thread-specific data
•   Scheduler activations


    Unit-2         OS              41
Thread Cancellation
• Terminating a thread before it has finished
• Two general approaches:
   – Asynchronous cancellation terminates the
     target thread immediately
   – Deferred cancellation allows the target thread to
     periodically check if it should be cancelled




 Unit-2           OS                   42
Linux Threads
Linux refers to them as tasks rather than threads

Thread creation is done through clone() system call

clone() allows a child task to share the address space of the parent task (process)




  Unit-2                    OS                                 43
Windows XP Threads




Unit-2      OS           44
Local Procedure Calls in Windows   XP




Unit-2          OS                   45
Windows XP Threads
• Implements the one-to-one mapping, kernel-level
• Each thread contains
   – A thread id
   – Register set
   – Separate user and kernel stacks
   – Private data storage area
• The register set, stacks, and private storage area are
  known as the context of the threads
• The primary data structures of a thread include:
   – ETHREAD (executive thread block)
   – KTHREAD (kernel thread block)
   – TEB (thread environment block)
 Unit-2             OS                       46
Alternating Sequence of CPU And
                I/O Bursts




Unit-2      OS            47
Windows XP Priorities




Unit-2        OS           48
Linux Scheduling
   • Constant order O(1) scheduling time
   • Two priority ranges: time-sharing and
     real-time
   • Real-time range from 0 to 99 and
     nice value from 100 to 140
   • (figure 5.15)




Unit-2         OS                 49
Examples of IPC Systems – Windows XP
• Message-passing centric via local procedure call (LPC) facility
  – Only works between processes on the same system
  – Uses ports (like mailboxes) to establish and maintain
    communication channels
  – Communication works as follows:
     • The client opens a handle to the subsystem’s connection
       port object
     • The client sends a connection request
     • The server creates two private communication ports and
       returns the handle to one of them to the client
     • The client and server use the corresponding port handle
       to send messages or callbacks and to listen for replies
  Unit-2              OS                       50
Priorities and Time-slice length




Unit-2       OS            51

More Related Content

What's hot

Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
Anil Kumar Pugalia
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
Shubhashish Punj
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
Kumar Pritam
 
Lecture 2 process
Lecture 2   processLecture 2   process
Lecture 2 process
Kumbirai Junior Muzavazi
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
Riya Choudhary
 
Process creation and termination In Operating System
Process creation and termination In Operating SystemProcess creation and termination In Operating System
Process creation and termination In Operating System
Farhan Aslam
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
MsAnita2
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
meet darji
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
Deepika Balichwal
 
Os Threads
Os ThreadsOs Threads
Os Threads
Salman Memon
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
Karthick Sekar
 
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)
nikeAthena
 
Process management in os
Process management in osProcess management in os
Process management in os
Miong Lazaro
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
Adeel Rasheed
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
Abhijith Reloaded
 
Concurrency
ConcurrencyConcurrency
Concurrency
rizwanaabassi
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process Scheduling
Damian T. Gordon
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
sangrampatil81
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
Nagarajan
 

What's hot (20)

Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Lecture 2 process
Lecture 2   processLecture 2   process
Lecture 2 process
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
Process creation and termination In Operating System
Process creation and termination In Operating SystemProcess creation and termination In Operating System
Process creation and termination In Operating System
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)
 
Process management in os
Process management in osProcess management in os
Process management in os
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process Scheduling
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 

Similar to OS Process and Thread Concepts

Os2
Os2Os2
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
Kp Sharma
 
Ch6
Ch6Ch6
Ch6
C.U
 
ch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdfch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdf
SonaliAjankar
 
Ch6 cpu scheduling
Ch6   cpu schedulingCh6   cpu scheduling
Ch6 cpu scheduling
Welly Dian Astika
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System Scheduling
Vishnu Prasad
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
tech2click
 
Process Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux KernelProcess Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux Kernel
Haifeng Li
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
Sonali Chauhan
 
Ch5
Ch5Ch5
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
Dr. Mazin Mohamed alkathiri
 
Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithm
Binal Parekh
 
multiprocessor real_ time scheduling.ppt
multiprocessor real_ time scheduling.pptmultiprocessor real_ time scheduling.ppt
multiprocessor real_ time scheduling.ppt
naghamallella
 
OS_Ch6
OS_Ch6OS_Ch6
OSCh6
OSCh6OSCh6
5 Process Scheduling
5 Process Scheduling5 Process Scheduling
5 Process Scheduling
Dr. Loganathan R
 
Ch05
Ch05Ch05
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
gopi7
 
Operating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling ProcessOperating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling Process
Chandrakant Divate
 
Priority Scheduling.pptx
Priority Scheduling.pptxPriority Scheduling.pptx
Priority Scheduling.pptx
SarupyaDatta1
 

Similar to OS Process and Thread Concepts (20)

Os2
Os2Os2
Os2
 
OS Process Chapter 3.pdf
OS Process Chapter 3.pdfOS Process Chapter 3.pdf
OS Process Chapter 3.pdf
 
Ch6
Ch6Ch6
Ch6
 
ch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdfch5_CPU Scheduling_part1.pdf
ch5_CPU Scheduling_part1.pdf
 
Ch6 cpu scheduling
Ch6   cpu schedulingCh6   cpu scheduling
Ch6 cpu scheduling
 
Operating System Scheduling
Operating System SchedulingOperating System Scheduling
Operating System Scheduling
 
Operating System 5
Operating System 5Operating System 5
Operating System 5
 
Process Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux KernelProcess Scheduler and Balancer in Linux Kernel
Process Scheduler and Balancer in Linux Kernel
 
Cpu Scheduling Galvin
Cpu Scheduling GalvinCpu Scheduling Galvin
Cpu Scheduling Galvin
 
Ch5
Ch5Ch5
Ch5
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Window scheduling algorithm
Window scheduling algorithmWindow scheduling algorithm
Window scheduling algorithm
 
multiprocessor real_ time scheduling.ppt
multiprocessor real_ time scheduling.pptmultiprocessor real_ time scheduling.ppt
multiprocessor real_ time scheduling.ppt
 
OS_Ch6
OS_Ch6OS_Ch6
OS_Ch6
 
OSCh6
OSCh6OSCh6
OSCh6
 
5 Process Scheduling
5 Process Scheduling5 Process Scheduling
5 Process Scheduling
 
Ch05
Ch05Ch05
Ch05
 
cpu sechduling
cpu sechduling cpu sechduling
cpu sechduling
 
Operating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling ProcessOperating Systems - CPU Scheduling Process
Operating Systems - CPU Scheduling Process
 
Priority Scheduling.pptx
Priority Scheduling.pptxPriority Scheduling.pptx
Priority Scheduling.pptx
 

Recently uploaded

Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Kalna College
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
RandolphRadicy
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
Celine George
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
Nguyen Thanh Tu Collection
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
Kalna College
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
shreyassri1208
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
OH TEIK BIN
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
Nguyen Thanh Tu Collection
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
 

Recently uploaded (20)

Observational Learning
Observational Learning Observational Learning
Observational Learning
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Contiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptxContiguity Of Various Message Forms - Rupam Chandra.pptx
Contiguity Of Various Message Forms - Rupam Chandra.pptx
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxxSimple-Present-Tense xxxxxxxxxxxxxxxxxxx
Simple-Present-Tense xxxxxxxxxxxxxxxxxxx
 
How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17How to Setup Default Value for a Field in Odoo 17
How to Setup Default Value for a Field in Odoo 17
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN TẬP VÀ PHÁT TRIỂN CÂU HỎI TRONG ĐỀ MINH HỌA THI TỐT NGHIỆP THPT ...
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 
220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology220711130097 Tulip Samanta Concept of Information and Communication Technology
220711130097 Tulip Samanta Concept of Information and Communication Technology
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGHKHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
KHUSWANT SINGH.pptx ALL YOU NEED TO KNOW ABOUT KHUSHWANT SINGH
 
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptxA Free 200-Page eBook ~ Brain and Mind Exercise.pptx
A Free 200-Page eBook ~ Brain and Mind Exercise.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
 

OS Process and Thread Concepts

  • 1. Lecture Plan UNIT-II Lecture Topic Slide No. No. 1 Process concepts threads 2-11 2 scheduling-criteria alg 12-19 3 scheduling-criteria alg 20-27 4 Algorithms evaluation 28-34 5 Thread scheduling 35-46 6 Case studies UNIX,LINUX 47-49 7 Windows 50-51 8 REVISION Unit-2 OS 1
  • 2. Process Concept • An operating system executes a variety of programs: – Batch system – jobs – Time-shared systems – user programs or tasks • Textbook uses the terms job and process almost interchangeably • Process – a program in execution; process execution must progress in sequential fashion • A process includes: – program counter – stack Unit-2 – data section OS 2
  • 3. Process State • As a process executes, it changes state – new: The process is being created – running: Instructions are being executed – waiting: The process is waiting for some event to occur – ready: The process is waiting to be assigned to a processor – terminated: The process has finished execution Unit-2 OS 3
  • 4. Diagram of Process State Unit-2 OS 4
  • 5. 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 Unit-2 OS 5
  • 6. Process Control Block (PCB) Unit-2 OS 6
  • 7. CPU Switch From Process to Process Unit-2 OS 7
  • 8. Process Scheduling Queues • Job queue – set of all processes in the system • Ready queue – set of all processes residing in main memory, ready and waiting to execute • Device queues – set of processes waiting for an I/O device • Processes migrate among the various queues Unit-2 OS 8
  • 9. Ready Queue And Various I/O Device Queues Unit-2 OS 9
  • 10. Representation of Process Scheduling Unit-2 OS 10
  • 11. Schedulers • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU Unit-2 OS 11
  • 12. Producer-Consumer Problem • Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process – unbounded-buffer places no practical limit on the size of the buffer – bounded-buffer assumes that there is a fixed buffer size Unit-2 OS 12
  • 13. Bounded-Buffer – Shared-Memory Solution • Shared data #define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; • Solution is correct, but can only use BUFFER_SIZE-1 elements Unit-2 OS 13
  • 14. Bounded-Buffer – Producer while (true) { /* Produce an item */ while (((in = (in + 1) % BUFFER SIZE count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; Unit-2 OS 14
  • 15. Bounded Buffer – Consumer while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; Unit-2 OS 15
  • 16. 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) Unit-2 OS 16
  • 17. Scheduling Algorithm Optimization Criteria • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time Unit-2 OS 17
  • 18. First-Come, First-Served (FCFS) Scheduling ProcessBurst 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: P1 P2 P3 0 24 27 30 Unit-2 OS 18
  • 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 P2 P3 P1 0 3 6 30 Unit-2 OS 19
  • 20. Shortest-Job-First (SJF) Scheduling • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time • SJF is optimal – gives minimum average waiting time for a given set of processes – The difficulty is knowing the length of the next CPU request Unit-2 OS 20
  • 21. Determining Length of Next CPU Burst • Can only estimate the length • Can be done by using the length of previous CPU bursts, using exponential averaging τ n = 1 = α t n + ( 1 − α )τ n . 1. t n = actual length of n th CPU burst 2. τ n +1 = predicted value for the next CPU burst 3. α , 0 ≤ α ≤ 1 4. Define : Unit-2 OS 21
  • 22. Examples of Exponential Averaging ∀ α =0 τn+1 = τn – Recent history does not count ∀ α =1 – τn+1 = α tn – Only the actual last CPU burst counts • If we expand the formula, we get: τn+1 = α tn+(1 - α)α tn -1 + … +(1 - α )j α tn -j + … +(1 - α )n +1 τ0 • Since both α and (1 - α) are less than or equal Unit-2 OS 22
  • 23. Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority (smallest integer ≡ highest priority) – Preemptive – nonpreemptive • SJF is a priority scheduling where priority is the predicted next CPU burst time • Problem ≡ Starvation – low priority processes may never execute • Solution ≡ Aging – as time progresses increase the priority of the process Unit-2 OS 23
  • 24. Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. • Performance – q large ⇒ FIFO – q small ⇒ q must be large with respect to context switch, otherwise overhead is too high Process Burst Time P1 24 P2 3 P1 P2 P3 P1 P1 P1 P1 P1 P3 3 0 4 7 10 14 18 22 26 30 The Gantt chart is: Typically, higher average turnaround than SJF, but better response Unit-2 OS 24
  • 25. Time Quantum and Context Switch Time Unit-2 OS 25
  • 26. Multilevel Queue Scheduling Multilevel Feedback Queues NUMA and CPU Scheduling Unit-2 OS 26
  • 27. Algorithm Evaluation • Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload • Queueing models • Implementation Unit-2 OS 27
  • 28. Evaluation of CPU schedulers by Simulation Unit-2 OS 28
  • 30. Time-Slicing Since the JVM Doesn’t Ensure Time- Slicing, the yield() Method May Be Used: while (true) { // perform CPU-intensive task ... Thread.yield(); Unit-2 } OS 30
  • 31. Thread Priorities Priority Comment Thread.MIN_PRIORITY Minimum Thread Priority Thread.MAX_PRIORITY Maximum Thread Priority Thread.NORM_PRIORITY Default Thread Priority Priorities May Be Set Using setPriority() method: setPriority(Thread.NORM_PRIORITY + 2); Unit-2 OS 31
  • 33. User Threads • Thread management done by user-level threads library • Three primary thread libraries: – POSIX Pthreads – Win32 threads Kernel – Java threads Threads • Supported by the Kernel • Examples – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X Unit-2 OS 33
  • 34. Thread Scheduling • Distinction between user-level and kernel-level threads • Many-to-one and many-to-many models, thread library schedules user-level threads to run on LWP – Known as process-contention scope (PCS) since scheduling competition is within the process Kernel thread scheduled onto available CPU is system-contention scope (SCS) – competition among all threads in system Unit-2 OS 34
  • 35. Pthread Scheduling • API allows specifying either PCS or SCS during thread creation – PTHREAD SCOPE PROCESS schedules threads using PCS scheduling – PTHREAD SCOPE SYSTEM schedules threads using SCS scheduling. Unit-2 OS 35
  • 36. Pthread Scheduling API #include <pthread.h> #include <stdio.h> #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[NUM THREADS]; pthread attr t attr; /* get the default attributes */ pthread attr init(&attr); /* set the scheduling algorithm to PROCESS or SYSTEM */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread attr setschedpolicy(&attr, SCHED OTHER); Unit-2 create the threads */ /* OS 36 for (i = 0; i < NUM THREADS; i++)
  • 37. Pthread Scheduling API /* now join on each thread */ for (i = 0; i < NUM THREADS; i++) pthread join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { printf("I am a threadn"); pthread exit(0); } Unit-2 OS 37
  • 38. Multiple-Processor Scheduling • CPU scheduling more complex when multiple CPUs are available • Homogeneous processors within a multiprocessor • Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing • Symmetric multiprocessing (SMP) – each processor is self- scheduling, all processes in common ready queue, or each has its own private queue of ready processes • Processor affinity – process has affinity for processor on which it is currently running – soft affinity – hard affinity Unit-2 OS 38
  • 39. Thread Libraries • Thread library provides programmer with API for creating and managing threads • Two primary ways of implementing – Library entirely in user space – Kernel-level library supported by the OS Unit-2 OS 39
  • 40. Pthreads • May be provided either as user-level or kernel-level • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • API specifies behavior of the thread library, implementation is up to development of the library • Common in UNIX operating systems (Solaris, Linux, Mac OS X) Unit-2 OS 40
  • 41. Threading Issues • Semantics of fork() and exec() system calls • Thread cancellation of target thread – Asynchronous or deferred • Signal handling • Thread pools • Thread-specific data • Scheduler activations Unit-2 OS 41
  • 42. Thread Cancellation • Terminating a thread before it has finished • Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled Unit-2 OS 42
  • 43. Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) Unit-2 OS 43
  • 45. Local Procedure Calls in Windows XP Unit-2 OS 45
  • 46. Windows XP Threads • Implements the one-to-one mapping, kernel-level • Each thread contains – A thread id – Register set – Separate user and kernel stacks – Private data storage area • The register set, stacks, and private storage area are known as the context of the threads • The primary data structures of a thread include: – ETHREAD (executive thread block) – KTHREAD (kernel thread block) – TEB (thread environment block) Unit-2 OS 46
  • 47. Alternating Sequence of CPU And I/O Bursts Unit-2 OS 47
  • 49. Linux Scheduling • Constant order O(1) scheduling time • Two priority ranges: time-sharing and real-time • Real-time range from 0 to 99 and nice value from 100 to 140 • (figure 5.15) Unit-2 OS 49
  • 50. Examples of IPC Systems – Windows XP • Message-passing centric via local procedure call (LPC) facility – Only works between processes on the same system – Uses ports (like mailboxes) to establish and maintain communication channels – Communication works as follows: • The client opens a handle to the subsystem’s connection port object • The client sends a connection request • The server creates two private communication ports and returns the handle to one of them to the client • The client and server use the corresponding port handle to send messages or callbacks and to listen for replies Unit-2 OS 50
  • 51. Priorities and Time-slice length Unit-2 OS 51