|
| 1 | +// These examples build on/mimic the multi-process ping pong program from |
| 2 | +// lecture 18 and the select video. The challenge this time is to synchronize |
| 3 | +// two pthreads who need to alternate printing out pings and pongs to the console. |
| 4 | +// |
| 5 | +// The main program creates four threads (2 pings and 2 pongs). The threads are |
| 6 | +// identical except for the messages they print and whether they print on even |
| 7 | +// or odd messages. We add an explicit usleep to trigger the race condition. |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <pthread.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +// How many total pings and pongs we'll print |
| 17 | +#define TOTAL_MSGS 100 |
| 18 | + |
| 19 | +// Structure that we'll use to transmit information |
| 20 | +// to the threads. |
| 21 | + |
| 22 | +struct pp_thread_info { |
| 23 | + int *msgcount; // Keeps track of total number of messages; shared state |
| 24 | + char *msg; |
| 25 | + int modval; |
| 26 | + pthread_mutex_t *mutex; // Sync access to msgcount |
| 27 | + pthread_cond_t *cv; // Condition variable to use with mutex |
| 28 | +}; |
| 29 | + |
| 30 | +// Unsynchronized version -- pings and pongs will not alternate nicely |
| 31 | + |
| 32 | +// Thread function for both pings and pongs; the arg function will be |
| 33 | +// pointer to a struct pp_thread_info and carries all the information |
| 34 | +// that the thread needs to process pings and pongs. |
| 35 | + |
| 36 | +void * |
| 37 | +pp_thread(void *arg) |
| 38 | +{ |
| 39 | + struct pp_thread_info *infop; |
| 40 | + int c; |
| 41 | + |
| 42 | + infop = arg; |
| 43 | + while (1) { |
| 44 | + pthread_mutex_lock(infop->mutex); |
| 45 | + if (*infop->msgcount >= TOTAL_MSGS) |
| 46 | + break; |
| 47 | + while (*infop->msgcount % 2 == infop->modval) |
| 48 | + pthread_cond_wait(infop->cv, infop->mutex); |
| 49 | + |
| 50 | + // When we get here, we know that it's time to |
| 51 | + // print our message and that we are holding the |
| 52 | + // mutex. |
| 53 | + printf("%s\n", infop->msg); |
| 54 | + c = *infop->msgcount; |
| 55 | + sched_yield(); |
| 56 | + c = c + 1; |
| 57 | + *infop->msgcount = c; |
| 58 | + |
| 59 | + pthread_cond_broadcast(infop->cv); |
| 60 | + pthread_mutex_unlock(infop->mutex); |
| 61 | + sched_yield(); |
| 62 | + } |
| 63 | + pthread_mutex_unlock(infop->mutex); |
| 64 | + return (NULL); |
| 65 | +} |
| 66 | + |
| 67 | +int |
| 68 | +main(void) |
| 69 | +{ |
| 70 | + pthread_t ping_id1, pong_id1; |
| 71 | + pthread_t ping_id2, pong_id2; |
| 72 | + pthread_mutex_t mutex; |
| 73 | + pthread_cond_t cv; |
| 74 | + struct pp_thread_info ping, pong; |
| 75 | + int msgcount; |
| 76 | + |
| 77 | + msgcount = 0; |
| 78 | + |
| 79 | + if (pthread_mutex_init(&mutex, NULL) != 0) { |
| 80 | + fprintf(stderr, "Mutex init failed: %s\n", strerror(errno)); |
| 81 | + exit(1); |
| 82 | + } |
| 83 | + if (pthread_cond_init(&cv, NULL) != 0) { |
| 84 | + fprintf(stderr, "CV init failed: %s\n", strerror(errno)); |
| 85 | + exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + ping.msgcount = &msgcount; |
| 89 | + ping.msg = "ping"; |
| 90 | + ping.modval = 0; |
| 91 | + ping.mutex = &mutex; |
| 92 | + ping.cv = &cv; |
| 93 | + |
| 94 | + pong.msgcount = &msgcount; |
| 95 | + pong.msg = "pong"; |
| 96 | + pong.modval = 1; |
| 97 | + pong.mutex = &mutex; |
| 98 | + pong.cv = &cv; |
| 99 | + |
| 100 | + // Create the two threads |
| 101 | + if ((pthread_create(&ping_id1, NULL, &pp_thread, &ping) != 0) || |
| 102 | + (pthread_create(&pong_id1, NULL, &pp_thread, &pong) != 0) || |
| 103 | + (pthread_create(&ping_id2, NULL, &pp_thread, &ping) != 0) || |
| 104 | + (pthread_create(&pong_id2, NULL, &pp_thread, &pong) != 0)) { |
| 105 | + fprintf(stderr, "pingpong: pthread_create failed %s\n", strerror(errno)); |
| 106 | + exit(1); |
| 107 | + } |
| 108 | + |
| 109 | + // Now wait for threads to exit (Probably should check for errors) |
| 110 | + pthread_join(ping_id1, NULL); |
| 111 | + pthread_join(pong_id1, NULL); |
| 112 | + pthread_join(ping_id2, NULL); |
| 113 | + pthread_join(pong_id2, NULL); |
| 114 | + pthread_mutex_destroy(&mutex); |
| 115 | + pthread_cond_destroy(&cv); |
| 116 | + |
| 117 | + printf("Main thread exiting\n"); |
| 118 | +} |
| 119 | + |
0 commit comments