我正在从一个文件中读取然后将其写在共享内存上。但是Writer在共享内存中写入工作正常但读者不是从共享内存中读取。 maxlen [x]存储了一条线的最大长度。
void * reader (void * param){
int i;
int count=1;
int waittime;
waittime = rand() % 5;
sleep(waittime);
shm_ptr = shmat(shm_id, NULL, 0);
if ((int)shm_ptr == -1){
printf("*** shmat error (server) ***\n");
exit(1);
}
sleep(1);
printf("\nReader is trying to enter");
sem_wait(&mutex);
readcount++;
if(readcount==1)
sem_wait(&wrt);
printf("\n%d Reader is inside ",readcount);
printf(" in crtical section \n");
int sizes=x;
x=0;
while (x<=sizes) {
shm_ptr[x] = malloc( sizeof(char *)* maxlen[x]);
puts(shm_ptr[x]);
x++;
}
sem_post(&mutex);
sem_wait(&mutex);
readcount--;
if(readcount==0)
sem_post(&wrt);
sem_post(&mutex);
printf("\nReader is Leaving");
sleep(waittime);
}