使用消息和msgsnd时在c中提取单个字符
这让我烦了好几天。 问题是我对c语言中的指针和地址的理解不是很好,所以我希望有人能帮我解决这个问题 我需要传递一些字符串作为输入参数,并创建尽可能多的生产者进程+一个消费者进程 生产者应该将字符串分开,并将每个字母作为消息发送到队列。最后它应该发送NULL(“”) 消费者应等待消息并打印出来 整个代码和输出如下。通过查看输出,我会说问题出在生产商的某个地方。更准确地说,它在te for循环的第一行,但我不能正确地得到它 这是操作流程的主程序使用消息和msgsnd时在c中提取单个字符,c,string,char,extraction,garbage,C,String,Char,Extraction,Garbage,这让我烦了好几天。 问题是我对c语言中的指针和地址的理解不是很好,所以我希望有人能帮我解决这个问题 我需要传递一些字符串作为输入参数,并创建尽可能多的生产者进程+一个消费者进程 生产者应该将字符串分开,并将每个字母作为消息发送到队列。最后它应该发送NULL(“”) 消费者应等待消息并打印出来 整个代码和输出如下。通过查看输出,我会说问题出在生产商的某个地方。更准确地说,它在te for循环的第一行,但我不能正确地得到它 这是操作流程的主程序 #include <unistd.h> #
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/msg.h>
int main( int argc, char *argv[], char *envp[] ) {
printf("Starting %d processes \n", argc);
putenv("MSG_KEY=12345");
for (int i = 1; i < argc; i++) {
printf("argv[%d] = %s \n", i, argv[i]);
pid_t producer = fork();
if (producer == 0) {
printf("producer pid - %d\n", getpid());
execl("./producer", "producer", argv[i], NULL);
}
}
pid_t consumer = fork();
if (consumer == 0) {
printf("consumer pid - %d\n", getpid());
execl("./consumer", "consumer", NULL);
exit(0);
} else {
printf("manager pid - %d\n", getpid());
wait(NULL);
}
int status;
while(waitpid(consumer, &status, 0) == -1);
printf("DONE consumer\n");
printf("DONE manager\n");
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[],char*envp[]{
printf(“启动%d个进程\n”,argc);
putenv(“MSG_KEY=12345”);
对于(int i=1;i
制作人
/*
** writes to message queue
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct my_msgbuf {
long mtype;
char mtext[1];
};
int main( int argc, char *argv[], char *envp[] ) {
struct my_msgbuf buf;
int msqid;
key_t key = atoi(getenv("MSG_KEY"));
if ((msqid = msgget(key, 0600 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
buf.mtype = getpid();
// I believe the error is in this for loop or to be more precise in the first line of the for loop.
// takes the first argument and sends characters in separate messages
for (int i = 0; i < strlen(argv[1]); ++i) {
char c = argv[1][i];
strcpy(buf.mtext, &c);
printf ("Sending -%s-\n", buf.mtext);
if (msgsnd(msqid, (struct msgbuf *)&buf, strlen(buf.mtext)+1, 0) == -1)
perror("msgsnd");
}
// send NULL at the end
memcpy(buf.mtext, "", strlen("")+1);
if (msgsnd(msqid, (struct msgbuf *)&buf, strlen("")+1, 0) == -1)
perror("msgsnd");
return 0;
}
/*
**写入消息队列
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构my_msgbuf{
长型;
字符多行文字[1];
};
int main(int argc,char*argv[],char*envp[]{
结构my_msgbuf buf;
int msqid;
key_t key=atoi(getenv(“MSG_key”);
如果((msqid=msgget(键,0600 | IPC_CREAT))=-1){
佩罗尔(“msgget”);
出口(1);
}
buf.mtype=getpid();
//我相信错误在这个for循环中,或者更准确地说,在for循环的第一行中。
//接受第一个参数并在单独的消息中发送字符
对于(int i=0;i
消费者
/*
** reads from message queue
*/
#include <stdio.h>
#include <unistd.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[1];
};
int main( int argc, char *argv[], char *envp[] ) {
struct my_msgbuf buf;
int msqid;
key_t key = atoi(getenv("MSG_KEY"));
if ((msqid = msgget(key, 0600 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
int flag = 0;
int wait_counter = 0;
while (wait_counter < 10) {
msgrcv(msqid, (struct msgbuf *)&buf, sizeof(buf)-sizeof(long), 0, flag);
if (errno == ENOMSG){
wait_counter++;
printf ("Sleaping for one second...zzzZZZzzz...%d\n", wait_counter);
usleep(1000 * 1000);
} else {
printf("Received:\n\ttype: -%ld- \n\tchar: -%s- \n", buf.mtype, buf.mtext);
int compare = strcmp(buf.mtext, "");
if(compare == 0){
printf("NULL received\n");
flag = IPC_NOWAIT;
} else {
flag = 0;
}
wait_counter = 0;
}
errno = 0;
}
if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
} else {
printf("Message queue removed\n");
}
return 0;
}
/*
**从消息队列读取
*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构my_msgbuf{
长型;
字符多行文字[1];
};
int main(int argc,char*argv[],char*envp[]{
结构my_msgbuf buf;
int msqid;
key_t key=atoi(getenv(“MSG_key”);
如果((msqid=msgget(键,0600 | IPC_CREAT))=-1){
佩罗尔(“msgget”);
出口(1);
}
int标志=0;
int wait_计数器=0;
while(等待计数器<10){
msgrcv(msqid,(struct msgbuf*)&buf,sizeof(buf)-sizeof(long),0,标志);
if(errno==enomg){
等等,计数器++;
printf(“搜索一秒钟…zzzzzzzzzzz…%d\n”,等待\u计数器);
usleep(1000*1000);
}否则{
printf(“收到:\n\t类型:-%ld-\n\t字符:-%s-\n”,buf.mtype,buf.mtext);
int compare=strcmp(buf.mtext,“”);
如果(比较==0){
printf(“收到空\n”);
flag=IPC_NOWAIT;
}否则{
flag=0;
}
等待计数器=0;
}
errno=0;
}
if(msgctl(msqid,IPC_RMID,NULL)=-1){
perror(“msgctl”);
出口(1);
}否则{
printf(“消息队列已删除\n”);
}
返回0;
}
输出-我必须给你这里的截图,因为c/p删除了问题,一切看起来都正常
任何帮助都将不胜感激!谢谢大家!
按照下面@sergeya答案中的建议使用时出错 *buf.mtext=c 你的问题(至少是其中一个)在这里:
strcpy()
将尝试复制尽可能多的字符,直到遇到nul终止符'\0'
,从c
开始。你需要准确地复制一个字符,所以你只需要
*buf.mtext = c;
正如我所说,问题在于for循环中的生产者。我把零钱放在这里。希望它能帮助任何有类似问题的人 @SergeyA给了我很好的线索,问题出在哪里,所以我从“strcpy”改为“memcpy”,我只复制了第一个字符,没有复制nul终结者。 此外,我还将“strlen”改为“sizeof”,并删除了+1 制作人
...
for (int i = 0; i < strlen(argv[1]); ++i) {
char c = argv[1][i];
memcpy(buf.mtext, &c, sizeof(&c)+1);
printf ("Sending -%c-\n", buf.mtext);
if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf.mtext), 0) == -1)
perror("msgsnd");
}
...
。。。
对于(int i=0;i
我喜欢你的评论,你可能是对的,但你提出的更改并非100%正确producer.c:在函数“main”中:
producer.c:38:13:错误:赋值给数组类型为buf.mtext=c的表达式代码>@Vedran,对。我忘了你声明它是数组。更新了代码。这将创建一个循环,用于发送大量消息。我不能把图片放在这里,所以我会把它放在上面问题的末尾。你在接收端也有同样的问题。你能更详细一些吗
...
for (int i = 0; i < strlen(argv[1]); ++i) {
char c = argv[1][i];
memcpy(buf.mtext, &c, sizeof(&c)+1);
printf ("Sending -%c-\n", buf.mtext);
if (msgsnd(msqid, (struct msgbuf *)&buf, sizeof(buf.mtext), 0) == -1)
perror("msgsnd");
}
...