Concurrency. Johan Montelius KTH

Size: px
Start display at page:

Download "Concurrency. Johan Montelius KTH"

Transcription

1 Concurrency Johan Montelius KTH / 32

2 What is concurrency? 2 / 32

3 What is concurrency? Concurrency: (the illusion of) happening at the same time. 2 / 32

4 What is concurrency? Concurrency: (the illusion of) happening at the same time. A property of the programing model. 2 / 32

5 What is concurrency? Concurrency: (the illusion of) happening at the same time. A property of the programing model. Why would we want to do things concurrently? 2 / 32

6 What is parallelism? 3 / 32

7 What is parallelism? Parallelism: the ability to do several things at the same time. 3 / 32

8 What is parallelism? Parallelism: the ability to do several things at the same time. A property of the execution. 3 / 32

9 What is parallelism? Parallelism: the ability to do several things at the same time. A property of the execution. Why would we want to do things in parallel? 3 / 32

10 concurrency vs parallelism parallel execution sequential sequential programing model concurrent 4 / 32

11 Why in this course? The problem of concurrency was first encountered in the implementation of operating systems. It has since been a central part in any course on operating systems. 5 / 32

12 Why in this course? The problem of concurrency was first encountered in the implementation of operating systems. It has since been a central part in any course on operating systems. Today - concurrency is such an important topic that it could (and often do) fill up a course of it s own. 5 / 32

13 What is the problem? If concurrent activities are not manipulating a shared resource then it s not a problem. 6 / 32

14 What is the problem? If concurrent activities are not manipulating a shared resource then it s not a problem. We often want to share resources between concurrent activities. 6 / 32

15 What is the problem? If concurrent activities are not manipulating a shared resource then it s not a problem. We often want to share resources between concurrent activities. What do two UNIX processes share? 6 / 32

16 A process As we have learned - the unit of a computation. 7 / 32

17 A process As we have learned - the unit of a computation. a program 7 / 32

18 A process As we have learned - the unit of a computation. a program an instruction pointer 7 / 32

19 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack 7 / 32

20 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack a data segment for static data structures 7 / 32

21 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack a data segment for static data structures a heap for dynamic data structures 7 / 32

22 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack a data segment for static data structures a heap for dynamic data structures a file table of open files 7 / 32

23 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack a data segment for static data structures a heap for dynamic data structures a file table of open files signal handlers,... 7 / 32

24 A process As we have learned - the unit of a computation. a program an instruction pointer a computation stack a data segment for static data structures a heap for dynamic data structures a file table of open files signal handlers,... 7 / 32

25 A thread process 8 / 32

26 A thread code process 8 / 32

27 A thread code process data/heap 8 / 32

28 A thread code process stack data/heap 8 / 32

29 A thread code process IP stack data/heap 8 / 32

30 A thread code process IP stack data/heap 8 / 32

31 A thread code process IP stack data/heap 8 / 32

32 A thread code process IP stack data/heap 8 / 32

33 A thread code process IP stack data/heap 8 / 32

34 A thread code thread IP stack process data/heap 8 / 32

35 A thread code thread thread process IP stack IP stack data/heap 8 / 32

36 A thread code thread thread thread process IP stack IP stack IP stack data/heap 8 / 32

37 Virtual memory layout 9 / 32

38 Virtual memory layout 9 / 32

39 Virtual memory layout kernel 9 / 32

40 Virtual memory layout code (.text) kernel 9 / 32

41 Virtual memory layout code (.text) data kernel 9 / 32

42 Virtual memory layout code (.text) data heap kernel 9 / 32

43 Virtual memory layout code (.text) data heap stack kernel 9 / 32

44 Virtual memory layout code (.text) data heap stack stack kernel 9 / 32

45 threads API # include < pthread.h> # include <stdio.h> int loop = 10; int count = 0; void * hello ( char * name ) { for ( int i = 0; i < loop ; i ++) { count ++; printf (" hello %s %d\n", name, count ); } } int main () { pthread_ t p1; pthread_create (&p1, NULL, hello, "A"); pthread_join (p1, NULL ); return 0; 10 / 32

46 Concurrency 11 / 32

47 Concurrency What is the problem? 11 / 32

48 Cache coherence 12 / 32

49 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. 12 / 32

50 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. All write operations to a single memory location: are atomic, 12 / 32

51 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. All write operations to a single memory location: are atomic, performed in program order and 12 / 32

52 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. All write operations to a single memory location: are atomic, performed in program order and seen by all processes in a total order. 12 / 32

