在 c 中使用 posix 信号量的 reader-writer 应用程序中的分段错误
Segmentation fault in reader-writer appilication using posix semaphore in c
在此代码中,编写器递增数据变量并reader打印此数据变量。任何时候只有一个写入器可以写入,但是当写入器不写入数据时,所有 reader 都可以打印数据。
附带的代码 运行 有时会成功,但有时会出现分段错误。
Reader-作者代码
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<unistd.h>
int reader_count;
int data = 10; /* data on which writer write update & reader read */
/* sem_write: to isolate reader & writer thread */
sem_t sem_write;
/* sem_read: to increment reader counter & sem_write */
sem_t sem_read;
void *writer(void *arg);
void *reader(void *arg);
int main(void)
{
pthread_t t[10];
int i = 10;
if (sem_init(&sem_read, 0, 1))
exit(1);
if (sem_init(&sem_write, 0, 1))
exit(1);
/* 5 writer create */
while (i != 5)
pthread_create(&t[i--], NULL, writer, NULL);
/* 5 reader create */
while (i)
pthread_create(&t[i--], NULL, reader, NULL);
/* wait for all threads to completed */
while (i != 10)
pthread_join(t[i++], NULL);
sem_destroy(&sem_read);
sem_destroy(&sem_write);
return 0;
}
void *writer(void *arg)
{
sem_wait(&sem_write);
sleep(1); /* writing work */
data++;
sem_post(&sem_write);
pthread_exit(NULL);
}
void *reader(void *arg)
{
sem_wait(&sem_read);
reader_count++;
if (reader_count == 1)
sem_wait(&sem_write);
sem_post(&sem_read);
sleep(1); /* reading work */
printf("Data: %d\n", data);
sem_wait(&sem_read);
reader_count--;
if (!reader_count)
sem_post(&sem_write);
sem_post(&sem_read);
pthread_exit(NULL);
}
您在访问 t[10]
时越界了
int i = 10;
....
while (i != 5)
pthread_create(&t[i--], NULL, writer, NULL);
在此代码中,编写器递增数据变量并reader打印此数据变量。任何时候只有一个写入器可以写入,但是当写入器不写入数据时,所有 reader 都可以打印数据。
附带的代码 运行 有时会成功,但有时会出现分段错误。
Reader-作者代码
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<unistd.h>
int reader_count;
int data = 10; /* data on which writer write update & reader read */
/* sem_write: to isolate reader & writer thread */
sem_t sem_write;
/* sem_read: to increment reader counter & sem_write */
sem_t sem_read;
void *writer(void *arg);
void *reader(void *arg);
int main(void)
{
pthread_t t[10];
int i = 10;
if (sem_init(&sem_read, 0, 1))
exit(1);
if (sem_init(&sem_write, 0, 1))
exit(1);
/* 5 writer create */
while (i != 5)
pthread_create(&t[i--], NULL, writer, NULL);
/* 5 reader create */
while (i)
pthread_create(&t[i--], NULL, reader, NULL);
/* wait for all threads to completed */
while (i != 10)
pthread_join(t[i++], NULL);
sem_destroy(&sem_read);
sem_destroy(&sem_write);
return 0;
}
void *writer(void *arg)
{
sem_wait(&sem_write);
sleep(1); /* writing work */
data++;
sem_post(&sem_write);
pthread_exit(NULL);
}
void *reader(void *arg)
{
sem_wait(&sem_read);
reader_count++;
if (reader_count == 1)
sem_wait(&sem_write);
sem_post(&sem_read);
sleep(1); /* reading work */
printf("Data: %d\n", data);
sem_wait(&sem_read);
reader_count--;
if (!reader_count)
sem_post(&sem_write);
sem_post(&sem_read);
pthread_exit(NULL);
}
您在访问 t[10]
int i = 10;
....
while (i != 5)
pthread_create(&t[i--], NULL, writer, NULL);