undefined reference to `sem_init' undefined reference to `sem_post' undefined reference to `sem_wait'编译选项需要加入一个多线程
gcc -pthread -o outfile.out filename.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <semaphore.h> #include <sys/types.h> #include <sys/sem.h> #include <sys/ipc.h> #include <sys/shm.h> #include <errno.h> #include <unistd.h> #include <signal.h> #include <time.h> #define NUM_THREADS_RD 5 /*define the number of reader*/ #define NUM_THREADS_WR 5 /*define the number of writer*/ #define MAX_BUFFER 20 #define RUN_TIME 20 int reader_counter=0; /*to count the number of reader who is reading*/ int timeout=0; /*time out write_pointer*/ int buffer[MAX_BUFFER]; /*difine the buffer */ sem_t writer_mutex,reader_share,print_mutex; pthread_t threads_rd[NUM_THREADS_RD]; /*reader*/ pthread_t threads_wr[NUM_THREADS_WR]; /*writer*/ FILE* fd; void* writer_thread(void*); void* reader_thread(void*); void writing(int); void reading(int); void showbuf(); /*show the concent of buffer*/ void handler(){ timeout=1; /*set the timeout flag*/ } int main(){ int i; fd=fopen("output.txt","w"); /*open a file to save the result*/ signal(SIGALRM,handler); sem_init(&reader_share,0,1); /*set the value of semaphores*/ sem_init(&writer_mutex,0,1); sem_init(&print_mutex,0,1); for(i=0;i<MAX_BUFFER;i++) buffer[i]=0; /*initiate the buffer*/ /*create the threads*/ for(i=0;i<NUM_THREADS_WR;i++) pthread_create(&threads_wr[i],NULL,(void*)writer_thread,(void*)i); for(i=0;i<NUM_THREADS_RD;i++) pthread_create(&threads_rd[i],NULL,(void*)reader_thread,(void *)i); alarm(RUN_TIME); /*set time to run*/ /*wait the threads to exit*/ for(i=0;i<NUM_THREADS_WR;i++) pthread_join(threads_wr[i],NULL); for(i=0;i<NUM_THREADS_RD;i++) pthread_join(threads_rd[i],NULL); /*destroy the semaphores*/ sem_destroy(&reader_share); sem_destroy(&writer_mutex); sem_destroy(&print_mutex); fclose(fd); return 0; } void* writer_thread(void *tid){ while(1){ sem_wait(&writer_mutex); /*writer apply for resource*/ writing((int)tid); /*writing*/ sem_post(&writer_mutex); /*finished write,free the resource*/ if(timeout==1)break; /*time out*/ srand((int)time(NULL)*((int)tid+1)); sleep(rand()%5+1); /*wait a few seconds ,then continue writing*/ } return ((void*)0); } void* reader_thread(void *tid){ while(1){ sem_wait(&reader_share); /*reader apply for resource*/ if(reader_counter==0){ /*the first reader*/ sem_wait(&writer_mutex); /*get the read mode and block writer*/ } reader_counter++; /*add 1 reader*/ sem_post(&reader_share); reading((int)tid); /*reading*/ sem_wait(&reader_share); reader_counter--; if(reader_counter==0){ /*the last reader inform writer*/ sem_post(&writer_mutex); } sem_post(&reader_share); if(timeout==1)break; /*time out*/ srand((int)time(NULL)*((int)tid+1)); sleep(rand()%5+1); /*wait a few seconds ,then continue reading*/ } return ((void*)0); } void writing(int n){ int write_pointer; srand((int)time(NULL)); write_pointer=rand()%MAX_BUFFER;/*set the write_pointer randomly*/ buffer[write_pointer]++; sleep(1); /*one second for writing*/ printf("writer:%d pointer:%d",n+1,write_pointer); fprintf(fd,"writer:%d pointer:%d",n+1,write_pointer); showbuf(); /*show the buffer*/ } void reading(int n){ int read_pointer; sem_wait(&print_mutex); /*apply for the stdout*/ srand((int)time(NULL)*(n+1)); read_pointer=rand()%MAX_BUFFER;/*set the read_pointer randomly*/ printf("reader:%d pointer:%d",n+1,read_pointer); fprintf(fd,"reader:%d pointer:%d",n+1,read_pointer); showbuf(); /*show the buffer*/ sem_post(&print_mutex); sleep(rand()%2); sem_wait(&print_mutex); printf("After reader %d exit,there are still %d readers reading./n",n+1,reader_counter-1); fprintf(fd,"After reader %d exit,there are still %d readers reading./n",n+1,reader_counter-1); sem_post(&print_mutex); } /*show the content of buffer*/ void showbuf(){ int i; printf(" buffer:"); fprintf(fd," buffer:"); for(i=0;i<MAX_BUFFER;i++){ printf("%d ",buffer[i]); fprintf(fd,"%d ",buffer[i]); } printf("/n"); fprintf(fd,"/n"); }