53 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. All write operations to a single memory location: are atomic, performed in program order and seen by all processes in a total order. The C compiler can do optimizations that we are not prepared for. 12 / 32

54 Cache coherence The CPU uses caches to improve performance, a cache protocol must provide coherence. All write operations to a single memory location: are atomic, performed in program order and seen by all processes in a total order. The C compiler can do optimizations that we are not prepared for. There are several alternatives of how coherence is defined, this is one example 12 / 32

55 More problems 13 / 32

56 More problems What is the expected outcome of an execution? 13 / 32

57 Sequential consistency The outcome is the same as if all the operations of the program were executed: 14 / 32

58 Sequential consistency The outcome is the same as if all the operations of the program were executed: as atomic operations in some sequence, 14 / 32

59 Sequential consistency The outcome is the same as if all the operations of the program were executed: as atomic operations in some sequence, consistent with the program order of each thread. 14 / 32

60 the code int loop = 10; int count = 0; void * hello ( void *) { : for ( int i = 0; i < loop ; i ++) { count ++; } : } 15 / 32

61 the code int loop = 10; int count = 0; void * hello ( void *) { : for ( int i = 0; i < loop ; i ++) { count ++; } : }.L3 : movl count (% rip ), % eax addl $1, % eax movl %eax, count (% rip ) addl $1, -4(% rbp ) movl loop (% rip ), % eax cmpl %eax, -4(% rbp ) jl.l3 15 / 32

62 16 / 32

63 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } 17 / 32

64 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 17 / 32

65 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 a c b 17 / 32

66 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread a c b 17 / 32

67 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 my = a c b 17 / 32

68 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 my = a c b 17 / 32

69 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 read your a c b 17 / 32

70 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread a c b 17 / 32

71 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 read count a c b 17 / 32

72 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 count = a c b 17 / 32

73 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 count = a c b 17 / 32

74 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 my = a c b 17 / 32

75 What about this? int count = 7; volatile int a = 0; volatile int b = 0; void critical (... ) { : while (1) { my = 1; if( your == 0) { count ++; my = 0; break ; } else { my = 0; } } } Thread 1 Thread 2 my = a c b 17 / 32

76 Total Store Order (TSO) 18 / 32

77 Total Store Order (TSO) Modern CPU:s do not provide sequential consistency, they only provide Total Store Order. 18 / 32

78 Total Store Order (TSO) Modern CPU:s do not provide sequential consistency, they only provide Total Store Order. Write operations are performed in a total order. 18 / 32

79 Total Store Order (TSO) Modern CPU:s do not provide sequential consistency, they only provide Total Store Order. Write operations are performed in a total order. A process will immediately see its own store operations but, 18 / 32

80 Total Store Order (TSO) Modern CPU:s do not provide sequential consistency, they only provide Total Store Order. Write operations are performed in a total order. A process will immediately see its own store operations but,... a read operation might bypass a write operation of another memory location. 18 / 32

81 Total Store Order (TSO) Modern CPU:s do not provide sequential consistency, they only provide Total Store Order. Write operations are performed in a total order. A process will immediately see its own store operations but,... a read operation might bypass a write operation of another memory location. There are operations provided by the hardware that will give us better guarantees. 18 / 32

82 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 19 / 32

83 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b / 32

84 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a = 1 a b / 32

85 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 19 / 32

86 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a 19 / 32

87 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a / 32

88 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a / 32

89 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a / 32

90 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a / 32

91 Total Store Order WARNING: the following sequence contains scenes that some viewers may find disturbing. P1 P2 a b 0 0 a = 1 b = 1 read b read a / 32

92 Hardware support - TGH 20 / 32

93 Hardware support - TGH TGH - Thank God for Hardware 20 / 32

94 Hardware support - TGH TGH - Thank God for Hardware Fences, barriers etc: all load and store operations before a fence are guaranteed to be performed before any operations after the fence. Atomic-swap, test-and-set etc: an instructions that reads and writes to a memory location in one atomic operation. 20 / 32

95 Hardware support - TGH TGH - Thank God for Hardware Fences, barriers etc: all load and store operations before a fence are guaranteed to be performed before any operations after the fence. Atomic-swap, test-and-set etc: an instructions that reads and writes to a memory location in one atomic operation. Modern CPU:s provide very weak consistency guarantees if these operations are not used. Don t rely on the program order of your code. 20 / 32

96 Hardware support - TGH TGH - Thank God for Hardware Fences, barriers etc: all load and store operations before a fence are guaranteed to be performed before any operations after the fence. Atomic-swap, test-and-set etc: an instructions that reads and writes to a memory location in one atomic operation. Modern CPU:s provide very weak consistency guarantees if these operations are not used. Don t rely on the program order of your code. Better still - if possible, use a library that handles synchronization. 20 / 32

