C通过命名管道发送分配的字符串-linux

C通过命名管道发送分配的字符串-linux,c,linux,C,Linux,我想通过命名管道发送分配的字符串,而不是简单的字符数组 我得到了以下代码: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <fcntl.h> int main() { int pid, fd; fd = mkfifo("fifo.ftc", S_IRUSR | S_IWUSR);

我想通过命名管道发送分配的字符串,而不是简单的字符数组

我得到了以下代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

int main()
{
    int pid, fd;
    fd = mkfifo("fifo.ftc", S_IRUSR | S_IWUSR);
    pid = fork();

    char* send, * recieve;

    if(pid == 0) {
        recieve = malloc(100);
        fd = open("fifo.ftc", O_RDONLY);
        read(fd, recieve, sizeof(recieve));
        close(fd);
        printf("%s\n", recieve);

        send = malloc(100);
        send = "This text was sent by child!";
        fd = open("fifo.ftc", O_WRONLY);
        write(fd, send, sizeof(send));
        close(fd);

        free(recieve);
        free(send);

    } else if(pid > 0) {
        send = malloc(100);
        send = "This text was sent by parent!";
        fd = open("fifo.ftc", O_WRONLY);
        write(fd, send, sizeof(send));
        close(fd);

        recieve = malloc(100);
        fd = open("fifo.ftc", O_RDONLY);
        read(fd, recieve, sizeof(recieve));
        close(fd);
        printf("%s\n", recieve);

        unlink("fifo.ftc");

        free(send);
        free(recieve);
    }
}

看起来它没有打印出整个字符串。如何接收来自家长和孩子的全部消息?

您的代码中存在多个错误

  • 您必须记住,两者都不能保证它们将有效地读取或写入您告诉它们的字节数

    从Linux write手册页:

    简介

    #包括

    ssize_t write(int fd,const void*buf,size_t count)

    说明

    write()最多可写入从缓冲区指向的buf到 文件描述符fd引用的文件字节数 如果,例如,没有足够的数据,则写入的数据可能小于计数 底层物理介质上的空间,或RLIMIT_FSIZE资源 遇到限制(请参阅setrlimit(2)),或者调用被中断 在写入少于计数字节后由信号处理程序执行。(见 还有管道(7)。)

    它们返回读取或写入的字节数,因此必须将其返回值存储在变量中并相应地进行处理。您通常会将它们包装在一个循环中,保留一个累积读/写字节数的计数器,并在达到原始缓冲区大小或返回错误值时退出循环

  • 正如@wildplasser在评论中提到的,您误用了
    sizeof
    运算符。当你做
    char*foo;富氏
    ,结果大小不是
    foo
    所指字符串的大小,而是字符指针类型的大小(64位体系结构中8个字节,32位中4个字节)。你应该改用strlen(foo)


  • 您的代码中有多个错误

  • 您必须记住,两者都不能保证它们将有效地读取或写入您告诉它们的字节数

    从Linux write手册页:

    简介

    #包括

    ssize_t write(int fd,const void*buf,size_t count)

    说明

    write()最多可写入从缓冲区指向的buf到 文件描述符fd引用的文件字节数 如果,例如,没有足够的数据,则写入的数据可能小于计数 底层物理介质上的空间,或RLIMIT_FSIZE资源 遇到限制(请参阅setrlimit(2)),或者调用被中断 在写入少于计数字节后由信号处理程序执行。(见 还有管道(7)。)

    它们返回读取或写入的字节数,因此必须将其返回值存储在变量中并相应地进行处理。您通常会将它们包装在一个循环中,保留一个累积读/写字节数的计数器,并在达到原始缓冲区大小或返回错误值时退出循环

  • 正如@wildplasser在评论中提到的,您误用了
    sizeof
    运算符。当你做
    char*foo;富氏
    ,结果大小不是
    foo
    所指字符串的大小,而是字符指针类型的大小(64位体系结构中8个字节,32位中4个字节)。你应该改用strlen(foo)


  • @jweyrich已经回答了你问题的要点。然而,还有一些其他的方面你可以考虑…p> 在开始使用该语言时,C中的字符串赋值有时是一个混淆点,因为有时会出现如下赋值:

    send = "This text was sent by parent!";
    
    当它不起作用时,它会起作用。它在这种情况下起作用,因为代码中的变量
    send
    是作为指针创建的。请记住,如果它是作为数组创建的,那么使用
    =
    运算符进行赋值的唯一时间应该是在初始化期间。对C字符串的
    字符数组
    形式的初始化后赋值必须以不同方式进行。通常,字符串函数(
    strcpy
    strcat
    sprintf
    等)用于修改字符串或对字符串进行赋值;示例:

    //string literal used to initialize
    char send[100] = "This text was sent by parent!"; //works
    
    //initialized with nul terminator
    char send[100] = '\0';
    //post initialization statements:
    strcpy(send, "This text was sent by parent!");//works
    strcat(send, "This text was sent by parent!");//works
    sprintf(send, "%s", "This text was sent by parent!");//works
    
    但是:

    此外,建议您始终检查
    malloc()
    的返回,以确保确实分配了内存:

    send = malloc(100);
    if(!send) return -1; //check return of malloc
    send = "This text was sent by parent!";
    
    还有一点:因为
    recieve
    是一个指针,它的大小将是包含其地址所需的字节数,对于32位目标可能是4字节,对于64位目标可能是8字节。因此,声明:

    read(fd, recieve, sizeof(recieve));
    
    很可能是告诉read读取4或8个字节,这可能不是您所期望的。将此行更改为:

    read(fd, recieve, 100);
    

    @jweyrich已经回答了你问题的要点。然而,还有一些其他的方面你可以考虑…p> 在开始使用该语言时,C中的字符串赋值有时是一个混淆点,因为有时会出现如下赋值:

    send = "This text was sent by parent!";
    
    当它不起作用时,它会起作用。它在这种情况下起作用,因为代码中的变量
    send
    是作为指针创建的。请记住,如果它是作为数组创建的,那么使用
    =
    运算符进行赋值的唯一时间应该是在初始化期间。对C字符串的
    字符数组
    形式的初始化后赋值必须以不同方式进行。通常,字符串函数(
    strcpy
    strcat
    sprintf
    等)用于修改字符串或对字符串进行赋值;示例:

    //string literal used to initialize
    char send[100] = "This text was sent by parent!"; //works
    
    //initialized with nul terminator
    char send[100] = '\0';
    //post initialization statements:
    strcpy(send, "This text was sent by parent!");//works
    strcat(send, "This text was sent by parent!");//works
    sprintf(send, "%s", "This text was sent by parent!");//works
    
    但是:

    此外,建议您始终检查
    malloc()
    的返回,以确保确实分配了内存:

    send = malloc(100);
    if(!send) return -1; //check return of malloc
    send = "This text was sent by parent!";
    
    还有一点:因为
    receive
    是一个指针,所以它的大小将是包含它的a所需的字节数