gpt4 book ai didi

消费者-生产者问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:12 30 4
gpt4 key购买 nike

我的消费者、生产者程序有问题,它似乎可以加载,但返回段错误。我已经尝试了一切来修复它,但仍然失败!将不胜感激任何帮助。笔记;代码真的很多,semaphore.h的代码都在里面,有谁想测试一下。其余代码与此处相同。我在 Unix 机器上运行它。

      //----------------SEMOPHORE.H--------------------------
/*********************** semaphore.h ****************************/
#define SEM_NAME "semaphore.h",'a'
#define SEM_MAX 3
#define FREE 0
#define DATA 1
#define ROOM 2
#define S_WAIT -1
#define S_SIGNAL 1
#define NO_EVENT -1

int sem_config(int, int);
int sem_wait(int, int);
int sem_signal(int, int);

//----------------Producer-----------------------
#include<stdio.h>
#include"semaphore.h"
#include"shbuf.h"
# include <string.h>
# include "shbuf.h"

void myadd();

main()
{
sem_config(DATA, FREE);
myadd();
sem_signal(DATA,S_SIGNAL);
return 0;
}
void myadd()
{
char str[80];
char discard;
printf("you can type a message for the producer."\n);
printf("Producing message : ");
scanf("%s%c", str, &discard);
q_add(strlen(str) , str);
}
//---------------------------CONSUMER---------------------------
#include<stdio.h>
#include"semaphore.h"
#include"shbuf.h"

# include <string.h>
# include "shbuf.h"

void myremove();

main()
{
sem_wait(DATA, S_WAIT);
myremove();
sem_signal(DATA,S_SIGNAL);

return 0;
}

void myremove()
{
char str[80];
if(q_items() > 0)
{
q_remove(1 , str);
str[1] = 0;
printf("%s\n", str);
}
}
//-------------------------------SEMAPHORE.C--------------------------------------
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "semaphore.h"

static int sem_id;

static union semun
{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
ushort array[1]; /* array for GETALL & SETALL, used in setting or retrieving all semaphorevalues in a set.*/
} sem_attr;

static struct sembuf asem[1];
static key_t s_key; //key for shared memory

int sem_config(int event, int init_val)
{
int x;
s_key = ftok(SEM_NAME); //key for semaphore
//create a semaphore for process synchronization
if ( -1 == (sem_id = semget(s_key, SEM_MAX, 0666|IPC_CREAT ) ))
{
perror("semget");
return -1;
}
if ( event == NO_EVENT )
return 0; /*locate semaphore only*/
//set initial value of semaphore
sem_attr.val = init_val;
if ( -1 == semctl(sem_id, event, SETVAL, sem_attr ) )
{
perror("semctl SETVAL");
return -1;
}
if ( -1 == ( x = semctl(sem_id, event, GETVAL, sem_attr ) ) )
{
perror("semctl GETVAL");
return -1;
}
assert( x == init_val );
return 0;
}

int sem_wait(int event, int nwaits)
{
asem[0].sem_num = event;
asem[0].sem_op = nwaits * S_WAIT;
asem[0].sem_flg = 0;
if ( event == NO_EVENT ) /*remove semaphore set*/
if ( -1 == semctl(sem_id, 0, IPC_RMID, sem_attr ) )
{
perror("semctl IPC_RMID");
return -1;
}
else
return 0;
if ( -1 == semop(sem_id, asem, 1 ) )
{
perror("semop");
return -1;
}
return 0;
}

int sem_signal(int event, int nsignals)
{
asem[0].sem_num = event;
asem[0].sem_op = nsignals * S_SIGNAL;
asem[0].sem_flg = 0;
if ( event == NO_EVENT )
if ( -1 == semctl(sem_id, 0, IPC_RMID, sem_attr ) )
{
perror("semctl IPC_RMID");
return -1;
}
else
return 0;
if ( -1 == semop(sem_id, asem, 1 ) )
{
perror("semop");
return -1;
}
return 0;
}

最佳答案

通常,调试段错误的最佳方法是使用 -g 标志进行编译并在调试器(如 gdb)中运行。它会告诉您崩溃的确切位置,并让您打印出变量的值来告诉您原因。

关于消费者-生产者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3540700/

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