+ All Categories
Home > Documents > Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Date post: 16-Dec-2015
Category:
Upload: ray-good
View: 223 times
Download: 0 times
Share this document with a friend
80
Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III
Transcript
Page 1: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Multithread API’s

Adam PiotrowskiGrzegorz Jabłoński

Lecture III

Page 2: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Agenda• Signals – software asynchronous

interrupts

• Processes

• Threads

2

Page 3: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Signals

• Signals are software interrupts that provide a mechanism for handling asynchronous events.

• The kernel can perform one of three actions:– ignore – catch and handle– perform default action

3

Page 4: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Type of signals

Signal Name Description

SIGCHLD Child has terminated Ignored

SIGINT User generated the interrupt character (Ctrl-C)

Terminate

SIGALRM Sent by alarm() Terminate

SIGUSR1 Process-defined signal Terminate

SIGKILL Uncatchable process termination

Terminate

SIGPIPE Process wrote to a pipe but there are no readers

Terminate

SIGABRT Sent by abort() Terminate with core dump

SIGSEGV Memory access violation Terminate with core dump 4

Page 5: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Signal handling

NAMEsignal - simplified software signal facilities  

SYNOPSIS#include <signal.h> void (* signal(int sig, void *func)(int)))(int);

typedef void (*sig_t) (int); sig_t signal(int sig, sig_t func);

5

Page 6: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Signals - Examplestatic void sigint_handler (int signo){

printf ("Caught SIGINT!\n");exit (EXIT_SUCCESS);

}

int main (void){

if (signal (SIGINT, sigint_handler) == SIG_ERR) {fprintf (stderr, "Cannot handle SIGINT!\n");exit (EXIT_FAILURE);

}for (;;)

pause ( );return 0;

}

6

Page 7: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Processes

• Processes are object code in execution: active, alive, running programs.

• Processes consist of data, resources, state, and a virtualized computer.

7

Page 8: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Multi processes application

8

parent process

child process

child process child process

child process

forkfork

fork fork

Page 9: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

fork NAME

fork -- create a new process

SYNOPSIS

#include <sys/types.h>#include <unistd.h>

pid_t fork(void);

9

Page 10: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

fork example

pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

printf(” Parent process”);}else if (pid == 0){

printf(”Child process”);}

• variables are duplicated

• descriptors are duplicated, too

10

Page 11: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

fork example

pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

printf(” Parent process”);}else if (pid == 0){

printf(”Child process”);}

11

pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

printf(” Parent process”);}else if (pid == 0){

printf(”Child process”);}

Page 12: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

fork exampleint sock;while (1){

/*wait for connection from clientestabilish communication with clientsock = …

*/pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

close(sock);}else if (pid == 0){

/*perform data exchange based on sock descriptor*/ close(sock); return 0;}

}12

Page 13: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Copy-on-Write

13

Page 14: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

processes & SIGCHLD signal

14

Page 15: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

processes & SIGCHLD signalint sock;

while (1){ /*

wait for connection from clientestabilish communication with clientsock = …

*/pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

close(sock);}else if (pid == 0){

/*perform data exchange based on sock descriptor*/ close(sock); return 0;}

}15

Page 16: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

processes & SIGCHLD signalint sock;

if (signal (SIGCHLD, sigint_handler) == SIG_ERR) ASSERT(false);

while (1){ /*

wait for connection from clientestabilish communication with clientsock = …

*/pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

close(sock);}else if (pid == 0){

/*perform data exchange based on sock descriptor*/ close(sock); return 0;}

}16

Page 17: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

processes & SIGCHLD signalint sock;

if (signal (SIGCHLD, sigint_handler) == SIG_ERR) ASSERT(false);

while (1){ /*

wait for connection from clientestabilish communication with clientsock = …

*/pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

close(sock);}else if (pid == 0){

/*perform data exchange based on sock descriptor*/ close(sock); return 0;}

}17

static void sigint_handler (int signo){

int status;wait(&status);

}

Page 18: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

processes & SIGCHLD signalint sock;

if (signal (SIGCHLD, sigint_handler) == SIG_ERR) ASSERT(false);