97 How to synchronize 21 / 32

98 How to synchronize Next week. 21 / 32

99 How to implement threads threads in user space threads in kernel space 22 / 32

100 How to implement threads threads in user space threads in kernel space kernel kernel 22 / 32

101 How to implement threads threads in user space threads in kernel space process kernel kernel 22 / 32

102 How to implement threads threads in user space threads in kernel space process kernel kernel 22 / 32

103 How to implement threads threads in user space threads in kernel space process kernel kernel 22 / 32

104 How to implement threads threads in user space threads in kernel space scheduler process kernel kernel 22 / 32

105 How to implement threads threads in user space threads in kernel space scheduler process kernel kernel 22 / 32

106 How to implement threads threads in user space threads in kernel space scheduler process kernel kernel 22 / 32

107 How to implement threads threads in user space threads in kernel space scheduler process kernel kernel 22 / 32

108 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

109 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

110 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

111 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

112 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

113 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

114 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

115 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

116 How to implement threads threads in user space threads in kernel space scheduler process processes kernel kernel 22 / 32

117 pros and cons Threads in user space: + You can change scheduler. 23 / 32

118 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. 23 / 32

119 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. 23 / 32

120 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. 23 / 32

121 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. 23 / 32

122 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. + A process can utilize multiple cores. 23 / 32

123 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. + A process can utilize multiple cores. - Thread scheduling requires trap to kernel. 23 / 32

124 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. + A process can utilize multiple cores. - Thread scheduling requires trap to kernel. - No way to change scheduler for a process. 23 / 32

125 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. + A process can utilize multiple cores. - Thread scheduling requires trap to kernel. - No way to change scheduler for a process. 23 / 32

126 pros and cons Threads in user space: + You can change scheduler. + Very fast task switching. - If the process is suspended, all threads are. - A process can not utilize multiple cores. Threads in kernel space: + One thread can suspend while other continue to execute. + A process can utilize multiple cores. - Thread scheduling requires trap to kernel. - No way to change scheduler for a process. Which approach is taken by GNU/Linux? 23 / 32

127 Java, Haskell and Erlang How is this handled in high level languages? 24 / 32

128 Java, Haskell and Erlang How is this handled in high level languages? Java: each Java thread mapped to one operating system thread. 24 / 32

129 Java, Haskell and Erlang How is this handled in high level languages? Java: each Java thread mapped to one operating system thread. Erlang and Haskell: Language threads scheduled by the virtual machine. The virtual machine will use several operating system threads to have several outstanding system calls, utilize multiple cores etc. 24 / 32

130 Java, Haskell and Erlang How is this handled in high level languages? Java: each Java thread mapped to one operating system thread. Erlang and Haskell: Language threads scheduled by the virtual machine. The virtual machine will use several operating system threads to have several outstanding system calls, utilize multiple cores etc. 24 / 32

131 Java, Haskell and Erlang How is this handled in high level languages? Java: each Java thread mapped to one operating system thread. Erlang and Haskell: Language threads scheduled by the virtual machine. The virtual machine will use several operating system threads to have several outstanding system calls, utilize multiple cores etc. Java originally had user space threads, and introduced the name, green threads. This was later replaced by native threads i.e. each Java thread attached to a kernel operating system thread. 24 / 32

132 an experiment How long time does it take to send a message around a ring of a hundred threads? 25 / 32

133 pthread_create() - from man pages #include <pthread.h> 26 / 32

134 pthread_create() - from man pages #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 26 / 32

135 pthread_create() - from man pages #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); pthread_t *thread : a pointer to a thread structure. 26 / 32

136 pthread_create() - from man pages #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); pthread_t *thread : a pointer to a thread structure. const pthread_attr_t *attr : a pointer to a structure that are the attributes of the thread. 26 / 32

137 pthread_create() - from man pages #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); pthread_t *thread : a pointer to a thread structure. const pthread_attr_t *attr : a pointer to a structure that are the attributes of the thread. void *(*start_routine) (void *) : a pointer to a function that takes one argument, (void*), with return value void*. void *arg : the arguments to the function, given as a a void *. 26 / 32

138 pthread_create() - from man pages #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); pthread_t *thread : a pointer to a thread structure. const pthread_attr_t *attr : a pointer to a structure that are the attributes of the thread. void *(*start_routine) (void *) : a pointer to a function that takes one argument, (void*), with return value void*. void *arg : the arguments to the function, given as a a void *. Compile and link with -pthread. 26 / 32

