简体   繁体   English

如何找出代码何时读取父进程或子进程以及它们如何在C中进行通信

[英]how to find out when the code reads parent or child process and how do they communicate in C

I have a shell program where a fork is created and then used two if else statements to separate the parent and the child process. 我有一个shell程序,在其中创建一个fork,然后使用两个if else语句将父进程和子进程分开。

My sample program is here and i got a few questions 我的示例程序在这里,我有几个问题

while(true)
{
    /* read command line input */

    x = fork();

    if( x > 0)
        {
         wait(&status);      
        }
    else
        {
         /* run the exec() command  */
        }
}

from the above code which statement would it execute first after fork and how would parent know that child exec command was executed successfully or unsuccessfully and when would the parent stop to wait and in which condition and how. 从上面的代码中,将在fork之后首先执行哪个语句,父级将如何知道子exec命令已成功执行或未成功执行,父级将何时停止等待以及在哪种条件下以及如何执行。 And also how would the wait(&status) code work. 还有wait(&status)代码如何工作。

Help would be much appreciated. 帮助将不胜感激。

叉子()。

This should give you a better idea of how the fork() system call works. 这应该使您更好地了解fork()系统调用的工作方式。


We can't decide which process (parent or child) should execute first. 我们无法决定哪个流程(父流程或子流程)应首先执行。 If a child has already terminated and is a zombie, wait returns immediately with that child's status. 如果孩子已经终止并成为僵尸,则wait会立即返回该孩子的状态。 vfork() guarantees that the child runs first, until the child calls exec or exit vfork()确保子级先运行,直到子级调用exec或退出

According to the POSIX specification of fork , there is no priority to run the parent or the child process. 根据forkPOSIX规范 ,运行父进程或子进程没有优先级。 So, in fact, you cannot expect one to be prior to the other one. 因此,实际上,您不能期望一个优先于另一个。

But, if you are using Linux, the child is always scheduled first after the fork. 但是,如果您使用的是Linux,则始终在派生之后首先安排子项。 Mainly, because of implementation reasons. 主要是由于实施原因。 Thus, the parent is started after. 因此,父级之后开始。 But, you also have to understand that they run concurrently once they are started. 但是,您还必须了解它们在启动后会同时运行。 See the fork manpage . 请参见fork手册

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

相关问题 如何让守护进程与 C 中的子进程通信 - How do I get daemon process to communicate with child process in C 如何查找父流程的所有子流程(在C中) - How to find all child process of a parent process (in C) 父进程如何找出子进程是否终止? - How can a parent-process find out if the child-process was terminated? C:如何在孩子的末尾打印父进程? - C: How to print parent process at the end of child? 如何运行子进程,并在 C 中与父进程一起执行? - How to run a child process, and act on it with the parent in C? 当父母发生某些事情时,父母程序如何告诉孩子去做某事? - How can parent process tell child to do something when something happens to parent? 在 C 中调用 execv(“/bin/sh”, NULL) 时,如何与子进程通信(例如,通过写入运行命令)? - how can I communicate (eg. run commands via write) with a child process when execv(“/bin/sh”, NULL) was called in C? Ruby进程和C进程之间如何通信? - How to communicate between Ruby process and C process? 在 Windows 中,如何在 C 中跟踪子进程读取和写入的文件? - In Windows, how can I trace in C which files a child process reads and writes? 在 C 中给定父 PID 时如何获取所有子进程的 PID - How to get all child process's PIDs when given the parent PID in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM