1

我编写了一个简单的代码,其中一个进程将结构传递给另一个进程,父进程正在写入数据,但子进程无法访问它。我使用fork()系统调用来创建两个进程。但是我在这里遇到问题,我调用了两次父进程,并且在我运行程序时也调用了两次子进程。谁能让我知道我在做什么错误。

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <string.h>

#define MAX_LINE_LEN    100
#define FIFO_NAME "my_fifo"

typedef struct student_info  {
char *name;
int age;
char *sex;
}student;

int w_byte_parent = 0, r_byte_parent = 0;
int w_byte_child = 0, r_byte_child = 0;

void do_child() {
  int fifo;
  char buffer[MAX_LINE_LEN];
  int i = 0;
  student child;

  printf("Child opening FIFO \n");
  if( (fifo = open(FIFO_NAME, O_RDWR)) < 0 )   {
    perror("open in child failed");
    exit(EXIT_FAILURE);
  }
  printf("Child  reading  from FIFO \n");
  r_byte_child = read(fifo, child, sizeof(student));
  if(r_byte_child < 0)
    printf("Read failed by child process\n");
  printf("%d Bytes read by child process\n", r_byte_child);
  }

 int main(int argc, char **argv)  {
 int fifo;
 char buffer[MAX_LINE_LEN];
 int ch = 0, i = 0;

 /*
 ** Create a FIFO
 */
 /* Parent creating FIFO */
if( (mkfifo(FIFO_NAME, 0666)) < 0)  {
    if( errno != EEXIST ) {
        perror( "mkfifo" );
        exit( EXIT_FAILURE );
    }
}
/*
 ** Create other process 
 */

switch(fork())  {
    case -1:
        perror("fork()");
        exit(EXIT_FAILURE);
    case 0: /* Child Process */
        do_child();
        break;
    default:/* Parent Process */
        break;
}
/* Pass a structure to FIFO */
student *info;
info = (student *)malloc( sizeof (student)) ;
info->name = (char *)calloc(sizeof(char), 10);
strcpy(info->name, "jack");
info->age = 27;
info->sex = (char *)calloc(sizeof(char), 10);
strcpy(info->sex , "Male");

/* Parent Opening FIFO */
printf("parent opening FIFO \n");
if( (fifo = open(FIFO_NAME,  O_RDWR)) < 0 )   {
    perror("open in parent failed");
    exit(EXIT_FAILURE);
}

/*
 ** Write some thing into FIFO so child can read it
 */
printf("parent writing to FIFO \n");
w_byte_parent = write( fifo, info, sizeof(struct student_info));
if(w_byte_parent < 0)
    printf("Nothing was written to FIFO by parent\n");
printf("Wrote %d bytes to FIFO\n",w_byte_parent);
}
4

1 回答 1

3

让我们从基础开始。使用 GCC 编译代码,-Wall选项如下:

$ gcc fifo.c -o fifo -Wall
fifo.c: In function ‘do_child’:
fifo.c:33: warning: implicit declaration of function ‘read’
fifo.c:24: warning: unused variable ‘i’
fifo.c:23: warning: unused variable ‘buffer’
fifo.c: In function ‘main’:
fifo.c:48: warning: implicit declaration of function ‘mkfifo’
fifo.c:58: warning: implicit declaration of function ‘fork’
fifo.c:88: warning: implicit declaration of function ‘write’
fifo.c:42: warning: unused variable ‘i’
fifo.c:42: warning: unused variable ‘ch’
fifo.c:41: warning: unused variable ‘buffer’
fifo.c:92: warning: control reaches end of non-void function

忽略所有未使用的变量问题,您需要包含<unistd.h><sys/stat.h>获得readmkfifofork和的正确声明write。再次编译:

$ gcc fifo.c -o fifo -Wall -Wno-unused
fifo.c: In function ‘do_child’:
fifo.c:35: error: incompatible type for argument 2 of ‘read’
fifo.c: In function ‘main’:
fifo.c:94: warning: control reaches end of non-void function

您将student实例作为参数 2 传递给read,但它需要一个void*指针。您需要将实例的地址作为. 此外,显式返回值形式会很好,尽管这不是绝对必要的,因为未能返回值形式会隐式返回 0(但适用于函数)。student&childmain()main()main()

但即使纠正了这些错误,您仍然会看到两次父进程的输出。为什么?因为父进程和子进程都在执行switch语句下面的代码块。子进程调用该do_child()函数并且从不退出,因此它返回并继续执行。

对此的简单解决方法是确保在结束时do_child()或调用返回后立即退出子进程,例如

于 2013-09-11T02:11:09.780 回答