c - 使用消息和 msgsnd 时在 C 中提取单个字符

标签 c string char extract garbage

这件事困扰了我好几天。 问题是我对 C 中的指针和地址不太了解,所以我希望有人能够帮助我。

我需要传递一些字符串作为输入参数,并创建尽可能多的生产者进程 + 一个消费者进程。

生产者应该将字符串分开并将每个字母作为消息发送到队列。最后它应该发送 NULL("")。

消费者应该等待消息并将其打印出来。

完整的代码和输出如下。通过查看输出,我会说问题出在生产者的某个地方。更准确地说,它位于 te for 循环的第一行,但我无法正确理解。

manager.c - 这是操作进程的主程序

#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;
}

生产者.c

/*
**  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;
}

消费者.c

/*
**  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;
}

输出 - 我必须在这里给你屏幕截图,因为 c/p 删除了问题并且一切看起来都正常

enter image description here

任何帮助将不胜感激!谢谢!

<小时/>

按照下面 @sergeya 答案中的建议使用时出错 *buf.mtext = c;

enter image description here

最佳答案

您的问题(至少其中一个)在这里:

  char c = argv[1][i];
  strcpy(buf.mtext, &c);

strcpy() 将尝试从 c 开始复制尽可能多的字符,直到遇到 nul 终止符 '\0' >。你需要精确复制一个字符,所以你只需要

*buf.mtext = c;

关于c - 使用消息和 msgsnd 时在 C 中提取单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35928389/

相关文章:

更改固定的二维数组以获取运行时给定的大小

python - <bytes> 转义 <str> Python 3

c - 对 char 数组中存储的字节使用 strtol

c - 如何在结构数组中使用数组变量?

c - 对于循环 : increment and decrement of variables

c - C的FILE有面向对象的接口(interface)吗?

c# - 验证字符串仅包含 C# 控制台应用程序中的字母

php - 使用 PHP 在文本框中显示动态文本时转义单引号和双引号

c++ - 如何在类中将 char 作为函数参数传递?

C Linked List char数组输入重用问题