我知道有类似的问题已被问过,但我也相信我的问题略有不同。我正在学习课堂作业的消息队列,教授建议我们使用本教程:http://beej.us/guide/bgipc/html/multi/mq.html。它非常有用,并且有一个标题为kirk和spock的发送器和接收器代码示例。
kirk.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) {
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Enter lines of text, ^D to quit:\n");
buf.mtype = 1; /* we don't really care in this case */
while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
int len = strlen(buf.mtext);
/* ditch newline at end, if it exists */
if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */
perror("msgsnd");
}
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}
return 0;}
spock.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;
if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */
perror("ftok");
exit(1);
}
if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
exit(1);
}
printf("spock: ready to receive messages, captain.\n");
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof buf.mtext, 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("spock: \"%s\"\n", buf.mtext);
}
return 0;
}
当我最初编译此代码时,我遇到了错误: msgget:执行spock.c时没有这样的文件或目录。我理解这个错误通常是因为权限/文件路径问题。但是,两个文件的工作目录是相同的,并且为两者正确设置了权限。我后来发现当我完成输入后输入ctrl + c(^ c)进入终端时错误被删除。我的问题是这个项目是基于使用文本的文件输入,虽然这个代码很容易接受文件,但我需要放置一个getchar()来请求用户键入^ C中断命令接收器正确读取消息队列。我想知道是否有人遇到过这个问题,如果有的话,是否有任何修复建议以便我不需要用户请求^ C中断?抱歉不发布我改变的代码,可以理解的是,我的教授不希望其他学生复制/抄袭。
以下是我写入终端的更多细节: 使用^ C成功输出:
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o kirk -g -Wall
kirk.c
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o spock -g -Wall
spock.c
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./kirk
Enter lines of text, ^D to quit:
value1
value2
value3
^C
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./spock
spock: ready to receive messages, captain.
spock: "value1"
spock: "value2"
spock: "value3"
^C
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$
使用打印指令输出,意思是使用^ D:
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o kirk -g -Wall
kirk.c
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ gcc -o spock -g -Wall
spock.c
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./kirk
Enter lines of text, ^D to quit:
value1
value2
value3
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$ ./spock
msgget: No such file or directory
user@user-ThinkPad-S1-Yoga:~/Documents/message_queue$
现在,即使在用户输入^ C中断时发生了正确的输出,我也不想在终端中键入它,只是希望执行发送程序,然后执行接收程序执行,无需退出&#34; action,因为文件将作为发送程序的输入而不是stdin加载。