lec10-synchronization - Lecture 10: Synchronization...

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

View Full Document Right Arrow Icon
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 10: Synchronization (Chapter 6)
Background image of page 1

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

View Full DocumentRight Arrow Icon
6.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Name the Problem: #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); }
Background image of page 2
6.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Producer-Consumer Problem Paradigm for cooperating processes producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size
Background image of page 3

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

View Full DocumentRight Arrow Icon
6.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Bounded-Buffer: Shared-Memory Solution Shared data #define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; //next free position in the buffer int out = 0; //next full position in the buffer
Background image of page 4
6.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Bounded-Buffer Approx Solution PRODUCER: while (true) {    /* Produce an item in  nextProduced */         while ((in + 1) %  BUFFER_SIZE== out)      ;   /* do nothing -- no free  buffers */     buffer[in] =  nextProduced ;     in = (in + 1) % BUFFER_SIZE;      } CONSUMER: while (true) {           while (in == out)                  ; // do nothing  -- nothing to consume
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

lec10-synchronization - Lecture 10: Synchronization...

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

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