Multithreading Indranil Sen Gupta (odd section) and Mainack Mondal - - PowerPoint PPT Presentation

multithreading
SMART_READER_LITE
LIVE PREVIEW

Multithreading Indranil Sen Gupta (odd section) and Mainack Mondal - - PowerPoint PPT Presentation

Multithreading Indranil Sen Gupta (odd section) and Mainack Mondal (even section) CS39002 Spring 2019-20 Bu But f first, w , we w will d do s o som ome p prob oblem so solving ng an and recap ecap Scheduling criteria CPU


slide-1
SLIDE 1

Multithreading

Indranil Sen Gupta (odd section) and Mainack Mondal (even section)

CS39002 Spring 2019-20

slide-2
SLIDE 2

Bu But f first, w , we w will d do s

  • som
  • me p

prob

  • blem

so solving ng an and recap ecap

slide-3
SLIDE 3

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)

  • Burst time – amount of time a process is executed
slide-4
SLIDE 4

Recap: Multi level feedback queue

  • Three queues:
  • Q0 – RR with time quantum (!) 8

ms

  • Q1 – RR with ! = 16ms
  • Q2 – FCFS
  • A process in Q1 can execute only when Q0 is empty
  • A process in Q0 can pre-empt a process in Q1 or Q2
  • If the CPU burst of a process exceeds ! its moved to lower priority queue
slide-5
SLIDE 5

Issue with Multi level feedback queue scheduling

  • Long running processes may starve
  • Permanent demotion of priority hurts processes that change

their behavior (e.g., lots of computation only at beginning)

  • Eventually all long-running processes move to FCFS
slide-6
SLIDE 6

Issue with Multi level feedback queue scheduling

  • Long running processes may starve
  • Permanent demotion of priority hurts processes that change

their behavior (e.g., lots of computation only at beginning)

  • Eventually all long-running processes move to FCFS
  • Solution
  • periodic priority boost: all processes moved to high priority

queue

  • Priority boost with aging: recompute priority based on

scheduling history of a process

slide-7
SLIDE 7

Ex 1: First Come First Serve scheduling (FCFS)

Example 1 Draw Gantt chart and calculate average waiting time for two schedules: P1, P2, P3 and P2, P3, P1 (Ans: 17 ms and 3 ms)

Process P1 P2 P3 Arrival time CPU burst 24ms 3ms 3ms

slide-8
SLIDE 8

Ex 2: Another FCFS

Example 2 Draw Gantt chart and calculate average waiting time

(Ans: 11/5 ms)

Process P1 P2 P3 P4 P5 Arrival time 2ms 3ms 5ms 9ms CPU burst 3ms 3ms 2ms 5ms 3ms

slide-9
SLIDE 9

Ex 3: SJF

What is the SJF schedule and corresponding wait time? Compare with the following FCFS schedule: P1, P2, P3, P4

(Ans: SJF – 7 ms and FCFS – 10.25 ms)

Process P1 P2 P3 P4 Arrival time CPU burst 6ms 8ms 7ms 3ms

slide-10
SLIDE 10

Ex 4: Shortest remaining time first

  • Pre-emptive version of SJF
  • A smaller CPU burst time process can evict a running

process

  • Draw preemptive gantt chart and computing waiting time.

(Ans: 6.5 ms)

Process P1 P2 P3 P4 Arrival time 1ms 2ms 3ms CPU burst 8ms 4ms 9ms 5ms

slide-11
SLIDE 11

Ex 5: Priority scheduling

  • A priority is assigned to each process
  • CPU is allotted to the process with highest priority
  • SJF is a type of priority scheduling

What is the average waiting time?

(Ans: 8.2 ms)

Process P1 P2 P3 P4 P5 Arrival time CPU burst 10ms 1ms 2ms 1ms 5ms Priority 3 1 4 5 2

slide-12
SLIDE 12

Ex 6: RR scheduling

Example: If time quantum ! = 4 ms, then what is the avg. wait time? (schedule P1, P2, P3,…)

(Ans: 5.66ms)

Process P1 P2 P3 Arrival time CPU burst 24ms 3ms 3ms

slide-13
SLIDE 13

Try this Exercise

Compute average turnaround time for ! = 1,2,3,4,5,6,7ms Compute average wait time for ! = 1,2,3,4,5,6,7ms Assume the schedule is P1, P2, P3, P4

Process P1 P2 P3 P4 Arrival time CPU burst 6ms 3ms 1ms 7ms

slide-14
SLIDE 14

No Now let’s go int nto mul ultithr hreading ng

slide-15
SLIDE 15

Rest of today’s class

  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-16
SLIDE 16

Rest of today’s class

  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-17
SLIDE 17

What is a thread?

  • Process is a program in execution with single thread of control
