+ All Categories
Home > Documents > ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8...

ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8...

Date post: 18-Mar-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
54
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne ©2009 Multithreaded Programming Modified by M.Rebaudengo - 2013
Transcript
Page 1: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009

Multithreaded Programming

Modified by M.Rebaudengo - 2013

Page 2: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.2

Single Thread vs. Multithreads

In traditional operating systems, each process has an address space and a single thread of controlThere are frequently solutions in which it is desirable to have multiple threads of control in the same address space running concurrently, as though they are (almost) separate processes.

Process A

Thread 1

Process B Thread1Thread2Thread3Thread4

Monothreaded process Multithreaded process

Page 3: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Processes and Threads

Process combines two conceptsConcurrency

Each process is a sequential execution stream of instructionsProtection

Each process defines an address spaceAddress space identifies all addresses that can be touched by the program

ThreadsKey idea: separate the concepts of concurrency from protectionA thread is a sequential execution stream of instructionsA process defines the address space that may be shared by multiple threads

Page 4: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.4

Why Threads?

Parallel execution: the same process is executed in parallel on a multicore CPU Shared resources faster communication without serializationeasier to create and destroy than processes (100x)useful if some are I/O-bound overlap computation and I/O

Page 5: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.5

Example of multithreads (I)

Consider how a Web server can improve performance and interactivity by using threads.

When a Web server receives requests for images and pages from many clients, it serves each request (from each client) with a different threadThe process that receives all the requests, creates a new separate thread for each request receivedThis new thread sends the required information to the remote client.While this thread is doing its task, the original thread is free to accept more requestsWeb servers are multiprocessor systems that allow for concurrent completion of several requests, thus improving throughput and response time.

Page 6: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.6

How Can it Help?

Create a number of threads, and for each thread doget network message from clientget URL data from disksend data over network

What did we gain?

Page 7: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.7

Overlapping Requests (Concurrency)

get network message (URL) from clientget URL data from disk

send data over network

get network message (URL) from clientget URL data from disk

send data over network

Request 1Thread 1

Request 2Thread 2

Time

(disk access latency)

(disk access latency)

Total time is less than request 1 + request 2

Page 8: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.8

Example of multithreads (II)

Consider a word processor composed of the following threads:a thread interacts with the usera thread handles reformatting the document in the background and performing spell checking a thread handles the disk backups without interfering with the others in order to automatically save the entire file every few minutes to protect the user against losing the work in the event of program crash.

Page 9: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Threads

A thread lives within a processA process can have several threadsA thread possesses an independent flow of control, and can be scheduled to run separately from other threads, because it maintains its own:

stackregisters (CPU state)

The other resources of the process are shared by all its threads:codememoryopen filesand more...

Threads are lightweightCreating a thread is more efficient than creating a processCommunication between threads is easier than between processesContext switching between threads requires fewer CPU cycles and memory references than switching processesThreads only track a subset of process state (share list of open files, pid, …).

Page 10: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Single and Multithreaded Processes

Page 11: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.11

Performance Benefits

It takes far less time to create a new thread in an existing process than to create a new processIt takes less time to terminate a thread than a processIt takes less time to switch between 2 threads within the same process than to switch between processesThreads between the same process share memory and files: they can communicate with each other without invoking the kernel.

If there is an application (or function) that should be implemented as a set of related units of execution, it is far more efficient to do so as a collection of threads rather than a collection of separate processes.

Page 12: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.12

Thread Model

Items shared by all threads in a process Items private to each thread

Page 13: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.13

Thread Control Block (TCB)

Each thread has:an identifier, a set of registers (including the program counter and stack pointer)a set of attributes (including the state, the stack size, scheduling parameters, etc).

Page 14: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Threads vs. Processes

Threads

A thread has no data segment or heapA thread cannot live on its own, it must live within a processThere can be more than one thread in a process, the first thread calls main and has the process’s stackIf a thread dies, its stack is reclaimedInter-thread communication via memoryEach thread can run on a different physical processorInexpensive creation and context switch

