C 生产者和消费者细分错误
我是并发编程的初学者,我想实现一个消费者和生产者。我想在3秒钟后从生成器发送两个整数,并打印生成器的数字总和 运行代码后,我有分段错误:(。 谢谢你的帮助:D 这是我的制片人代码:C 生产者和消费者细分错误,c,producer-consumer,C,Producer Consumer,我是并发编程的初学者,我想实现一个消费者和生产者。我想在3秒钟后从生成器发送两个整数,并打印生成器的数字总和 运行代码后,我有分段错误:(。 谢谢你的帮助:D 这是我的制片人代码: #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <string.h> #include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
int vector[VECTOR_SIZE];
};
int main() {
int msqid;
// int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
int i;
key = ftok(".", 'g');
//Get the message queue ID for the given key
if ((msqid = msgget(key, IPC_CREAT | 0666 )) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
//Message Type
sbuf.mtype = 1;
while(1){
for(i = 0; i < 2; i++){
printf("Enter a message to add to message queue : ");
scanf("%d",sbuf.vector[i]);
buflen = strlen(sbuf.vector[i]) + 1 ;
}
sleep(3);
}
// getchar();
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %ld, %d, %d\n", msqid, sbuf.mtype, sbuf.vector[0], sbuf.vector[1], (int)buflen);
perror("Could not send message!\n");
exit(1);
}
else
printf("Message Sent\n");
exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义向量大小2
结构msgbuf
{
长型;
int向量[向量大小];
};
int main(){
int msqid;
//int msgflg=IPC|U CREAT|0666;
钥匙(t)钥匙;;
结构msgbuf-sbuf;
布弗伦尺寸;
int i;
键=ftok(“.”,“g”);
//获取给定密钥的消息队列ID
如果((msqid=msgget(键,IPC|u CREAT | 0666))<0){
perror(“无法获取消息队列\n”);
出口(1);
}
printf(“MSQID值:%d\n”,MSQID);
//消息类型
sbuf.mtype=1;
而(1){
对于(i=0;i<2;i++){
printf(“输入要添加到消息队列的消息:”);
scanf(“%d”,sbuf.vector[i]);
buflen=strlen(sbuf.vector[i])+1;
}
睡眠(3);
}
//getchar();
如果(msgsnd(msqid和sbuf、buflen、IPC_NOWAIT)<0)
{
printf(“%d,%ld,%d,%d\n”,msqid,sbuf.mtype,sbuf.vector[0],sbuf.vector[1],(int)buflen);
perror(“无法发送邮件!\n”);
出口(1);
}
其他的
printf(“已发送的消息”);
出口(0);
}
我这里有消费者代码:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
#define VECTOR_SIZE 2
struct msgbuf
{
long mtype;
char vector[VECTOR_SIZE];
};
int main() {
int msqid;
key_t key;
struct msgbuf rcvbuffer;
// key = 1234;
key = ftok(".", 'g');
if ((msqid = msgget(key, 0666)) < 0) {
perror("Could not get message queue\n");
exit(1);
}
printf("MSQID value: %d\n", msqid);
// Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) {
perror("Could not receive message!\n");
exit(1);
}
printf("%d\n", rcvbuffer.vector[0] + rcvbuffer.vector[1]);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义MAXSIZE 128
#定义向量大小2
结构msgbuf
{
长型;
字符向量[向量大小];
};
int main(){
int msqid;
钥匙(t)钥匙;;
结构msgbuf rcvbuffer;
//密钥=1234;
键=ftok(“.”,“g”);
如果((msqid=msgget(键,0666))<0){
perror(“无法获取消息队列\n”);
出口(1);
}
printf(“MSQID值:%d\n”,MSQID);
//接收消息类型为1的应答。
if(msgrcv(msqid和rcvbuffer,MAXSIZE,1,0)<0){
perror(“无法接收消息!\n”);
出口(1);
}
printf(“%d\n”,rcvbuffer.vector[0]+rcvbuffer.vector[1]);
返回0;
}
在producer中,您在整数上使用strlen
,即buflen=strlen(sbuf.vector[i])+1;
在scanf()
中,它应该是scanf(“%d”)和(sbuf.vector[i]);
。sbuf vector[i]的地址
并且生产者的第二个最后一个printf也包含比格式预期的更多的参数。4预期5给定。