【问题标题】:Problem to understand how it works theses processes理解这些过程是如何工作的问题
【发布时间】:2018-11-09 21:30:30
【问题描述】:

晚上好,

我正在使用 fork() 和 waitpid() 系统调用对 C 中的进程进行一些编程和测试。我理解全局变量的行为,但我不明白为什么当第二个进程完成并返回第一个进程时,变量“i”与第二个进程具有相同的值。

还有为什么当程序回到根进程时,变量“i”的值是 2。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int total = 0;
int main(int argc, char **argv) {
    int i, pid;

    for(i = 1; i < 3; ++i) {
        pid = fork();

        if(pid == 0) {
            total = total + i;
            printf("\n");
            printf("Child process %d\n", getpid());
            printf("Parent process %d\n", getppid());
            printf("i = %d\n", i);
        } else {
            waitpid(pid, NULL, 0);
        }
    }
    printf("Child process (end) %d\n", getpid());
    printf("Parent process (end) %d\n", getppid());
    printf("total = %d\n", total);
    printf("i = %d\n", i);
    exit(0);
}

这是执行的结果

Child process 9191
Parent process 9190
i = 1

Child process 9192
Parent process 9191
i = 2

Child process (end) 9192
Parent process (end) 9191
total = 3
i = 3

Child process (end) 9191
Parent process (end) 9190
total = 1
i = 3

Child process 9193
Parent process 9190
i = 2

Child process (end) 9193
Parent process (end) 9190
total = 2
i = 3

Child process (end) 9190
Parent process (end) 2876
total = 0
i = 3

我有一个建议:就是函数waitpid()把资源拿给子进程,但是它不能解释根进程中变量“i”的值。

我希望我清楚自己的问题,并为我的英语有点糟糕感到抱歉。

感谢您的回答。

【问题讨论】:

  • 当您fork() 时,原始进程和子进程从分叉点继续,最初状态相同(受某些例外情况不问题在这里)。特别是,在您的情况下,两者都独立地继续执行循环。如果这不能回答问题,那么恐怕你需要更清楚地解释你是什么让你感到困惑。
  • 您可能会发现绘制流程树或为每个流程写出事件表很有帮助。
  • it cannot explain the value of the variable "i" in the root process。实际上,您最后只打印一次根进程的i。在循环中,您仅在 fork() 返回 0 时打印,并且仅适用于子进程。
  • OT:当main()argcargv[]的参数不被使用时,为避免编译器输出未使用变量的警告信息,使用签名:int main( void )
  • 关于:if(pid == 0) { total = total + i; printf("\n"); printf("Child process %d\n", getpid()); printf("Parent process %d\n", getppid()); printf("i = %d\n", i); } 在子进程结束时,必须有一个exit( 0 ); 语句,否则子进程将继续循环执行

标签: c linux fork waitpid


【解决方案1】:

以下更正的代码:

  1. 干净编译
  2. 执行所需的功能
  3. 清楚地表明父进程中的total 没有被子进程更改

现在,建议的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int total = 0;


int main( void )   // <<-- corrected statement
{
    int i, pid;

    for(i = 1; i < 3; ++i) 
    {
        pid = fork();

        if(pid == 0) 
        {
            total = total + i;
            printf( "\n") ;
            printf( "Child process %d\n", getpid() );
            printf( "Parent process %d\n", getppid() );
            printf( "i = %d\n", i );
            exit( 0 );    // <<-- added statement
        } 

        else 
        {
            waitpid( pid, NULL, 0 );
        }
    }

    printf( "Child process (end) %d\n", getpid() );
    printf( "Parent process (end) %d\n", getppid() );
    printf( "total = %d\n", total );
    printf( "i = %d\n", i );
    exit( 0 );
}

以上代码的输出为:

Child process 10378
Parent process 10377
i = 1

Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3

但是,代码无法从函数中检查错误情况:fork(),因此如果对fork() 的调用失败,则将面临重大故障。 IE。代码应该(也)检查来自对fork()的调用的返回值-1并调用

if( pid < 0 )
{
    perror( "fork failed");  
    exit( 1 );`
}

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2021-12-04
    • 2013-04-04
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    相关资源
    最近更新 更多