139 Pthreads in Linux How do we implement threads in Linux? 27 / 32

140 Pthreads in Linux How do we implement threads in Linux? In Linux, both fork() and pthread_create() are implemented using the system call clone(). 27 / 32

141 Pthreads in Linux How do we implement threads in Linux? In Linux, both fork() and pthread_create() are implemented using the system call clone(). What is clone()? 27 / 32

142 clone() - from man pages Unlike fork(2), clone() allows the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. 28 / 32

143 clone() - from man pages Unlike fork(2), clone() allows the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. The system call clone() allows us to define how much should be shared: fork(): copy table of file descriptors, copy memory space and signal handlers i.e a perfect copy pthread_create(): share table of file descriptors and memory, copy signal handlers 28 / 32

144 clone() - from man pages Unlike fork(2), clone() allows the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. The system call clone() allows us to define how much should be shared: fork(): copy table of file descriptors, copy memory space and signal handlers i.e a perfect copy pthread_create(): share table of file descriptors and memory, copy signal handlers Using clone() directly you can pick and choose of more than twenty parameters what the clone should share. 28 / 32

145 Thread Local Storage (TLS) All threads have their own stack, the heap is shared. 29 / 32

146 Thread Local Storage (TLS) All threads have their own stack, the heap is shared. Would it not be nice to have some thread local storage? 29 / 32

147 Thread Local Storage (TLS) All threads have their own stack, the heap is shared. Would it not be nice to have some thread local storage? thread int local = 42; 29 / 32

148 TLS implementation thread int local = 0; int global = 1; void * hello ( void * name ) { } int stk = 2; int sum = local + global + stk ; 30 / 32

149 TLS implementation thread int local = 0; int global = 1; void * hello ( void * name ) { } int stk = 2; int sum = local + global + stk ; pushq % rbp movq %rsp, % rbp movq %rdi, -24(% rbp ) movl $2, -8(% rbp ) movl %fs: local@tpoff, % edx movl global (% rip ), % eax addl %eax, % edx movl -8(% rbp ), % eax addl %edx, % eax movl %eax, -4(% rbp ) nop popq % rbp ret $ 30 / 32

150 remember segmentation The TLS is referenced using the segment selector fs:. 31 / 32

151 remember segmentation The TLS is referenced using the segment selector fs:. When we change thread, the kernel sets the fs selector register. 31 / 32

152 remember segmentation The TLS is referenced using the segment selector fs:. When we change thread, the kernel sets the fs selector register. The TLS has an original copy that is copied by each thread (even the mother thread) before any write operations. 31 / 32

153 remember segmentation The TLS is referenced using the segment selector fs:. When we change thread, the kernel sets the fs selector register. The TLS has an original copy that is copied by each thread (even the mother thread) before any write operations. You can take an address of a TLS structure and pass it to another thread. 31 / 32

154 remember segmentation The TLS is referenced using the segment selector fs:. When we change thread, the kernel sets the fs selector register. The TLS has an original copy that is copied by each thread (even the mother thread) before any write operations. You can take an address of a TLS structure and pass it to another thread. 31 / 32

155 Summary 32 / 32

156 Summary Concurrency vs parallelism? 32 / 32

157 Summary Concurrency vs parallelism? What is a thread? 32 / 32

158 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? 32 / 32

159 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order 32 / 32

160 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order Threads in kernel or user space? 32 / 32

161 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order Threads in kernel or user space? Threads in GNU/Linux and clone(). 32 / 32

162 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order Threads in kernel or user space? Threads in GNU/Linux and clone(). What is Thread Local Storage? 32 / 32

163 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order Threads in kernel or user space? Threads in GNU/Linux and clone(). What is Thread Local Storage? 32 / 32

164 Summary Concurrency vs parallelism? What is a thread? What do threads of process share? Sequential Consistency vs Total Store Order Threads in kernel or user space? Threads in GNU/Linux and clone(). What is Thread Local Storage? 32 / 32

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time. What is concurrency? Concurrency Johan Montelius KTH 2017 Concurrency: (the illusion of) happening at the same time. A property of the programing model. Why would we want to do things concurrently? What

More information

pthreads CS449 Fall 2017

pthreads CS449 Fall 2017 pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate

More information

CS 3305 Intro to Threads. Lecture 6

CS 3305 Intro to Threads. Lecture 6 CS 3305 Intro to Threads Lecture 6 Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer Introduction Applications often need to perform

More information

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

Threads. Threads (continued)

Threads. Threads (continued) Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state

