lec10-synchronization

lec10-synchronization - Silberschatz, Galvin and Gagne 2009...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Silberschatz, Galvin and Gagne 2009 Operating System Concepts 8 th Edition, Lecture 10: Synchronization (Chapter 6) 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); } 6.3 Silberschatz, Galvin and Gagne 2009 Operating System Concepts 8 th Edition Producer-Consumer Problem Paradigm for cooperating processes z producer process produces information that is consumed by a consumer process z unbounded-buffer places no practical limit on the size of the buffer z bounded-buffer assumes that there is a fixed buffer size 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 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 // remove an item from the buffer nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE;...
View Full Document

Page1 / 22

lec10-synchronization - Silberschatz, Galvin and Gagne 2009...

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