【问题标题】:how to send dynamically allocated string using pipe如何使用管道发送动态分配的字符串
【发布时间】:2016-06-28 05:00:54
【问题描述】:

我有一个作业需要使用管道在进程之间发送一堆字符串。

当我使用管道将 创建编译时间的字符串 从子进程发送到主进程时,它已成功发送。这是我在主进程和子进程中所做的。

子进程:

    char* str ="from child";
    int lengthOfString=strlen(str)+1;//+1 for null terminator

  write(pipes[0][1],&lengthOfString,sizeof(int));//send the length of string beforehand
  write(pipes[0][1],&str,lengthOfString); //send the actual string

主要流程:

int numberOfChar;
    char* buffer;
    buffer=(char*)malloc(sizeof(char)*15);
        read(pipes[0][0],&numberOfChar,sizeof(int));//get length of upcoming string
        read(pipes[0][0],&buffer,numberOfChar);//read the actual sting
        printf("received from pipe :%s\n",buffer);

但是,如果我对 动态分配的字符串 尝试相同的操作,我在主进程中只会收到 NULL 作为字符串。这就是我所做的。

子进程:

    char* dynamicallyAllocatedString=(char*)malloc(sizeof(char)*strlen(str));
    strcpy(dynamicallyAllocatedString,str);
    int lengthOfString=strlen(dynamicallyAllocatedString)+1;//+1 for null terminator

  write(pipes[0][1],&lengthOfString,sizeof(int));//send the length of string beforehand
  write(pipes[0][1],&dynamicallyAllocatedString,lengthOfString); //send the actual string

主要流程:

   read(pipes[0][0],&numberOfChar,sizeof(int));//get length of upcoming string
        buffer=(char*)malloc(sizeof(char)*numberOfChar);
        read(pipes[0][0],&buffer,numberOfChar);//read the actual sting
        printf("received from pipe :%s\n",buffer);

这里是完整的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char * argv[])
{
pid_t pid;
int numberOfPipe=1;
int ret;
int c;
int pipeNumber=-1;
int** pipes= (int**)malloc(numberOfPipe*sizeof(int));
 for(c=0;c<numberOfPipe;c++)//create pipe for each process
{
    pipes[c]= (int*)malloc(sizeof(int)*2);
    ret = pipe(pipes[c]);
    if(ret==-1)
        {
            perror("pipe error");
            exit(1);
        }
}


for(c=0;c<numberOfPipe;c++)//create child process
{
pid = fork();
pipeNumber++;
if(0==pid)
{
    break;
}
}
if(0==pid)
{
    // Child process
    int i=0;
    char* str ="from child";
    char* dynamicallyAllocatedString=(char*)malloc(sizeof(char)*strlen(str));
    strcpy(dynamicallyAllocatedString,str);
    int lengthOfString=strlen(dynamicallyAllocatedString)+1;//+1 for null terminator
    close(pipes[pipeNumber][0]); //close read side

  write(pipes[pipeNumber][1],&lengthOfString,sizeof(int));//send the length of string beforehand
  write(pipes[pipeNumber][1],&dynamicallyAllocatedString,lengthOfString); //send the actual string

    close(pipes[pipeNumber][1]); //close write side
    exit(0);
}
else
{
    //main process
    printf("main process\n");

    int numberOfChar;
    char* buffer;


    for(c=0;c<numberOfPipe;c++)//close write side of each pipe
    {
        close(pipes[c][1]);
    }

    for(c=0;c<numberOfPipe;c++)//iterate each pipe
    {
        read(pipes[c][0],&numberOfChar,sizeof(int));//get length of upcoming string
        buffer=(char*)malloc(sizeof(char)*numberOfChar);
        read(pipes[c][0],&buffer,numberOfChar);//read the actual sting
        printf("received from pipe :%s\n",buffer);
    }

    for(c=0;c<numberOfPipe;c++)//close read side of each pipe
    {
        close(pipes[c][0]);
    }
}

printf("end");
return 0;

}

我能做些什么来获得实际的字符串而不是 NULL ? 谢谢。

【问题讨论】:

  • malloc(sizeof(char)*strlen(str)) 不足以存储 C 字符串。如果您打算离开终止的'\0',请务必在两侧适当地处理“字符串”。

标签: c++ c operating-system pipe


【解决方案1】:
write(pipes[0][1],&dynamicallyAllocatedString,lengthOfString);

你传递一个指向动态分配字符串的指针,而不是一个指向实际字符串的指针。

您的代码中的第二个问题是您没有检查 read() 或 write() 的返回值。当您使用管道时,不能保证您请求的整个写入或读取都会成功。

read() 和 write() 都可以返回读取或写入的字节数少于请求的字节数。您的代码必须检查返回值并采取适当的措施。如果不这样做,最终会导致难以定位和调试的细微错误。

此外,对于您声称的“编译时”案例,您声称可以正常工作,但出于同样的原因,它不能正常工作。要么这不是您使用的实际代码,要么您未能检测到相同的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2019-03-23
    • 2014-04-20
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多