Multiple ethernet segments can be connected into

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: dwait. Meanwhile, after the main thread creates the peer thread, it waits up to one second for the peer thread to terminate. If pthread cond timedwait does not time out, then the main thread knows that the peer thread has terminated, so it prints “Whew!” and exits. Otherwise, it beeps and waits for another second. This continues until it has waited a total of 5 seconds, at which point the loop terminates, the main thread explodes by printing “Boom!”, and then exits. 11.5. SYNCHRONIZING THREADS WITH MUTEX AND CONDITION VARIABLES 591 code/threads/timebomb.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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #include "csapp.h" #define TIMEOUT 5 void *thread(void *vargp); struct timespec *maketimeout(struct timespec *tp, int secs); pthread_cond_t cond; pthread_mutex_t mutex; pthread_t tid; int main() { int i, rc; struct timespec timeout; Pthread_cond_init(&cond, NULL); Pthread_mutex_init(&mutex, NULL); Pthread_mutex_lock(&mutex); Pthread_create(&tid, NULL, thread, NULL); for (i=0; i<TIMEOUT; i++) { printf("BEEP\n&quot...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online