More information

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή EPL372 Lab Exercise 2: Threads and pthreads Εργαστήριο 2 Πέτρος Παναγή 1 Threads Vs Processes 2 Process A process is created by the operating system, and requires a fair amount of "overhead". Processes

More information

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system

More information

Threads. studykorner.org

Threads. studykorner.org Threads Thread Subpart of a process Basic unit of CPU utilization Smallest set of programmed instructions, can be managed independently by OS No independent existence (process dependent) Light Weight Process

More information

Locks and semaphores. Johan Montelius KTH

Locks and semaphores. Johan Montelius KTH Locks and semaphores Johan Montelius KTH 2017 1 / 41 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;

More information

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a recap, what s the problem Locks and semaphores Johan Montelius KTH 2017 # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++; int main

More information

Locks and semaphores. Johan Montelius KTH

Locks and semaphores. Johan Montelius KTH Locks and semaphores Johan Montelius KTH 2018 1 / 40 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;

More information

Processes. Johan Montelius KTH

Processes. Johan Montelius KTH Processes Johan Montelius KTH 2017 1 / 47 A process What is a process?... a computation a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other

More information

A process. the stack

A process. the stack A process Processes Johan Montelius What is a process?... a computation KTH 2017 a program i.e. a sequence of operations a set of data structures a set of registers means to interact with other processes

More information

Introduction to PThreads and Basic Synchronization

Introduction to PThreads and Basic Synchronization Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization

More information

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

CSci 4061 Introduction to Operating Systems. (Threads-POSIX) CSci 4061 Introduction to Operating Systems (Threads-POSIX) How do I program them? General Thread Operations Create/Fork Allocate memory for stack, perform bookkeeping Parent thread creates child threads

More information

CS333 Intro to Operating Systems. Jonathan Walpole

CS333 Intro to Operating Systems. Jonathan Walpole CS333 Intro to Operating Systems Jonathan Walpole Threads & Concurrency 2 Threads Processes have the following components: - an address space - a collection of operating system state - a CPU context or

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole The Process Concept 2 The Process Concept Process a program in execution Program - description of how to perform an activity instructions and static

More information

CSE 333 SECTION 9. Threads

CSE 333 SECTION 9. Threads CSE 333 SECTION 9 Threads HW4 How s HW4 going? Any Questions? Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts

More information

Introduction to pthreads

Introduction to pthreads CS 220: Introduction to Parallel Computing Introduction to pthreads Lecture 25 Threads In computing, a thread is the smallest schedulable unit of execution Your operating system has a scheduler that decides

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole Threads & Concurrency 2 Why Use Threads? Utilize multiple CPU s concurrently Low cost communication via shared memory Overlap computation and blocking

More information

Section 4: Threads and Context Switching

Section 4: Threads and Context Switching CS162 September 19-20, 2017 Contents 1 Warmup 2 1.1 Hello World............................................ 2 2 Vocabulary 2 3 Problems 3 3.1 Join................................................ 3 3.2

More information

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2 CS162 September 15, 2016 Contents 1 Warmup 2 1.1 Hello World............................................ 2 2 Vocabulary 2 3 Problems 3 3.1 Join................................................ 3 3.2 Stack

More information

Operating systems fundamentals - B06

Operating systems fundamentals - B06 Operating systems fundamentals - B06 David Kendall Northumbria University David Kendall (Northumbria University) Operating systems fundamentals - B06 1 / 12 Introduction Introduction to threads Reminder

More information

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process EECS 3221 Operating System Fundamentals What is thread? Thread Concept No. 3 Thread Difference between a process and a thread Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

More information

LSN 13 Linux Concurrency Mechanisms

LSN 13 Linux Concurrency Mechanisms LSN 13 Linux Concurrency Mechanisms ECT362 Operating Systems Department of Engineering Technology LSN 13 Creating Processes fork() system call Returns PID of the child process created The new process is

More information

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5 CS 153 Lab4 and 5 Kishore Kumar Pusukuri Outline Introduction A thread is a straightforward concept : a single sequential flow of control. In traditional operating systems, each process has an address

More information

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits CS307 What is a thread? Threads A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution

More information

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001 15-213 The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing data

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

CSE 306/506 Operating Systems Threads. YoungMin Kwon

CSE 306/506 Operating Systems Threads. YoungMin Kwon CSE 306/506 Operating Systems Threads YoungMin Kwon Processes and Threads Two characteristics of a process Resource ownership Virtual address space (program, data, stack, PCB ) Main memory, I/O devices,

More information

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Threads Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Concurrency

More information

Threaded Programming. Lecture 9: Alternatives to OpenMP

