paralellism and posix threads -...

30
Paralellism and POSIX Threads Paolo Burgio [email protected]

Upload: others

Post on 25-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

Paralellism and

POSIX Threads

Paolo Burgio

[email protected]

Page 2: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ ..a core?

✓ …a program?

✓ …a process?

✓ …a thread?

✓ ..a task?

2

What is…

Programmazione II - 2016/17

Page 3: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ ..a core?

– An electronic circuit to execute instruction (=> programs)

✓ …a program?

– The implementation of an algorithm

✓ …a process?

– A program that is executing

✓ …a thread?

– A unit of execution (of a process)

✓ ..a task?

– A unit of work (of a program)

3

What is…

Programmazione II - 2016/17

Page 4: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ ..a core?

✓ …a program?

✓ …a process?

✓ …a thread?

✓ ..a task?

4

What is…

T

t

P

code.c DATA

CPU

0

CPU

1

CPU

3

CPU

2

MEM

DATADATA

code.c

Programmazione II - 2016/17

Page 5: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

Process

✓ "A program that executes"

Process

int main()

{

int c;

c = 11;

if(c == 11)

{

c--;

}

else

{

c++;

}

return 0;

}

T

Shared memory

5Programmazione II - 2016/17

Page 6: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

Process

✓ "A program that executes"

Process

int main()

{

int c;

c = 11;

if(c == 11)

{

c--;

}

else

{

c++;

}

return 0;

}

TShared memory

5Programmazione II - 2016/17

Page 7: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

Process

✓ "A program that executes"

Process

int main()

{

int c;

c = 11;

if(c == 11)

{

c--;

}

else

{

c++;

}

return 0;

}

T

T

Shared memory

5Programmazione II - 2016/17

Page 8: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

Process

✓ "A program that executes"

Process

int main()

{

int c;

c = 11;

if(c == 11)

{

c--;

}

else

{

c++;

}

return 0;

}

T

T

Shared memory

5Programmazione II - 2016/17

Page 9: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Processes have a given amount of RAM memory that can be shared

among concurrent threads

Shared memory

Process

Shared memory

T

(read, write)

(read, write)

DATUM

TT

6Programmazione II - 2016/17

Page 10: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Memory: centralized with uniform access time (UMA) and bus

interconnect, I/O

– Typically, multi-core (sub)systems (8-16 cores)

– Examples: Sun Enterprise 6000, SGI Challenge, Intel

Symmetric multi-processing

CPU

0

One or

more cache

levels

Main memory

CPU

1

One or

more cache

levels

CPU

2

One or

more cache

levels

CPU

3

One or

more cache

levels

I/O system

T TT T T

Can be 1 bus, N

busses, or any

network

7Programmazione II - 2016/17

Page 11: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

Ok, let's go

Page 12: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Specifies an operating system interface similar to most UNIX systems

– It extends the C language with primitives that allows the specification of the

concurrency

✓ POSIX distinguishes between the terms process and thread

– "A process is an address space with one or more threads executing in that

address space"

– "A thread is a single flow of control within a process (a unit of execution)"

✓ Every process has at least one thread

– the “main()” (aka "master") thread; its termination ends the process

– All the threads share the same address space, and have a private stack

The POSIX IEEE standard

Programmazione II - 2016/17 9

Page 13: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ The pthread primitives are usually implemented into a pthread library

✓ All the declarations of the primitives cited in these slides can be found into sched.h, pthread.h and semaphore.h

– Use man to get on-line documentation

✓ When compiling under gcc & GNU/Linux, remember the –lpthread option!

The PThread library

Programmazione II - 2016/17 10

Page 14: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

PThread creation,

join, end

Programmazione II - 2016/17 11

Page 15: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ A (P)thread is identified by a C function, called body:

void *my_pthread_fn(void *arg)

{

}

✓ A thread starts with the first instruction of its body

✓ The threads ends when the body function ends

– it's not the only way a thread can die

PThread body

Programmazione II - 2016/17 12

Page 16: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Thread can be created using the primitive

int pthread_create ( pthread_t *ID,

pthread_attr_t *attr,

void *(*body)(void *),

void * arg

);

✓ pthread_t is the type that contains the thread ID

✓ pthread_attr_t is the type that contains the parameters of the

thread

✓ arg is the argument passed to the thread body when it starts