Processes

A process has code/data/heap & other segmentsThere must be at least one thread in a processThreads within a process share code/data/heap, share I/O, but each has its own stack and registersIf a process dies, its resources are reclaimed and all threads dieInter-process communication via OS and data copyingEach process can run on a different physical processorExpensive creation and context switch

Page 15: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Implementing Threads

Processes define an address space; threads share the address space

Process Control Block (PCB) contains process-specific information

Owner, PID, heap pointer, priority, active thread, and pointers to thread information

Thread Control Block (TCB) contains thread-specific information

Stack pointer, PC, thread state (running, …), register values, a pointer to PCB, …

Code

Initialized data

Heap

DLL’s

mapped segments

Process’s address space

Stack – thread1

PCSP

StateRegisters

TCB for Thread1

Stack – thread2

PCSP

StateRegisters

TCB for Thread2

Page 16: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Threads’ Life Cycle

Threads (just like processes) go through a sequence of start, ready, running, waiting, and done states

RunningReady

Waiting

Start Done

Page 17: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.17

Threads vs. ProcessesThreads use and exist within the process resources A thread maintains its own stack and registers, scheduling properties, set of pending and blocked signals.

Secondary Threads vs. Initial ThreadsAn initial thread is created automatically when a process is created.Secondary threads are peers.

Threads vs. Processes

Page 18: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.18

Thread Libraries

Thread libraries provide programmer with APIs (Application Programming Interface) for creating and managing threadsTwo primary ways of implementing

User-Level Threads (ULT): library entirely in user space (invoking a function in the library results in a local function call in user space and not a system call)Kernel-Level Threads (KLT): Kernel-level library supported by the OS (system call).

Page 19: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.19

User-Level Thread

All of the work of thread management is done by the application and the kernel is not aware of the existence of threadsAn application can be programmed to be multithreading by using a threadslibrary, which is a package of routines for thread management Threads library contains code for:

creating and destroyng threadspassing data between threadsscheduling thread executionsaving and restoring thread context.

Page 20: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.20

User LeveL ThreadsImplemented as a thread library, which contains the code for thread creation, termination, scheduling and switching

Kernel sees one process and it is unaware of its thread activity

Page 21: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.21

User-Level Thread

Page 22: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.22

ULT: behavior (I)

By default, an application begins with a single thread and begins running the threadThis application and its thread are allocated to a single process managed by thekernelAt any time that the application is running (i.e., the process is in the Running state),the application may spawn a new thread to run within the same process. Spawning isdone by invoking the spawn utility in the threads library. Control is passed to thatutility by a procedure callThe threads library creates a data structure for the new thread and then passescontrol to one of the threads, within this process, in the Ready state using somescheduling algorithmWhen control is passed to the library, the context of the current thread is saved, andwhen the control is passed from the library to a thread the context of that thread isrestored. The context consists of the contents of user registers, the program counterand the stack pointers.All the previous activities takes place in the user space and within a single process.The kernel is anaware of this activity.

Page 23: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.23

ULT: behavior (II)

The kernel continues to schedule the process as a unit and assigns a single execution state (Running, Blocked, Ready, etc.) to that processWhen a thread does something that may cause it to become blocked locally (e.g., waiting for another thread in its process to complete some work), it calls a procedure in the threads libraryThis procedure checks if the thread must be put into blocked state. If so, it saves its context, calls the thread scheduler to pick another thread to runThe thread scheduler looks in the thread table for a ready thread to run and restores its context

Page 24: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.24

User-level Threads Scheduling

B1, B2, B3,

Page 25: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.25

User-level threads: comments

+ Fast to create and switchprocedures that saves the thread's state and the scheduler are user proceduresno system call is neededno context switch is needed

- When a ULT executes a system call, all the threads within the process are blocked