while (1){ /*

wait for connection from clientestabilish communication with clientsock = …

*/pid_t pid;pid = fork();if (pid < 0)

ASSERT(false)else if (pid > 0) {

close(sock);}else if (pid == 0){

/*perform data exchange based on sock descriptor*/ close(sock); return 0;}

}18

static void sigint_handler (int signo){ int status; while (waitpid(-1, &status, WNOHANG) > 0);}

Page 19: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

IPC• shared memory• signals• pipes• named pipes• message queues• sockets• semaphores• files

19

Page 20: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thread vs Processes

process• complex interprocesses

communications (latency, security problem)

• simple to create and manage

• more expensive (clear cache)

• need more memory• need more time to

switch contextArt of Multiprocessor

Programming20

Page 21: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thread vs Processes

thread• simple interthread

communication• require

synchronisation• less expensive • faster to create

process• complex interprocesses

communications (latency, security problem)

• simple to create and manage

• more expensive (clear cache)

• need more memory• need more time to

switch contextArt of Multiprocessor

Programming21

Page 22: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Pthread API

• thread management• thread atributess• synchronisation

22

Page 23: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Creating threadNAME

pthread_create - creates a new thread, with attributes specified by attr 

SYNOPSIS#include <pthread.h>

int pthread_create(

pthread_t *thread,

const pthread_attr_t *attr,

void* (*start_routine)(void*),

void *arg); 23

Page 24: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

pthread_create example

void *thread_fun(void *arg) {

if ((int)arg == 7)//do something

}

pthread_t tid;pthread_attr_t attr;

//attr initialisation...