slide-18
SLIDE 18

What is a thread?

  • Process is a program in execution with single thread of control
  • All modern OS allows process to have multiple threads of

control

slide-19
SLIDE 19

What is a thread?

  • Process is a program in execution with single thread of control
  • All modern OS allows process to have multiple threads of

control

  • Multiple tasks within an application can be implemented by

separate threads

  • Update display
  • Fetch data
  • Spell checking
  • Answer a network request
slide-20
SLIDE 20

How is a thread created?

  • Can be considered a basic unit of CPU utilization
  • Unique thread ID, Program counter (PC), register set &

stack

slide-21
SLIDE 21

How is a thread created?

  • Can be considered a basic unit of CPU utilization
  • Unique thread ID, Program counter (PC), register set &

stack

  • Shares with other threads from same process the code

section, data section and other OS resources like open files

slide-22
SLIDE 22

How is a thread created?

  • Can be considered a basic unit of CPU utilization
  • Unique thread ID, Program counter (PC), register set &

stack

  • Shares with other threads from same process the code

section, data section and other OS resources like open files

  • Essentially same virtual memory address space
  • Process creation is he

heavy vy-wei weight while thread creation is lig light ht- wei weight

slide-23
SLIDE 23

Comparison: single and multi threaded processes

registers code data files stack thread single-threaded process

slide-24
SLIDE 24

Comparison: single and multi threaded processes

registers code data files stack registers registers registers code data files stack stack stack thread thread single-threaded process multithreaded process

slide-25
SLIDE 25
  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-26
SLIDE 26

Thread: The benefits

  • Context switching among threads of same process is faster
  • OS needs to reset/store less memory locations/registers
slide-27
SLIDE 27

Thread: The benefits

  • Context switching among threads of same process is faster
  • OS needs to reset/store less memory locations/registers
  • Responsiveness is better (important for interactive applications)
  • E.g., even if part of process is busy the interface still works
slide-28
SLIDE 28

Thread: The benefits

  • Context switching among threads of same process is faster
  • OS needs to reset/store less memory locations/registers
  • Responsiveness is better (important for interactive applications)
  • E.g., even if part of process is busy the interface still works
  • Resource sharing is better for peer threads
  • Many possible threads of activity in same address space
  • Sharing variable is more efficient than pipe, shared memory
slide-29
SLIDE 29

Thread: The benefits

  • Context switching among threads of same process is faster
  • OS needs to reset/store less memory locations/registers
  • Responsiveness is better (important for interactive applications)
  • E.g., even if part of process is busy the interface still works
  • Resource sharing is better for peer threads
  • Many possible threads of activity in same address space
  • Sharing variable is more efficient than pipe, shared memory
  • Thread creation:10-30 times faster than process creation
slide-30
SLIDE 30

Thread: The benefits

  • Context switching among threads of same process is faster
  • OS needs to reset/store less memory locations/registers
  • Responsiveness is better (important for interactive applications)
  • E.g., even if part of process is busy the interface still works
  • Resource sharing is better for peer threads
  • Many possible threads of activity in same address space
  • Sharing variable is more efficient than pipe, shared memory
  • Thread creation:10-30 times faster than process creation
  • Better scalability for multiprocessor architecture
slide-31
SLIDE 31
  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-32
SLIDE 32

Thread: The applications

  • A typical application is implemented as a separate process

with multiple threads of control

  • Ex 1: A web browser
  • Ex 2: A web server
  • Ex 3: An OS
slide-33
SLIDE 33

Thread example: Web browser

  • Think of a web browser (e.g., chrome)
  • Thread 1: retrieve data
  • Thread 2: display image or text (render)
  • Thread 3: waiting for user input (your password)
slide-34
SLIDE 34

Thread example: Web server

  • A single instance of web server (apache tomcat, nginx) may

be required to perform several similar tasks

  • One thread accepts request over network
  • New threads service each request: one thread per request
  • The main process create these threads
slide-35
SLIDE 35

Thread example: OS

  • Most OS kernels are multithreaded
  • Several threads operate in kernel
  • Each thread performing a specific task
  • E.g., managing memory, managing devices, handling

interrupts etc.

slide-36
SLIDE 36
  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-37
SLIDE 37

User threads and kernel threads

  • User threads: management done by user-level threads

library

  • A few well extablished primary thread libraries
  • POSIX Pthreads, Windows threads, Java threads
slide-38
SLIDE 38

User threads and kernel threads

  • User threads: management done by user-level threads

library

  • A few well extablished primary thread libraries
  • POSIX Pthreads, Windows threads, Java threads
  • Kernel threads - Supported by the Kernel
  • Exists virtually in all general purpose OS
  • Windows, Linux, Mac OS X
slide-39
SLIDE 39