Threaded Programming. Lecture 9: Alternatives to OpenMP Threaded Programming Lecture 9: Alternatives to OpenMP What s wrong with OpenMP? OpenMP is designed for programs where you want a fixed number of threads, and you always want the threads to be consuming

More information

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting

More information

Multithreaded Programming

Multithreaded Programming Multithreaded Programming The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. September 4, 2014 Topics Overview

More information

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling Threads Today Why threads? Thread model & implementation Next time CPU Scheduling What s in a process A process consists of (at least): An address space Code and data for the running program Thread state

More information

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization

Threads. To do. Why threads? Thread model & implementation. q q q. q Next time: Synchronization Threads To do q q q Why threads? Thread model & implementation q Next time: Synchronization What s in a process A process consists of (at least): An address space Code and data for the running program

More information

Threads (light weight processes) Chester Rebeiro IIT Madras

Threads (light weight processes) Chester Rebeiro IIT Madras Threads (light weight processes) Chester Rebeiro IIT Madras 1 Processes Separate streams of execution Each process isolated from the other Process state contains Process ID Environment Working directory.

More information

Lecture 19: Shared Memory & Synchronization

Lecture 19: Shared Memory & Synchronization Lecture 19: Shared Memory & Synchronization COMP 524 Programming Language Concepts Stephen Olivier April 16, 2009 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts Forking int pid;

More information

Creating Threads. Programming Details. COMP750 Distributed Systems

Creating Threads. Programming Details. COMP750 Distributed Systems Creating Threads Programming Details COMP750 Distributed Systems Thread and Process Creation Processes can be created on Unix systems in C or C++ using the fork() function. Threads can be created in C

More information

High Performance Computing Course Notes Shared Memory Parallel Programming

High Performance Computing Course Notes Shared Memory Parallel Programming High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed

More information

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017 CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming

More information

Last class: Today: Thread Background. Thread Systems

Last class: Today: Thread Background. Thread Systems 1 Last class: Thread Background Today: Thread Systems 2 Threading Systems 3 What kind of problems would you solve with threads? Imagine you are building a web server You could allocate a pool of threads,

More information

CS 326: Operating Systems. Process Execution. Lecture 5

CS 326: Operating Systems. Process Execution. Lecture 5 CS 326: Operating Systems Process Execution Lecture 5 Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 2 Today s Schedule Process Creation

More information

Processes and Threads

Processes and Threads TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Processes and Threads [SGG7] Chapters 3 and 4 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

More information

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads Exercise (could be a quiz) 1 2 Solution CSE 421/521 - Operating Systems Fall 2013 Lecture - IV Threads Tevfik Koşar 3 University at Buffalo September 12 th, 2013 4 Roadmap Threads Why do we need them?

More information

Computer Systems Laboratory Sungkyunkwan University

