Operating System Three easy pieces Remzi H ArpaciDusseau

  • Slides: 12
Download presentation
Operating System : Three easy pieces Remzi H. Arpaci-Dusseau Andrea C. Arpaci-Dusseau Chapter 26

Operating System : Three easy pieces Remzi H. Arpaci-Dusseau Andrea C. Arpaci-Dusseau Chapter 26 Concurrency: An Introduction Jeongsoo Shin (jeongsoo. shin@snu. ac. kr) School of Computer Science and Engineering Seoul National University

Outline § Thread § Address Spaces § Thread Creation § Race Condition § Atomicity

Outline § Thread § Address Spaces § Thread Creation § Race Condition § Atomicity § Key Concurrency Terms § Summary Operating System : Three easy pieces 2

Thread § Shares the address space and resources of its process § Has a

Thread § Shares the address space and resources of its process § Has a stack for local variables § Has an execution state (running, ready, etc. ) § Saves the thread context when not running § Stack Pointer, PC, State, Register Operating System : Three easy pieces 3

Address Spaces Stack 1 Thread 1 (free) Stack 2 Thread 2 (free) Heap Data

Address Spaces Stack 1 Thread 1 (free) Stack 2 Thread 2 (free) Heap Data + BSS Text Single Thread Multiple Threads Operating System : Three easy pieces 4

Thread Creation (1/3) § Thread Trace (1) 1 2 3 4 5 6 7

Thread Creation (1/3) § Thread Trace (1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio. h> #include <assert. h> #include <pthread. h> void *mythread(void *arg) { printf("%sn", (char *) arg); return NULL; } [Output] main: begin A B main: end int main(int argc, char *argv[]) { pthread_t p 1, p 2; int rc; printf("main: beginn"); rc = pthread_create(&p 1, NULL, mythread, "A"); rc = pthread_create(&p 2, NULL, mythread, "B"); // join waits for the threads to finish rc = pthread_join(p 1, NULL); rc = pthread_join(p 2, NULL); printf("main: endn"); return 0; } Operating System : Three easy pieces 5

Thread Creation (2/3) § Thread Trace (2) 1 2 3 4 5 6 7

Thread Creation (2/3) § Thread Trace (2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio. h> #include <assert. h> #include <pthread. h> void *mythread(void *arg) { printf("%sn", (char *) arg); return NULL; } [Output] main: begin A B main: end int main(int argc, char *argv[]) { pthread_t p 1, p 2; int rc; printf("main: beginn"); rc = pthread_create(&p 1, NULL, mythread, "A"); rc = pthread_create(&p 2, NULL, mythread, "B"); // join waits for the threads to finish rc = pthread_join(p 1, NULL); rc = pthread_join(p 2, NULL); printf("main: endn"); return 0; } Operating System : Three easy pieces 6

Thread Creation (3/3) § Thread Trace (3) 1 2 3 4 5 6 7

Thread Creation (3/3) § Thread Trace (3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio. h> #include <assert. h> #include <pthread. h> void *mythread(void *arg) { printf("%sn", (char *) arg); return NULL; } [Output] main: begin B A main: end int main(int argc, char *argv[]) { pthread_t p 1, p 2; int rc; printf("main: beginn"); rc = pthread_create(&p 1, NULL, mythread, "A"); rc = pthread_create(&p 2, NULL, mythread, "B"); // join waits for the threads to finish rc = pthread_join(p 1, NULL); rc = pthread_join(p 2, NULL); printf("main: endn"); return 0; } Operating System : Three easy pieces 7

Race Condition (1/2) § Two threads : T 1, T 2 § One global

Race Condition (1/2) § Two threads : T 1, T 2 § One global variable : counter (0 x 8049 a 1 c) Stack 1 § Scenario T 1 § Two threads are running the same code (free) § Each thread adds 1 to the counter Stack 2 T 2 counter = counter + 1 (free) Heap mov 0 x 8049 a 1 c, %eax add $0 x 1, %eax mov %eax, 0 x 8049 a 1 c counter Data + BSS Text 0 x 0000000 Operating System : Three easy pieces 8

Race Condition (2/2) Operating System : Three easy pieces 9

Race Condition (2/2) Operating System : Three easy pieces 9

Atomicity § Solution 1 § Using a super instruction mov 0 x 8049 a

Atomicity § Solution 1 § Using a super instruction mov 0 x 8049 a 1 c, %eax add $0 x 1, %eax mov %eax, 0 x 8049 a 1 c memory-add 0 x 8049 a 1 c, $0 x 1 § Solution 2 § Using an atomic test-and-set instruction Operating System : Three easy pieces 10

Key Concurrency Terms § Critical Section § A piece of code that accesses a

Key Concurrency Terms § Critical Section § A piece of code that accesses a shared resource § Race Condition § A situation where the output is dependent on the sequence or timing of other uncontrollable events § Mutual Exclusion § The requirement of ensuring that no two concurrent processes are in their critical section at the same time Operating System : Three easy pieces 11

Summary § Threads share the same address space § Race condition occurs when two

Summary § Threads share the same address space § Race condition occurs when two or more threads can access shared data § Synchronization primitives can resolve the race condition Operating System : Three easy pieces 12