E.g., read from file can block all threads- User-level scheduler can fight with kernel-level scheduler- A multithread application cannot take advantage of multiprocessing. A kernel assigns one process to only one processor at a time. There are applications that would benefit the ability to execute portions of code simultaneously.

Page 26: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.26

Kernel-Level Threads

The kernel knows the threads and manges themThere is no thread table in each process. Instead, the kernel has a thread table that keeps track of all the threads in the systemWhen a process wants to create a new thread or destroy an existing thread, it makes a kernel call, which then does the creation or destruction by updating the kernel thread tableThe thread table containing TCBs holds the same information as with the ULT, but now kept in the kernel instead of in user space.

Page 27: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.27

Kernel-Level Threads

Page 28: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.28

Kernel Level Threads

Thread management done by the kernel

Page 29: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.29

Kernel-level Threads Scheduling

Page 30: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition

Kernel-level threads+ Kernel-level threads do not block process for a system call

if one thread in a process is blocked by a system call (e.g., for a page fault), the kernel can easily check if the process has one thread ready to run

+ Only one scheduler (and kernel has global view)- Can be difficult to make efficient (create & switch)

Kernel-level threads: comments

Page 31: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.31

Pthreads

May be provided either as user-level or kernel-levelA POSIX standard (IEEE 1003.1c) API to:

create and destroy threadssynchronize threads and lock program resourcesmanage thread scheduling

API specifies behavior of the thread library, implementation is up to the development of the libraryCommon in UNIX operating systems (Solaris, Linux, Mac OS X).

Page 32: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.32

pthread_create()

Synthax:pthread_create(thread,attr,start_routine,arg)

Arguments: thread: a unique identifier for the new thread returned by the subroutine. attr: it specifies a thread attributes object, or NULL for the default values. start_routine: the C routine that the thread will execute once it is created. arg: A single argument that may be passed to start_routine. It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.

Return value:If successfull, it returns 0. Otherwise, it returns a nonzero error code.

Page 33: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.33

#include <stdio.h>

#include <pthread.h>

main() {

pthread_t f2_thread, f1_thread, f3_thread; int i1=1,i2=2;

void *f2(), *f1(),*f3();

pthread_create(&f1_thread,NULL,f1,&i1);

pthread_create(&f2_thread,NULL,f2,&i2);

pthread_create(&f3_thread,NULL,f3,NULL);

}

void *f1(int *i){

}

void *f2(int *i){

}

void *f3() {

}

Page 34: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.34

pthread_exit()

When a thread has finished its work, it can exit by calling the pthread_exit() library procedure

The thread then vanishes and is no longer schedulable and the stack is released.

Page 35: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.35

Example#include <stdio.h>

#include <stdlib.h>

#include <signal.h>

#include <sys/types.h>

#include <unistd.h>

#include <pthread.h> /* POSIX threads */

void *mythread(void *);

int sharedVar ; /* global variable */

int main() {

pthread_t tid;

sharedVar = 1234;

printf("Main: sharedVar= %d\n", sharedVar);

pthread_create(&tid, NULL, mythread, NULL);

sleep(1); /* yield to another thread */

printf("Main: sharedVar= %d\n", sharedVar);

sharedVar = 999;

printf("Main: sharedVar= %d\n", sharedVar);

sleep(1); /* yield to another thread */

printf("Main: sharedVar= %d\n", sharedVar);

printf("DONE\n");

exit(0);

}

void *mythread (void *arg) {

/* display the value of the global variable */ printf("thread %lu: sharedVar is %d \n", pthread_self(), sharedVar);

sharedVar = 111;printf("thread %lu: sharedVar is %d \n", pthread_self(), sharedVar);

sleep(1); /* yield to another thread or main */

printf("thread %lu: sharedVar is %d \n", pthread_self(), sharedVar);

sharedVar = 222;printf("thread %lu: sharedVar is %d.\n", pthread_self(), sharedVar); sleep(1); /* yield to another thread or main */

/* thread exit with an integer */ pthread_exit (NULL);

}