Computer Systems Laboratory Sungkyunkwan University Concurrent Programming Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet, SOCK_STREAM, 0); bzero((char

More information

CISC2200 Threads Spring 2015

CISC2200 Threads Spring 2015 CISC2200 Threads Spring 2015 Process We learn the concept of process A program in execution A process owns some resources A process executes a program => execution state, PC, We learn that bash creates

More information

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control Processes & Threads Concurrent Programs Process = Address space + one thread of control Concurrent program = multiple threads of control Multiple single-threaded processes Multi-threaded process 2 1 Concurrent

More information

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100 ECE 2035(A) Programming for Hardware/Software Systems Fall 2013 Exam Three November 20 th 2013 Name: Q1: /8 Q2: /30 Q3: /30 Q4: /32 Total: /100 1/10 For functional call related questions, let s assume

More information

Computer Systems Assignment 2: Fork and Threads Package

Computer Systems Assignment 2: Fork and Threads Package Autumn Term 2018 Distributed Computing Computer Systems Assignment 2: Fork and Threads Package Assigned on: October 5, 2018 Due by: October 12, 2018 1 Understanding fork() and exec() Creating new processes

More information

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005 Threads vs. Processes Lecture 4 Thread Programming January 21, 2005 fork() is expensive (time, memory) Interprocess communication is hard. Threads are lightweight processes: one process can contain several

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

Operating Systems & Concurrency: Process Concepts

Operating Systems & Concurrency: Process Concepts Operating Systems & Concurrency: Process Concepts Michael Brockway October 6, 2011 Outline Processes - context, data area, states Process creation, termination unix examples Processes and threads Processes

More information

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010 CSC 0 - Systems Programming Fall 00 Lecture - XIII Concurrent Programming - I Roadmap Sequential vs Concurrent Programming Shared Memory vs Message Passing Divide and Compute Threads vs Processes POSIX

More information

Thread and Synchronization

Thread and Synchronization Thread and Synchronization pthread Programming (Module 19) Yann-Hang Lee Arizona State University yhlee@asu.edu (480) 727-7507 Summer 2014 Real-time Systems Lab, Computer Science and Engineering, ASU Pthread

More information

THREADS: (abstract CPUs)

THREADS: (abstract CPUs) CS 61 Scribe Notes (November 29, 2012) Mu, Nagler, Strominger TODAY: Threads, Synchronization - Pset 5! AT LONG LAST! Adversarial network pong handling dropped packets, server delays, overloads with connection

More information

Concurrent Programming

Concurrent Programming Concurrent Programming is Hard! Concurrent Programming Kai Shen The human mind tends to be sequential Thinking about all possible sequences of events in a computer system is at least error prone and frequently

More information

ENCM 501 Winter 2019 Assignment 9

ENCM 501 Winter 2019 Assignment 9 page 1 of 6 ENCM 501 Winter 2019 Assignment 9 Steve Norman Department of Electrical & Computer Engineering University of Calgary April 2019 Assignment instructions and other documents for ENCM 501 can

More information

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science High Performance Computing Lecture 21 Matthew Jacob Indian Institute of Science Semaphore Examples Semaphores can do more than mutex locks Example: Consider our concurrent program where process P1 reads

More information

Machine Program: Procedure. Zhaoguo Wang

Machine Program: Procedure. Zhaoguo Wang Machine Program: Procedure Zhaoguo Wang Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z; Requirements of procedure calls? P() { y = Q(x); y++;

More information

Concurrent Programming

Concurrent Programming Concurrent Programming Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int

More information

POSIX Threads: a first step toward parallel programming. George Bosilca

POSIX Threads: a first step toward parallel programming. George Bosilca POSIX Threads: a first step toward parallel programming George Bosilca bosilca@icl.utk.edu Process vs. Thread A process is a collection of virtual memory space, code, data, and system resources. A thread

More information

Creating Threads COMP755

Creating Threads COMP755 Creating Threads COMP755 "I pledged California to a Northern Republic and to a flag that should have no treacherous threads of cotton in its warp, and the audience came down in thunder." Thomas Starr King

More information

Memory management. Johan Montelius KTH

Memory management. Johan Montelius KTH Memory management Johan Montelius KTH 2017 1 / 22 C program # include int global = 42; int main ( int argc, char * argv []) { if( argc < 2) return -1; int n = atoi ( argv [1]); int on_stack

More information

PRACE Autumn School Basic Programming Models

PRACE Autumn School Basic Programming Models PRACE Autumn School 2010 Basic Programming Models Basic Programming Models - Outline Introduction Key concepts Architectures Programming models Programming languages Compilers Operating system & libraries

More information

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CMPSC 311- Introduction to Systems Programming Module: Concurrency CMPSC 311- Introduction to Systems Programming Module: Concurrency Professor Patrick McDaniel Fall 2013 Sequential Programming Processing a network connection as it arrives and fulfilling the exchange

More information

CS533 Concepts of Operating Systems. Jonathan Walpole

CS533 Concepts of Operating Systems. Jonathan Walpole CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?

More information

Concurrent Programming

Concurrent Programming Concurrent Programming is Hard! Concurrent Programming Kai Shen The human mind tends to be sequential Thinking about all possible sequences of events in a computer system is at least error prone and frequently

More information

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

CSC209H Lecture 11. Dan Zingaro. March 25, 2015 CSC209H Lecture 11 Dan Zingaro March 25, 2015 Level- and Edge-Triggering (Kerrisk 63.1.1) When is an FD ready? Two answers: Level-triggered: when an operation will not block (e.g. read will not block),

More information

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CMPSC 311- Introduction to Systems Programming Module: Concurrency CMPSC 311- Introduction to Systems Programming Module: Concurrency Professor Patrick McDaniel Fall 2016 Sequential Programming Processing a network connection as it arrives and fulfilling the exchange

More information

CS170 Operating Systems. Discussion Section Week 4 Project 2 - Thread

CS170 Operating Systems. Discussion Section Week 4 Project 2 - Thread CS170 Operating Systems Discussion Section Week 4 Project 2 - Thread Project-1 feedback How to compile 1. create threads.h and threads.c 2. include "threads.h" in your test.c file 3. gcc -c -o threads.o

More information

Threads. CS3026 Operating Systems Lecture 06

Threads. CS3026 Operating Systems Lecture 06 Threads CS3026 Operating Systems Lecture 06 Multithreading Multithreading is the ability of an operating system to support multiple threads of execution within a single process Processes have at least

More information

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective Part I: Operating system overview: Processes and threads 1 Overview Process concept Process scheduling Thread

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 8 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ How many partners can we cave for project:

More information

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1 Threads CS-3013 Operating Systems Hugh C. Lauer (Slides include materials from Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts,

More information

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) pthreads 1 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) 2 1 Thread Packages Kernel thread packages Implemented and supported at kernel

More information

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Concurrency and Synchronization ECE 650 Systems Programming & Engineering Duke University, Spring 2018 Concurrency Multiprogramming Supported by most all current operating systems More than one unit of

More information

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam 95.300 Operating Systems Threads and Signals Amir Ghavam Winter 2002 1 Traditional Process Child processes created from a parent process using fork Drawbacks Fork is expensive: Memory is copied from a

More information

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads Overview Administrative * HW 2 Grades * HW 3 Due Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads * Basic Thread Management * User vs. Kernel Threads * Thread Attributes

More information

PThreads in a Nutshell

PThreads in a Nutshell PThreads in a Nutshell Chris Kauffman CS 499: Spring 2016 GMU Logistics Today POSIX Threads Briefly Reading Grama 7.1-9 (PThreads) POSIX Threads Programming Tutorial HW4 Upcoming Post over the weekend

More information

Operating systems. Lecture 10

Operating systems. Lecture 10 Operating systems. Lecture 10 Michał Goliński 2018-12-04 Introduction Recall fork one more example allocating memory system and library calls signals Plan for today Threads pthreads Threads in the standard

More information

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed Announcement Total Score is updated Please check it Will be changed The score of PA4 will be changed! 1 Concurrent Programming Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Sanghoon Han(sanghoon.han@csl.skku.edu)

More information

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid. Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter

More information

4.8 Summary. Practice Exercises

4.8 Summary. Practice Exercises Practice Exercises 191 structures of the parent process. A new task is also created when the clone() system call is made. However, rather than copying all data structures, the new task points to the data

More information

CMPSC 311- Introduction to Systems Programming Module: Concurrency

CMPSC 311- Introduction to Systems Programming Module: Concurrency CMPSC 311- Introduction to Systems Programming Module: Concurrency Professor Patrick McDaniel Fall 2013 Sequential Programming Processing a network connection as it arrives and fulfilling the exchange

More information

CS 261 Fall Mike Lam, Professor. Threads

CS 261 Fall Mike Lam, Professor. Threads CS 261 Fall 2017 Mike Lam, Professor Threads Parallel computing Goal: concurrent or parallel computing Take advantage of multiple hardware units to solve multiple problems simultaneously Motivations: Maintain

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

Programming with Shared Memory. Nguyễn Quang Hùng

Programming with Shared Memory. Nguyễn Quang Hùng Programming with Shared Memory Nguyễn Quang Hùng Outline Introduction Shared memory multiprocessors Constructs for specifying parallelism Creating concurrent processes Threads Sharing data Creating shared

More information

TCSS 422: OPERATING SYSTEMS

TCSS 422: OPERATING SYSTEMS TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread

More information

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency, Thread. Dongkun Shin, SKKU Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point

More information

Multicore and Multiprocessor Systems: Part I

Multicore and Multiprocessor Systems: Part I Chapter 3 Multicore and Multiprocessor Systems: Part I Max Planck Institute Magdeburg Jens Saak, Scientific Computing II 44/337 Symmetric Multiprocessing Definition (Symmetric Multiprocessing (SMP)) The

More information

The Compilation Process

The Compilation Process Crash Course in C Lecture 2 Moving from Python to C: The compilation process Differences between Python and C Variable declaration and strong typing The memory model: data vs. address The Compilation Process

More information

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4 Multithreading Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4 Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003. EEL 602 1 Outline Process and Threads Multithreading

More information

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Fall 2017 1 Outline Inter-Process Communication (20) Threads

More information

Multiprocessors 2007/2008

Multiprocessors 2007/2008 Multiprocessors 2007/2008 Abstractions of parallel machines Johan Lukkien 1 Overview Problem context Abstraction Operating system support Language / middleware support 2 Parallel processing Scope: several

More information

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs independently from its main program. Multi-threaded programs

More information

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing an infinite loop Processes and Exceptions int main(void) { while (1) { /* waste CPU time */ If I run this on a lab machine, can you still use it? even if the machine only has one core? 1 2 timing nothing

More information

CS Operating Systems: Threads (SGG 4)

CS Operating Systems: Threads (SGG 4) When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information