if (pthread_create(&tid, &attr, thread_fun, (void*)7)ASSERT(false);

24

Page 25: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Exiting thread

NAMEpthread_exit - terminates the calling thread, returning status 

SYNOPSIS#include <pthread.h> void pthread_exit(void *status);

25

Page 26: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Additional functions

26

NAMEthread_self - returns the thread ID of the calling

thread.

SYNOPSIS#include <pthread.h>

pthread_t pthread_self(void);

NAME pthread_equal - compares the thread IDs t1 and t2

and returns a non-zero value if they are equal; otherwise, it returns zero.  

SYNOPSIS#include <pthread.h>

int pthread_equal(pthread_t t1, pthread_t t2);

Page 27: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Joining threadsvoid *thread_fun(void *arg) {

//do something pthread_exit(NULL);

}

int main(){

pthread_t tid;void* status;

if (pthread_create(&tid, NULL, thread_fun, NULL)ASSERT(false);

//do something

return 0;}

27

Page 28: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Joining threadsvoid *thread_fun(void *arg) {

//do something pthread_exit(NULL);

}

int main(){

pthread_t tid;void* status;

if (pthread_create(&tid, NULL, thread_fun, NULL)ASSERT(false);

//do something

if (pthread_join(main_thr, &status))ASSERT(false);

return 0;}

28

Page 29: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Joining threads

NAMEpthread_join - blocks the calling thread until thread terminates. If status is not null, the status from pthread_exit() will be placed there.

SYNOPSIS#include <pthread.h> int pthread_join(pthread_t thread, void **status);

29

Page 30: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Joining threads

30

int main(){

pthread_t tid;void* status;

if (pthread_create(&tid, NULL, thread_fun, NULL)ASSERT(false);

//do something

if (pthread_join(main_thr, &status))ASSERT(false);

return 0;}

void *thread_fun(void *arg) {

//do something pthread_exit(NULL);

}

Page 31: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

What, if we don’t want to wait for thread

pthread_attr_t attr;

if (pthread_attr_init(&attr)) ASSERT(false);

if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED))ASSERT(false);

/*create new thread*/

if (pthread_attr_destroy(&attr)) ASSERT(false);

31

Page 32: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

What, if we don’t want to wait for thread

32

NAMEpthread_detach- turns the current thread into a detached thread.

SYNOPSIS#include <pthread.h> int pthread_detach(pthread_t thread);

Page 33: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributes

All threads attributes are set in a thread attributes object by a function of the form:

int pthread_attr_setname( pthread_attr_t *attr, Type t);

All threads attributes are retrieved from a threads attributes object by a function of the form:

int pthread_attr_getname( pthread_attr_t *attr, Type t);

33

Page 34: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributes

All threads attributes are set in a thread attributes object by a function of the form:

int pthread_attr_setname( pthread_attr_t *attr, Type t);

All threads attributes are retrieved from a threads attributes object by a function of the form:

int pthread_attr_getname( pthread_attr_t *attr, Type t);

34

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);

Page 35: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 35

Page 36: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 36

The stacksize attribute shall define the minimum stack size (in bytes) allocated for the created threads stack.

Page 37: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 37

The stackaddr attribute specifies the location of storage to be used for the created thread's stack.

Page 38: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 38

The guardsize attribute controls the size of the guard area for the created thread's stack. The guardsize attribute provides protection against overflow of the stack pointer. If a thread's stack is created with guard protection, the implementation allocates extra memory at the overflow end of the stack as a buffer against stack overflow of the stack pointer.

Page 39: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 39

Control whether the thread is created in the joinable state (PTHREAD_CREATE_JOINABLE) or in the detached state ( PTHREAD_CREATE_DETACHED).

Page 40: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 40

The contentionscope attribute may have the values PTHREAD_SCOPE_SYSTEM, signifying system scheduling contention scope, or PTHREAD_SCOPE_PROCESS, signifying process scheduling contention scope.

Page 41: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 41

When the attributes objects are used by pthread_create(), the inheritsched attribute determines how the other scheduling attributes of the created thread shall be set. •PTHREAD_INHERIT_SCHED Specifies that the thread scheduling attributes shall be inherited from the creating thread, and the scheduling attributes in this attr argument shall be ignored. •PTHREAD_EXPLICIT_SCHED Specifies that the thread scheduling attributes shall be set to the corresponding values from this attributes object.

Page 42: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 42

The supported values of policy shall include SCHED_FIFO, SCHED_RR, and SCHED_OTHER,

Page 43: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 43

For the SCHED_FIFO and SCHED_RR policies, the only required member of param is sched_priority. For the SCHED_SPORADIC policy, the required members of the param structure are sched_priority, sched_ss_low_priority, sched_ss_repl_period, sched_ss_init_budget, and sched_ss_max_repl. The specified sched_ss_repl_period must be greater than or equal to the specified sched_ss_init_budget for the function to succeed; if it is not, then the function shall fail. The value of sched_ss_max_repl shall be within the inclusive range [1, {SS_REPL_MAX}] for the function to succeed; if not, the function shall fail.

Page 44: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME TYPE DESCRIPTION

stacksize size_t The thread's stack size.

stackaddr void* The thread's stack address.

guardsize size_t The thread's stack guard size.

detachstate int The thread's detach state.

contentionscope int The thread’s scope

inheritsched int The thread’s scheduling inheritence

schedpolicy int The thread’s scheduling polices

schedparam struct sched_param The thread’s scheduling parameters 44

int pthread_setschedparam(pthread_t, int , const struct sched_param *);

int pthread_detach(pthread_t);

Page 45: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributesNAME

pthread_attr_destroy, pthread_attr_init - destroy and initialize the thread attributes object  

SYNOPSIS#include <pthread.h>

int pthread_attr_destroy(pthread_attr_t *attr);int pthread_attr_init(pthread_attr_t *attr);

45

Page 46: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Threads attributes - example

pthread_attr_t attr;size_t size = 1024;

if (pthread_attr_init(&attr)) ASSERT(false);

if (pthread_attr_getstacksize(&attr, &size))ASSERT(false);

/*create new thread*/

if (pthread_attr_destroy(&attr)) ASSERT(false);

46

Page 47: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threadsNAME

pthread_cancel - function requests that thread be cancelled.

SYNOPSIS#include <pthread.h>

int pthread_cancel(pthread_t thread);

47

Page 48: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threadsNAME

pthread_cleanup_push, pthread_cleanup_pop - pushes the handler routine and argument onto the calling thread’s cancellation cleanup stack or removes the routine at the top of the calling thread’s cancellation cleanup stack and invokes it if execute is non-zero.

SYNOPSIS#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void *), void *arg); void pthread_cleanup_pop(int execute);

48

Page 49: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threadsNAME

pthread_cleanup_push, pthread_cleanup_pop - pushes the handler routine and argument onto the calling thread’s cancellation cleanup stack or removes the routine at the top of the calling thread’s cancellation cleanup stack and invokes it if execute is non-zero.

SYNOPSIS#include <pthread.h>

void pthread_cleanup_push(void (*routine)(void *), void *arg); void pthread_cleanup_pop(int execute);

49

Page 50: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threadsNAME

pthread_setcanceltype, pthread_setcancelstate - This function both sets the calling thread’s cancellability type/state to type/state and returns the previous value in oldtype/oldstate. Legal values for type are PTHREAD_CANCEL_DEFERRED and

PTHREAD_CANCEL_ASYNCHRONOUS. Legal values for state are PTHREAD_CANCEL_ENABLED and PTHREAD_CANCEL_DISABLED.

SYNOPSIS#include <pthread.h>

int pthread_setcanceltype(int type, int *oldtype);

int pthread_setcancelstate(int state, int *oldstate);

50

Page 51: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threadsNAME

pthread_testcancel - cancellation point. If the cancel state is disabled, it just returns. If there are no outstanding cancellation requests, then it will also return. Otherwise it will not return and the thread will be cancelled.

SYNOPSIS#include <pthread.h>

void pthread_testcancel(void);

51

Page 52: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threads - Examplevoid *thread_fun(void *arg)

{int *p;

pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);

pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);while (1){

p = malloc(10 * sizeof(int));

pthread_cleanup_push(cleanup_pointer, (void *) p);

//do somethingpthread_testcancel();//do somethingpthread_cleanup_pop(0);free(p);pthread_testcancel();

}pthread_exit(NULL);

}52

