简体   繁体   English

前叉和等待系统调用的问题

[英]Issue with fork and wait system call

I have written a basic c++ program in unix with fork() and wait() system call. 我已经用fork()和wait()系统调用在UNIX中编写了一个基本的c ++程序。 I am only creating one child. 我只生一个孩子。 I have used two pipes. 我用了两个管道。 So After fork operation with first pipe i am writing from child to parent and as after parent receives the data, parent is writing back to child in second pipe. 因此,在使用第一个管道进行fork操作之后,我正在从子级写入父级,而当父级收到数据后,父级正在第二个管道中回写给子级。 after that in parent side I am using wait(0) system call. 之后,在父端,我正在使用wait(0)系统调用。 but still my parent process dies before child process? 但是我的父进程仍然在子进程之前死亡吗?

structure is something like this: 结构是这样的:

 main()
 char buff[] = "Parent process kills";
 char cuff[] = "before Child process";
 int fd1[2];
 int fd2[2];
 pipe(fd1);
 pipe(fd2);
 if((pid = fork()) == 0)
 {
   close(fd1[0]);
   close(fd2[1]);
   write(fd1[1],buff,strlen(buff)+1);
   read(fd2[0],cuff,sizeof(cuff));

 }
 else
 {
    close(fd1[1]);
    close(fd2[0]);

    read(fd1[0],buff,sizeof(buff));
    write(fd2[1],cuff,strlen(cuff)+1);
    wait((int *) 0);
  }

  close(fd1);
  close(fd2);

  }'

Even though wait() is used but still parent process dies before child. 即使使用了wait(),但父进程仍然在子进程之前死亡。 Thanks in adavance. 非常感谢。

Your call to read result in undefined behavior. 您的read调用导致未定义的行为。 You try to read into string literals, not the buffers you have. 您尝试读入字符串文字,而不是您拥有的缓冲区。 In this case it probably results in a crash. 在这种情况下,可能会导致崩溃。

Your write calls also writes a string literal and not the buffer you have. 您的write调用还会写入字符串文字,而不是您拥有的缓冲区。

Also, since you have character arrays initialized to strings, sizeo(buff) and strlen(buff) + 1 are equal. 另外,由于将字符数组初始化为字符串,因此sizeo(buff)strlen(buff) + 1相等。

Are you sure you're not dying due to a segfault? 您确定您不会因为段错误而死吗? Each of these commands is trying to send more than you intend: 这些命令中的每一个都试图发送比您期望的更多的信息:

write(fd1[1],"buff",strlen(buff)+1);

and

write(fd2[1],"cuff",strlen(cuff)+1);

and each of these is trying to receive into read-only memory: 并且每个都试图接收到只读存储器中:

read(fd2[0],"cuff",sizeof(cuff));

and

read(fd1[0],"buff",sizeof(buff));

There is a subtle error in the line 该行中有一个细微的错误

if(pid == fork())

You compare the result of fork() with pid instead of assigning to it and comparing it to zero. 您将fork()的结果与pid进行比较,而不是对其进行赋值并将其与零进行比较。 What you wanted to write is this: 您要写的是这样的:

if((pid = fork()))

Note the extra set of parentheses that tells the compiler that you really want to do the assignment, and that you don't want get a warning on it. 请注意额外的括号集,这些括号告诉编译器您确实要执行分配,并且不想收到警告。

And with the corrected if, you have the parent executing the first case, not the second, so the correct code would be: 并在更正后的情况下,让父级执行第一种情况,而不是第二种情况,因此正确的代码将是:

if(pid == fork()) {
  close(fd1[1]);
  close(fd2[0]);

  read(fd1[0],"buff",sizeof(buff));
  write(fd2[1],"cuff",strlen(cuff)+1);
  wait((int *) 0);
} else {
  close(fd1[0]);
  close(fd2[1]);
  write(fd1[1],"buff",strlen(buff)+1);
  read(fd2[0],"cuff",sizeof(cuff));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM