32 progress graph for a program that can deadlock 600

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: n the previous section, we learned that semaphores can be used for both sharing and signaling. Pthreads, on the other hand, provides one set of functions (based on mutex variables) for sharing, and another set (based on condition variables) for signaling. 11.5.1 Mutex Variables A mutex is synchronization variable that is used like a binary semaphore to protect the access to shared variables. There are three basic operations defined on a mutex. 584 CHAPTER 11. CONCURRENT PROGRAMMING WITH THREADS code/threads/prodcons.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include "csapp.h" #define NITERS 5 void *producer(void *arg), *consumer(void *arg); struct { int buf; /* shared variable */ sem_t full, empty; /* semaphores */ } shared; int main() { pthread_t tid_producer, tid_consumer; /* initialize the semaphores */ Sem_init(&shared.empty, 0, 1); Sem_init(&shared.full, 0, 0); /* create threads and wait for them to finish */ Pthread_create(&tid_producer, N...
View Full Document

Ask a homework question - tutors are online