0

我正在学习如何在linux中使用普通管道进行父子进程之间的通信。基本任务只是从父进程向子进程发送消息,然后子进程进行一些转换并将结果传递回父进程。我显示的结果是一些随机字符,如����。我已经考虑了很长时间,仍然无法弄清楚这个错误。谢谢你的帮助。

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define READ_END 0
#define WRITE_END 1

void convert(char* str);

int main(int argc, char *argv[]){
  int pid; /* Process ID */
  int status;
  char *input;
  char *read_msg_c;
  char *read_msg_p;
  int pfd1[2], pfd2[2];
  if (argc !=2){/* argc should be 2 for correct execution */
    /* We print argv[0] assuming it is the program name */
    printf("Please provide the string for conversion \n");
    exit(-1);
  }
  input = argv[1];

  if(pipe(pfd1) < 0 || pipe(pfd2) < 0){
    printf("Failed to create a pipe between parent and child \n");
    exit(-1);
  }
  if((pid = fork()) < 0){ /* Fork the process */
     printf("Fork error \n");
     exit(-1);
  }
  else if(pid > 0){ /* Parent code */
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    printf("Process ID of the parent is %d. \n", getpid()); /* Print parent's process ID */
    write(pfd1[WRITE_END],input,strlen(input)+1);
    close(pfd1[WRITE_END]);

    read(pfd2[READ_END],read_msg_p,strlen(input)+1);
    printf("%s\n",read_msg_p);
    close(pfd2[READ_END]);
  }
  else if(pid == 0){ /* Child code */
    close(pfd1[WRITE_END]);
    close(pfd2[READ_END]);

    printf("Process ID of the child is %d. \n", getpid()); /* Print child's process ID */
    read(pfd1[READ_END],read_msg_c, strlen(input)+1);
    printf("Child: Reversed the case of the received string. \n");
    write(pfd2[WRITE_END],read_msg_c,strlen(input)+1);
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    exit(0); /* Child exits */
   }
}

void convert(char *str){
  int i = 0;
  while (str[i]){
    if (isupper(str[i])){
      str[i] = tolower(str[i]);
    }
    else if (islower(str[i])){
      str[i] = toupper(str[i]);
    }
    i++;
  }
}
4

1 回答 1

2

您的主要错误是您的变量read_msg_pread_msg_c未初始化的指针。

把它们做成数组:

char read_msg_p[1024];
char read_msg_c[1024];

你似乎失踪了<stdio.h>(但你真的不需要<sys/types.h>更多了)。你应该错误检查你的读写;为它们分配空间后,您的读取可能会使用不同的最大大小。等等。

我通过查看编译器警告发现了问题:

$ gcc -O3 -g -std=c99 -Wall -Wextra pipes-14420398.c -o pipes-14420398
pipes-14420398.c: In function ‘main’:
pipes-14420398.c:40:22: warning: ‘read_msg_p’ may be used uninitialized in this function [-Wuninitialized]
pipes-14420398.c:52:22: warning: ‘read_msg_c’ may be used uninitialized in this function [-Wuninitialized]
$

忽略行号;当这些是剩下的唯一警告时,我已经适度严重地破解了你的代码。但有问题的线路是read()电话。


示例输出来自被黑代码,工作正常。

$ ./pipes-14420398 string-to-convert
Process ID of the parent is 37327. 
Process ID of the child is 37328. 
Child read 18 bytes: <<string-to-convert>>
Parent read 18 bytes: <<string-to-convert>>
$

请注意,下面的代码读取 18 个字节(包括 null),但不打印 null(nbytes-1因为printf().

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define READ_END 0
#define WRITE_END 1


int main(int argc, char *argv[])
{
  int pid; /* Process ID */
  char *input;
  char read_msg_c[1024];
  char read_msg_p[1024];
  int pfd1[2], pfd2[2];

  if (argc !=2){/* argc should be 2 for correct execution */
    /* We print argv[0] assuming it is the program name */
    fprintf(stderr, "Usage: %s string-to-convert\n", argv[0]);
    exit(-1);
  }
  input = argv[1];

  if(pipe(pfd1) < 0 || pipe(pfd2) < 0){
    printf("Failed to create a pipe between parent and child \n");
    exit(-1);
  }
  if((pid = fork()) < 0){ /* Fork the process */
     printf("Fork error \n");
     exit(-1);
  }
  else if(pid > 0){ /* Parent code */
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    printf("Process ID of the parent is %d. \n", getpid()); /* Print parent's process ID */
    write(pfd1[WRITE_END], input, strlen(input)+1);
    close(pfd1[WRITE_END]);

    int nbytes = read(pfd2[READ_END], read_msg_p, sizeof(read_msg_p));
    if (nbytes <= 0)
        printf("Parent: read failed\n");
    else
        printf("Parent read %d bytes: <<%.*s>>\n", nbytes, nbytes-1, read_msg_p);
    close(pfd2[READ_END]);
  }
  else if(pid == 0){ /* Child code */
    close(pfd1[WRITE_END]);
    close(pfd2[READ_END]);

    printf("Process ID of the child is %d. \n", getpid()); /* Print child's process ID */
    int nbytes = read(pfd1[READ_END], read_msg_c, sizeof(read_msg_c));
    if (nbytes <= 0)
        printf("Child: read failed\n");
    else
    {
        printf("Child read %d bytes: <<%.*s>>\n", nbytes, nbytes-1, read_msg_c); 
        write(pfd2[WRITE_END], read_msg_c, nbytes);
    }
    close(pfd1[READ_END]);
    close(pfd2[WRITE_END]);
    exit(0); /* Child exits */
   }
}

正如WhozCraig所指出的,可以进行许多其他更改。但是,这可以使事情合理地干净地工作。你非常接近OK。

注意调试技术:

  1. 以高警告级别编译并修复所有警告。
  2. 在信息可用时打印信息(或在调试器中运行并在信息可用时观察信息)。
于 2013-01-20T00:37:24.223 回答