User threads and kernel threads

  • User threads: management done by user-level threads

library

  • A few well extablished primary thread libraries
  • POSIX Pthreads, Windows threads, Java threads
  • Kernel threads - Supported by the Kernel
  • Exists virtually in all general purpose OS
  • Windows, Linux, Mac OS X

As you might have guessed: Even user threads will ultimately need kernel thread support

slide-40
SLIDE 40

Multithreading Models

  • There are multiple models to map users to kernel threads
  • Many-to-One
  • One-to-One
  • Many-to-Many
slide-41
SLIDE 41

Many-to-One

  • Many user-level threads mapped

to single kernel thread

  • Blocking one thread causes all to

block

  • Multiple threads may not run in

parallel on multicore system because only one may be in kernel at a time

  • Old model: Only few systems

currently use this model

user thread kernel thread k

slide-42
SLIDE 42

One-to-One

  • Each user-level thread maps to one kernel thread
  • A user-level thread creation -> a kernel thread creation
  • More concurrency than many-to-one
  • #threads per process sometimes restricted due to
  • verhead on kernel
  • Windows. Linux

user thread kernel thread k k k k

slide-43
SLIDE 43

Many-to-Many Model

  • Allows many user level threads to be mapped to many

kernel threads

  • Allows the operating system to create sufficient number of

kernel threads

  • Windows with the ThreadFiber package

user thread kernel thread k k k

slide-44
SLIDE 44
  • What is a thread?
  • Why do you need threads?
  • How are threads used in real-world?
  • Multithreading models
  • POSIX Pthread library
slide-45
SLIDE 45

POSIX Pthread: basics

  • May be provided either as user level or kernel level library
  • Global data: Any variable/data declared globally are shared

among all threads of the same process

  • Local data: Data local to a function (running in a thread)

are stored in thread stack

  • Example:
  • A separate thread in created that calculates the sum of N

natural numbers (N is an input)

  • The parent thread waits for the child thread to end
slide-46
SLIDE 46

Now the code

#include<stdio.h> #include<pthread.h>

slide-47
SLIDE 47

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this

slide-48
SLIDE 48

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ } void *runner (void *param){ }

slide-49
SLIDE 49

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes } void *runner (void *param){ }

slide-50
SLIDE 50

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread } void *runner (void *param){ }

slide-51
SLIDE 51

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread pthread_join(tid, NULL); //wait for the thread to exit } void *runner (void *param){ }

slide-52
SLIDE 52

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread pthread_join(tid, NULL); //wait for the thread to exit printf(“\n sum = %d”, sum); // print accumulated sum } void *runner (void *param){ }

slide-53
SLIDE 53

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread pthread_join(tid, NULL); //wait for the thread to exit printf(“\n sum = %d”, sum); // print accumulated sum } void *runner (void *param){ int I , N = atoi(param); // get input value sum = 0; }

slide-54
SLIDE 54

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread pthread_join(tid, NULL); //wait for the thread to exit printf(“\n sum = %d”, sum); // print accumulated sum } void *runner (void *param){ int I , N = atoi(param); // get input value sum = 0; for(i = 1; i<=N;i++){sum = sum+i;} }

slide-55
SLIDE 55

Now the code

#include<stdio.h> #include<pthread.h> int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init (&attr); // get default attributes pthread_create(&tid, &attr, runner, argv[1]); // create the thread pthread_join(tid, NULL); //wait for the thread to exit printf(“\n sum = %d”, sum); // print accumulated sum } void *runner (void *param){ int i , N = atoi(param); // get input value sum = 0; for(i = 1; i<=N;i++){sum = sum+i;} pthread_exit(0); // terminate the thread }

slide-56
SLIDE 56

You can also create many threads

#include<stdio.h> #include<pthread.h> #define N_THR 10 Int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t mythreads[N_THR]; … … for (int i=0; i< N_THR; i++) pthread_create(&mythreads[i], &attr, runner, argv[1]); // create the threads } void *runner (void *param){ … }

slide-57
SLIDE 57

You can also create many threads

#include<stdio.h> #include<pthread.h> #define N_THR 10 Int sum; // data shared over threads void *runner (void *param); // child process calls this int main(int argc, char *argv[]){ pthread_t mythreads[N_THR]; … … for (int i=0; i< N_THR; i++) pthread_create(&mythreads[i], &attr, runner, argv[1]); // create the threads for (int i=0; i< N_THR; i++) pthread_join(mythreads[i], NULL); //wait for the threads to exit printf(“\n sum = %d”, sum); // print accumulated sum } void *runner (void *param){ … }

slide-58
SLIDE 58

Next class

  • More on POSIX Pthread library
  • Thread scheduling
  • Thread cancellation
  • Signal handling