Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在消息队列linux c中发送不同类型的消息_C_Linux_Message Queue - Fatal编程技术网

在消息队列linux c中发送不同类型的消息

在消息队列linux c中发送不同类型的消息,c,linux,message-queue,C,Linux,Message Queue,我有这样一个代码: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAXLINE 1024 struct my_msgbuf { long mtype; char mtext[

我有这样一个代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAXLINE 1024

struct my_msgbuf {
        long mtype;
        char mtext[MAXLINE];
};
int main(void)
{
    struct my_msgbuf buf;
    int msqid;
    key_t key;

    if ((key = ftok("client.c", 'B')) == -1) {
        perror("ftok");
        exit(1);
    }

    if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
        perror("msgget");
        exit(1);
    }

    printf("Write a text:\n");

    buf.mtype = 1; 
    while( fgets(buf.mtext, MAXLINE, stdin) != NULL ) {
        if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0) == -1)
            perror("msgsnd");
    }

    if (msgctl(msqid, IPC_RMID, NULL) == -1) {
        perror("msgctl");
        exit(1);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义MAXLINE 1024
结构my_msgbuf{
长型;
字符多行文字[MAXLINE];
};
内部主(空)
{
结构my_msgbuf buf;
int msqid;
钥匙(t)钥匙;;
如果((key=ftok(“client.c”,“B”))=-1){
佩罗尔(“ftok”);
出口(1);
}
如果((msqid=msgget(键0644 | IPC_CREAT))=-1){
佩罗尔(“msgget”);
出口(1);
}
printf(“编写文本:\n”);
buf.mtype=1;
while(fgets(buf.mtext、MAXLINE、stdin)!=NULL){
if(msgsnd(msqid,(struct msgbuf*)&buf,sizeof(buf),0)=-1)
perror(“msgsnd”);
}
if(msgctl(msqid,IPC_RMID,NULL)=-1){
perror(“msgctl”);
出口(1);
}
返回0;
}
此代码在linux(c)中使用ipc消息队列发送消息,但它发送的消息类型等于“1”。我必须发送消息,但每条消息都必须有不同的类型。我删除了“while”,只留下fgets。没有“while”工作的FGET将成为新的生产线或达到MAXLINE。它不起作用。我想达到这样的效果:

写一个类型:
1(我)
写一段文字:
第一条消息(我)
写一个类型:
2(me)
写一段文字:
第二条消息(我)

编译并运行程序后,我达到:

写一个类型:
1(我)
写一段文字:
写一个类型:
2(me)
写一段文字:
写一个类型:

这是我修改后的代码。怎么了

#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAXLINE 1024

struct my_msgbuf {
        long mtype;
        char mtext[MAXLINE];
};

int main(void)
{
    struct my_msgbuf buf;

    int msqid;
    key_t key;

    if ((key = ftok("client.c", 'B')) == -1) {
        perror("ftok");
        exit(1);
    }

    if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
        perror("msgget");
        exit(1);
    }
while(1){
        printf("Write a type:\n");
        scanf("%ld", &buf.mtype);
        printf("Write a text:\n");
        fgets(buf.mtext, MAXLINE, stdin);

        if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf), 0) == -1){
            perror("msgsnd");
        }
}

    if (msgctl(msqid, IPC_RMID, NULL) == -1) {
        perror("msgctl");
        exit(1);
    }

    return 0;
}```









#包括
#包括
#包括
#包括
#包括
#定义MAXLINE 1024
结构my_msgbuf{
长型;
字符多行文字[MAXLINE];
};
内部主(空)
{
结构my_msgbuf buf;
int msqid;
钥匙(t)钥匙;;
如果((key=ftok(“client.c”,“B”))=-1){
佩罗尔(“ftok”);
出口(1);
}
如果((msqid=msgget(键0644 | IPC_CREAT))=-1){
佩罗尔(“msgget”);
出口(1);
}
而(1){
printf(“写入类型:\n”);
scanf(“%ld”和&buf.mtype);
printf(“编写文本:\n”);
fgets(buf.mtext、MAXLINE、stdin);
if(msgsnd(msqid,(struct msgbuf*)&buf,sizeof(buf),0)=-1){
perror(“msgsnd”);
}
}
if(msgctl(msqid,IPC_RMID,NULL)=-1){
perror(“msgctl”);
出口(1);
}
返回0;
}```