Programmazione II - 2016/17 13

Thread creation

Page 17: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Thread attributes specifies the characteristics of a thread

– Stack size and address

– Detach state (joinable or detached)

– Scheduling parameters (priority, …)

✓ Attributes must be initialized and destroyed

– int pthread_attr_init(pthread_attr_t *attr);

– int pthread_attr_destroy(pthread_attr_t *attr);

Thread attributes

Programmazione II - 2016/17 14

Page 18: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ A thread can terminate itself calling

void pthread_exit(void *retval);

✓ When the thread body ends after the last “}”, pthread_exit() is

called implicitly

✓ Exception: when main() terminates, exit() is called implicitly

Thread termination

Programmazione II - 2016/17 15

Page 19: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Each thread has a unique ID

✓ The thread ID of the current thread can be obtained using

pthread_t pthread_self(void);

✓ Two thread IDs can be compared using

int pthread_equal( pthread_t thread1,

pthread_t thread2 );

Thread IDs

Programmazione II - 2016/17 16

Page 20: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ A thread can wait the termination of another thread using

int pthread_join ( pthread_t th,

void **thread_return);

✓ It gets the return value of the thread or PTHREAD_CANCELED if the

thread has been killed

✓ By default, every thread must be joined

– The join frees all the internal resources

– Stack, registers, and so on

Joining a thread

Programmazione II - 2016/17 17

Page 21: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ A thread that does not need to be joined has to be declared as

detached

✓ 2 ways to have it:

– While creating (in father thread) usingpthread_attr_setdetachstate()

– The thread itself can become detached calling in its bodypthread_detach()

✓ Joining a detached thread returns an error

Joining a thread (2)

Programmazione II - 2016/17 18

Page 22: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Filename: ex_create.c

✓ The demo explains how to create a thread

– the main() thread creates another thread (called body())

– the body() thread checks the thread Ids using pthread_equal() and

then ends

– the main() thread joins the body() thread

✓ Credits to PJ

Example

Programmazione II - 2016/17 19

Let's

code!

Page 23: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

The "variable increment" problem

Process

/* Shared var */

int x;

void * body(void * arg)

{

x++;

}

T T

Shared memory

11

void * body(void * arg)

{

int local_x = x;

local_x = local_x + 1;

x = local_x;

}

x

local_x = 11;

local_x = 11;

local_x = 12;

x = 12;

local_x = 12;

x = 12;

Page 24: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

POSIX semaphores

21

Page 25: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ A semaphore is a counter managed with a set of primitives

✓ It is used for

– Synchronization

– Mutual exclusion

✓ POSIX Semaphores can be

– Unnamed (local to a process)

– Named (shared between processed through a file descriptor)

Semaphores

Programmazione II - 2016/17 22

Page 26: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Mainly used with multithread applications

✓ Operations permitted:

– initialization /destruction

– blocking wait / nonblocking wait

• counter decrement

– post

• counter increment

– counter reading

• simply returns the counter

Unnamed semaphores

Programmazione II - 2016/17 23

Page 27: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ The sem_t type contains all the semaphore data structures

int sem_init(sem_t *sem, int pshared,

unsigned int value);

– pshared is 0 if sem is not shared between processes

int sem_destroy(sem_t *sem)

– It destroys the sem semaphore

Initializing a semaphore

Programmazione II - 2016/17 24

Page 28: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

int sem_wait(sem_t *sem);

int sem_trywait(sem_t *sem);

✓ Under the hood..

✓ Blocks the calling thread

✓ If the counter is greater than 0 the thread does not block

– sem_trywait never blocks

Semaphore waits

Programmazione II - 2016/17 25

Page 29: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

int sem_post(sem_t *sem);

– It increments the semaphore counter

– It unblocks a waiting thread

int sem_getvalue(sem_t *sem,int *val);

– It simply returns the semaphore counter

Other semaphore primitives

Programmazione II - 2016/17 26

Page 30: Paralellism and POSIX Threads - UNIMOREalgogroup.unimore.it/.../programmazioneII-1617/Pthreads.pdf · POSIX distinguishes between the terms process and thread – "A process is an

© 2016 Università degli studi di Modena

✓ Filename: ex_sem.c

✓ In this example, semaphores are used to implement mutual exclusion

in the output of a character in the console.

Example

Programmazione II - 2016/17 27

Let's

code!