OUTPUT

Main: sharedVar= 1234thread 792: sharedVar is 1234thread 792: sharedVar is 111Main: sharedVar= 111Main: sharedVar= 999thread 792: sharedVar is 999thread 792: sharedVar is 222Main: sharedVar= 222DONE

Page 36: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.36

Example#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#define NUM_THREADS 5

void *PrintHello (void *threadid)

{

long * tid;

tid = (long *) threadid;

printf("Hello World! It's me, thread #%ld!\n", * tid);

pthread_exit(NULL);

}

int main(int argc, char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc;

long t;

for(t=0;t<NUM_THREADS;t++){

printf("In main: creating thread %ld\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);

if (rc){

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

Page 37: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.37

Example#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#define NUM_THREADS 5

void *PrintHello (void *threadid)

{

long * tid;

tid = (long *) threadid;

printf("Hello World! It's me, thread #%ld!\n", * tid);

pthread_exit(NULL);

}

int main(int argc, char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc;

long t;

for(t=0;t<NUM_THREADS;t++){

printf("In main: creating thread %ld\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t);

if (rc){

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

In main: creating thread 0 In main: creating thread 1 Hello World! It's me, thread #0! In main: creating thread 2 Hello World! It's me, thread #1! Hello World! It's me, thread #2! In main: creating thread 3 In main: creating thread 4 Hello World! It's me, thread #3! Hello World! It's me, thread #4!

Page 38: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.38

Multiple arguments via a structure: example#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS 5

char *messages[NUM_THREADS];

struct thread_data{

int thread_id;int sum;char *message;

};

struct thread_data thread_data_array[NUM_THREADS];

Page 39: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.39

void *PrintHello(void *threadarg){

int taskid, sum;char *hello_msg;struct thread_data *my_data;

sleep(1);my_data = (struct thread_data *) threadarg;taskid = my_data->thread_id;sum = my_data->sum;hello_msg = my_data->message;printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);pthread_exit(NULL);

}

Page 40: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.40

int main(int argc, char *argv[]){pthread_t threads[NUM_THREADS];int *taskids[NUM_THREADS];int rc, t, sum =0;

messages[0] = "English: Hello World!";messages[1] = "French: Bonjour, le monde!";messages[2] = "Spanish: Hola al mundo";messages[3] = "German: Guten Tag, Welt!"; messages[4] = "Russian: Zdravstvytye, mir!";

for(t=0;t<NUM_THREADS;t++) {sum = sum + t;thread_data_array[t].thread_id = t;thread_data_array[t].sum = sum;thread_data_array[t].message = messages[t];printf("Creating thread %d\n", t);rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]);if (rc) {

printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}

}pthread_exit(NULL);}

Page 41: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.41

pthread_join()

In some thread systems, one thread can wait for a (specific) thread to exit by calling the pthread_join() procedure

This procedure blocks the calling thread until (a specific) thread has exitedThe thread identifier of the thread to wait for is given as a parameter.

Page 42: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.42

Example

void *howdy(void *vargp);

int main() {pthread_t tid;

pthread_create(&tid, NULL, howdy, NULL);pthread_join(tid, NULL);exit(0);

}

/* thread routine */void *howdy(void *vargp) {printf("Hello, world!\n");

pthread_exit(NULL);}

Page 43: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.43

Execution

main thread

peer thread

pthread_exit()main thread waits for peer thread to terminate

exit()terminates

main thread and any peer threads

call Pthread_create()

call Pthread_join()

Pthread_join() returns

printf()

(peer threadterminates)

Pthread_create() returns

Page 44: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.44

Example#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#define NTHREADS 5

void *myFun (void *x)

{

int tid;

tid = *((int *) x);

printf ("Hi from thread %d!\n", tid);

pthread_exit(NULL);

}

Page 45: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.45

int main(int argc, char *argv[])

{

pthread_t threads[NTHREADS];

int thread_args[NTHREADS];

int rc, i;

/* spawn the threads */

for (i=0; i<NTHREADS; ++i)

{

thread_args[i] = i;

printf("spawning thread %d\n", i);

rc = pthread_create(&threads[i], NULL, myFun, (void *) &thread_args[i]);

}

/* wait for threads to finish */

for (i=0; i<NTHREADS; ++i) {

rc = pthread_join(threads[i], NULL);

}

return 1;

}

Output:

spawning thread 0spawning thread 1Hi from thread 0!spawning thread 2Hi from thread 1!spawning thread 3Hi from thread 2!spawning thread 4Hi from thread 3!Hi from thread 4!

Page 46: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.46

Example

A program implementing the summation function where the summation operation is run as a separate thread.

Page 47: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.47

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

int sum; /* this data is shared by the thread(s) */

void *runner(void *param); /* the thread */int main(int argc, char *argv[]){pthread_t tid; /* the thread identifier */

if (argc != 2) { fprintf(stderr,"usage: a.out <integer value>\n");return -1; }

if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative\n",atoi(argv[1]));return -1;}

/* create the thread */pthread_create(&tid,NULL,runner,argv[1]);

/* now wait for the thread to exit */pthread_join(tid,NULL);

printf("sum = %d\n",sum);}

Page 48: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.48

/*** The thread will begin control in this function*/

void *runner(void *param) {int i, upper = atoi(param);sum = 0;

if (upper > 0) {for (i = 1; i <= upper; i++)sum += i;

}

pthread_exit(0);}

Page 49: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.49

Exercise

Write a counting program, which should have 2 threadsWhile the peer thread loops, incrementing a counter, the main thread peeks at the counter every second and prints its valueAfter 10 iterations, the main thread kills the peer one and computes the average number of counts.

Page 50: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.50

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

#include <errno.h>

char running = 1;

long long counter = 0;

void * process()

{

while (running)

counter++;

printf("Thread: exit\n");

pthread_exit(NULL);

}

Page 51: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.51

int main()

{

int i;

pthread_t thread_Id;

if (pthread_create(&thread_Id, NULL, process, NULL))

{

printf("ERROR in pthread_create()\n");

exit(-1);

}

for (i=0 ; i < 10 ; i++)

{

sleep(1);

printf("counter = %lld\n", counter);

}

running = 0;

pthread_join(thread_Id, NULL);

printf("Average Instructions = %lld \n", counter/10);

return 0;

}

Page 52: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.52

Thread and Processes#include <pthread.h>#include <stdio.h>#include <sys/types.h>

int value = 0; /* this data is shared by the thread(s) */void *runner(void *param); /* the thread */

void *runner(void *param) {

value = 5;

pthread_exit(0);}

Page 53: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.53

int main(int argc, char *argv[]){pid_t pid;pthread_t tid; /* the thread identifier */

pid = fork();if (pid == 0) { /* child process */

pthread_create(&tid, NULL,runner,NULL);/* now wait for the thread to exit */

pthread_join(tid,NULL);printf("CHILD: value = %d\n",value);

}else if (pid > 0) { /* parent process */

wait(NULL);printf("PARENT: value = %d\n",value);

}}

Page 54: ch04 - Multithreaded Programming.ppt - polito.it · 2014-04-10 · Operating System Concepts – 8 th Edition 4.2 Silberschatz, Galvin and Gagne ©2009 Single Thread vs. Multithreads

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition 4.54

Semantics of fork() and exec()

Does fork() duplicate only the calling thread or all threads?some UNIX systems have chosen to have two versions of fork():

one that duplicates all threads (forkall())one that duplicates only the thread invoked by the fork() system call (fork1())

If exec() is called immediately after forking, then duplicating all threads is unnecessary. In this istance, duplicating only the calling thread is appropriate.


Recommended