int main(){

pthread_t tid;void* status;

if (pthread_create(&tid, NULL, thread_fun, NULL)

ASSERT(false);//do somethingreturn 0;

}

void cleanup_pointer(void* p){

free(p);}

Page 53: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Canceling threads

53

Page 54: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thread specific data (TSD)

• Sometimes it is useful to have data that is globally accessible to any function, yet still unique to the thread.

54

Page 55: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thread specific data (TSD)

int pthread_key_create(pthread_key_t *, void (*)(void *));

int pthread_key_delete(pthread_key_t);

int pthread_setspecific(pthread_key_t, const void *);

void *pthread_getspecific(pthread_key_t);

55

Page 56: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thread specific data example

pthread_key_t house_key;void foo(void * arg){

pthread_setspecific(house_key, arg);bar();

}void bar(){

float n;n = (float) pthread_getspecific(house_key);

}int main(){...

pthread_keycreate(&house_key, destroyer);pthread_create(&tid, NULL, foo, (void *) 1.414);pthread_create(&tid, NULL, foo, (void *) 3.141592653589);

...} 56

Page 57: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Synchronisation

• Mutexes• Semaphores• Condition Variables

57

Page 58: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Mutex

The mutual exclusion lock is the simplest and most primitive synchronization variable. It provides a single, absolute owner for the section of code (thus a critical section) that it brackets between the calls to pthread_mutex_lock()and pthread_mutex_unlock(). The first thread that locks the mutex gets ownership, and any subsequent attempts to lock it will fail, causing the calling thread to go to sleep.

58

Page 59: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Mutex initialisationNAME

pthread_mutex_init, pthread_mutex_destroy - initializes mutex with attr or destroys the mutex, making it unusable in any form

SYNOPSIS#include <pthread.h>

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); int pthread_mutex_destroy(pthread_mutex_t *mutex); 59

Page 60: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Mutex unlock operationNAME

pthread_mutex_unlock - unlocks mutex and wakes up the first thread sleeping on it.

SYNOPSIS#include <pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *mutex);

60

Page 61: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Mutex example

thread 1add(request_t *request){

pthread_mutex_lock(&lock);request->next = requests; requests = request pthread_mutex_unlock(&lock);

}

thread 2

request_t *remove(){ pthread_mutex_lock(&lock);

...sleeping...

request = requests;requests = requests->next;pthread_mutex_unlock(&lock)return(request);

}

