gpt4 book ai didi

c - C 多线程消费者-生产者程序中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:49 25 4
gpt4 key购买 nike

我目前正在学习线程程序的概念,并被分配使用线程和信号量模拟股票市场。这是代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <pthread.h>

#define NUM_WRITERS 5
#define NUM_READERS 5
#define STOCKLIST_SIZE 10

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t stop_writers;

typedef struct
{ int readers;
int slots[STOCKLIST_SIZE];
} mem_structure;

int id[NUM_READERS + NUM_WRITERS];
mem_structure *stocklist;
pthread_t thr[NUM_WRITERS + NUM_READERS];

void init(){
sem_init(&stop_writers, 0, 1);
}

void cleanup(int signo) // clean up resources by pressing Ctrl-C
{ sem_destroy(&stop_writers);
printf("Closing...\n");
exit(0);
}


void write_stock(int n_writer)
{
int stock = (int)(rand()%STOCKLIST_SIZE);
int stock_value = 1 + (int)(100.0 * rand()/(RAND_MAX + 1.0));
stocklist->slots[stock] = stock_value;
fprintf(stderr, "Stock %d updated by BROKER %d to %d\n", stock, n_writer, stock_value);
}

void* writer(void* id){
int my_id = *((int*) id);
int i = my_id;

while (1)
{
pthread_mutex_lock(&mutex);
sem_wait(&stop_writers);
write_stock(i);
pthread_mutex_unlock(&mutex);
sem_post(&stop_writers);

sleep(1);
++i;
}

}

void read_stock(int pos, int n_reader){
fprintf(stderr, "Stock %d read by client %d = %d.\n", pos, n_reader, stocklist->slots[pos]);
}

void* reader(void* id){
int my_id = *((int*) id);
int i = my_id;

while (1)
{ sem_wait(&stop_writers);
read_stock((int)(rand()%STOCKLIST_SIZE), i);
sem_post(&stop_writers);
sleep(1 + (int) (3.0 * rand() / (RAND_MAX + 1.0)));
++i;
}
}

void monitor() // main process monitors the reception of Ctrl-C
{
struct sigaction act;
act.sa_handler = cleanup;
act.sa_flags = 0;
if ((sigemptyset(&act.sa_mask) == -1) ||
(sigaction(SIGINT, &act, NULL) == -1))
perror("Failed to set SIGINT to handle Ctrl-C");
while(1){
sleep(5);
printf("Still working...\n");
}
exit(0);
}


int main(void)
{ int i, j;
init();

for (i = 0; i < NUM_WRITERS; ++i){
id[i] = i;
pthread_create(&thr[i], NULL, writer, &id[i]);}
for (j = i; j < NUM_READERS; ++j){
id[j] = j;
pthread_create(&thr[j], NULL, reader, &id[j]);}

stocklist->readers = 0;
pthread_exit(NULL);

monitor();
return 0;
}

这几乎是在 main() 开始时给我一个段错误,并创建了一个线程...因为它不是我从 root 创建的代码,所以我无法回溯错误并且修复它 - 如果有人可以看一下并给我一些提示,那就太好了

谢谢!

编辑

解决了问题,感谢您的帮助:)

我在 init() 上初始化了库存 list ,

void init(){
sem_init(&stop_writers, 0, 1);
stocklist = (mem_structure*)malloc(sizeof(mem_structure));
}

并修改了main()中的第二个cicle,

for (j = i; j < NUM_READERS+NUM_WRITERS; ++j){
id[j] = j;
pthread_create(&thr[j], NULL, reader, &id[j]);}

谢谢

最佳答案

以下是我为找出问题所做的工作:

gcc -g program.c -lpthread

gdb a.out

运行

输出:

程序收到信号 SIGSEGV,段错误。
[切换到线程 0xb7feeb70 (LWP 23060)]
program.c:44 处的 write_stock (n_writer=0) 中的 0x08048834
44 stocklist->slots[库存] = stock_value;

看起来问题出在这里:

stocklist->slots[stock] = stock_value;

关于c - C 多线程消费者-生产者程序中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com