当前位置: 技术问答>linux和unix
用信号量同步出错,请教高手。。。
来源: 互联网 发布时间:2016-09-04
本文导语: 本帖最后由 xiaowai0219 于 2010-06-15 20:14:34 编辑 #include #include #include #include #include #include #include #include #define NUM_THREADS_RD 4 /*define the number of reader*/ #define NUM_THREADS_WR 3 /*define the number of writer*/ #define MAX_BUFFER 20 int ...
#include
#include
#include
#include
#include
#include
#include
#include
#define NUM_THREADS_RD 4 /*define the number of reader*/
#define NUM_THREADS_WR 3 /*define the number of writer*/
#define MAX_BUFFER 20
int reader_counter=0; /*to count the number of reader who is reading*/
int semrdwr;
int semarray[2]; /*0 for read and 1 for write*/
int buffer[MAX_BUFFER],flag=0; /*difine the buffer and pointer*/
pthread_t threads_rd[NUM_THREADS_RD]; /*reader*/
pthread_t threads_wr[NUM_THREADS_WR]; /*writer*/
void* writebuf(void); /*writer writer the buffer*/
void* readbuf(void); /*reader read the buffer*/
void showbuf(); /*show the concent of buffer*/
void sem_wait(int semid,int semno); /*wait operation*/
void sem_signal(int semid,int semno); /*signal operation*/
int main(){
key_t semkey;
int i;
if((semkey=ftok("/etc/profile",1))