61

Page 62: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Mutex example

62

Page 63: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphores

A counting semaphore6 is a variable that you can increment arbitrarily high, but decrement only to zero. A sem_post() operation increments the semaphore, while a sem_wait() attempts to decrement it. If the semaphore is greater than zero, the operation succeeds; if not, then the calling thread must go to sleep until a different thread increments it.

63

Page 64: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphores initialisationNAME

sem_init, sem_destroy - initializes the semaphore to value. If pshared is non-zero, then the semaphore will be sharable among processes. This destroys the semaphore.

SYNOPSIS#include <pthread.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);int sem_destroy(sem_t *sem);

64

Page 65: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphores operationsNAME

sem_post, sem_wait, sem_trywait - function increments the value of the semaphore or decrements the value of sem by one. If the semaphore’s value is zero, sem_wait() blocks, waiting for the semaphore to be incremented by another process or thread, while sem_trywait() will return immediately.

SYNOPSIS#include <pthread.h>

int sem_post(sem_t *sem);int sem_trywait(sem_t *sem);int sem_wait(sem_t *sem); 65

Page 66: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphores operations

66

Page 67: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphores operationsNAME

sem_open, sem_close - returns a pointer to the semaphore name. All processes which call this on the same name will get the same semaphore pointer or closes the named semaphore for this process.

SYNOPSIS#include <pthread.h>

sem_t *sem_open(char *name, int oflag,... );int sem_close(sem_t *sem);

67

Page 68: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphors example

68

Page 69: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Semaphors example

69

producer(){ request_t *request; while(1) { request = get_request(); add(request); sem_post(&requests_length); }}

consumer(){ request_t *request; while(1){ SEM_WAIT(&requests_length);

request = remove();process_request(request);

}}

request_t *get_request(){ request_t *request; request = (request_t *) malloc(sizeof(request_t)); request->data = read_from_net(); return(request)}

void process_request(request_t *request){ process(request->data); free(request);}

Page 70: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variables

70

Page 71: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable initialisation

NAMEpthread_cond_init, pthread_cond_destroy - initializes cond with att or destroys the condition variable, making it unusable in any form.

SYNOPSIS#include <pthread.h>

int pthread_cond_init(pthread_cond_t *cond, constpthread_condattr_t *attr); int pthread_cond_destroy(pthread_cond_t *cond);

71

Page 72: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable Wait Operation

NAMEpthread_cond_wait, pthread_cond_timewait - atomically releases mutex and causes the calling thread to block on cond. Upon successful return, the mutex will be reacquired.

SYNOPSIS#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t*mutex);int pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mutex, const struct timespec *abstime);

72

Page 73: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable Signal Operation

NAMEpthread_cond_signal, pthread_cond_broadcast - unblocks the first thread (if any) blocked on a condition variable or unblocks all threads blocked on a condition variable. You do notknow the order in which they awake.

SYNOPSIS#include <pthread.h>

int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond);

73

Page 74: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable Example

thread 1pthread_mutex_lock(&m);while (!my_condition) pthread_cond_wait(&c,

&m);

... sleeping ...

do_thing()pthread_mutex_unlock(&m);

thread 2

pthread_mutex_lock(&m);my_condition = TRUE;pthread_mutex_unlock(&m);pthread_cond_signal(&c);

74

Page 75: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable Example

75

Page 76: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Conditional Variable Example

void *producer(void *arg){ request_t *request; while(1) {

request = get_request();pthread_mutex_lock(&r_lock);while (length >= 10)

pthread_cond_wait(&r_producer, &r_lock); add(request); length++;

pthread_mutex_unlock(&r_lock); pthread_cond_signal(&r_consumer); }}

void *consumer(void *arg){ request_t *request; while(1) { pthread_mutex_lock(&r_lock); while (length == 0) pthread_cond_wait(&r_consumer, &r_lock); request = remove(); length--; pthread_mutex_unlock(&r_lock); pthread_cond_signal(&r_producer); process_request(request); }}

76

Page 77: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Multithread API’sWindows OS

77

Page 78: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

78

Page 79: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

79

Page 80: Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture III.

Thank You

80


Recommended