lecture09-chapter05 - OperatingSystems Lecture09...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Operating Systems Lecture 09: Concurrency Anda Iamnitchi [email protected] 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
QOD part 1 #include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ main(int argc, char *argv[]) { pthread_t tidl, tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr, ’’usage: a.out <integer value>\n’’); exit(); } if (atoi(argv[1] < 0) { fprintf(stderr, ’’%d must be >= 0\n’’, atoi(argv[1])); exit(); } pthread_attr_init(&attr); /* create the threads*/ pthread_create(&tid,&attr,runner,argv[1]); pthread_create(&tidl,&attr,runner,argv[1]); pthread_join(tid,NULL); pthread_join(tidl,NULL); printf(sum = %d\n,sum); } /* The thread will begin control in this function */ void *runner(void *param) { int upper = atoi(param); int i; sum = 0; if (upper > 0) { for (i = 1;i<= upper; i++) sum += i; } pthread_exit(0); } 2
Background image of page 2
3 An(other) Example of Race Condition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Code in Shared Memory void echo() { chin = getchar(); chout = chin; putchar(chout); } Input read one keystroke at a time, sent to the display Used by each app in a multiprogramming environment Thus, makes sense to be in shared memory
Background image of page 4
Troubles? Process P1 Process P2 .. chin = getchar(); . .c h i n = getchar(); chout = chin; chout = chin; putchar(chout); . . putchar(chout); ..
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Race Condition Occurs when multiple processes or threads read and write data items The final result depends on the order of execution the “loser” of the race is the process that updates last and will determine the final value of the variable
Background image of page 6
Multiple Processes Operating System design is concerned with the management of processes and threads: Multiprogramming Multiprocessing Distributed Processing
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arises in Three Different Contexts: Multiple Applications invented to allow processing time to be shared among active applications Structured Applications extension of modular design
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 39

lecture09-chapter05 - OperatingSystems Lecture09...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online