/* linux.c * * This is a N-paired program. Each consumer takes message from the buffer in which * the corresponding producer puts data. Each pair is independent of the others. * A producer is randomly picked to read from stdin. If 0 is typed as stdin, an * arithmetic exception happens. Core file is expected to dump at this point. * Mutex is used to synchronize the communication between producer and consumer. * To guarantee each message is consumed before producer puts in new one, condition * variables are used. * * command line: linux 1000000 * The second argument is the time (microseconds) the producer sleeps aftet each * iteration. * * Written by Yahui Lin */ #include #include #include #include #include #include #include #include #include #define NUMPAIRS 15 int buffer[NUMPAIRS]; int seg_fault; int p_sleep; pthread_mutex_t prod_mutex[NUMPAIRS], cons_mutex[NUMPAIRS]; pthread_t prod_id[NUMPAIRS], cons_id[NUMPAIRS]; pthread_cond_t prod_cond[NUMPAIRS], cons_cond[NUMPAIRS]; void *producer(void *arg); void *consumer(void *arg); void sighandler(int sig) { if( sig != SIGFPE ) fprintf(stderr, "unexpected signal in handler: %d\n", sig); else abort(); } int main(int argc, char *argv[]) { int i; struct sigaction act; sigset_t mask_sig; if( argc != 2 ) { fprintf(stderr, "usage: %s prod_sleep_time\n", argv[0]); exit(1); } if( (p_sleep = atoi(argv[1])) < 0 ) p_sleep = 0; sigemptyset(&mask_sig); sigaddset(&mask_sig, SIGFPE); act.sa_handler = sighandler; act.sa_mask = mask_sig; act.sa_flags = 0; if(sigaction(SIGFPE, &act, NULL) == -1){ perror("sigaction error"); exit(1); } for(i=0; i