Explain this code's working; how the child process returns values and where?

烂漫一生 提交于 2019-12-13 12:12:33

问题


I don't get how the value is returned by a child process and to whom?

Output is 6, 7 ; question source: http://www.cs.utexas.edu/~mwalfish/classes/s11-cs372h/hw/sol1.html

Program 1: 
main() 
{ 
    val = 5; 
    if(fork()) 
    wait(&val); 
    val++; 
    printf("%d\n", val); 
    return val; 
}

回答1:


Main process:

val = 5; 
wait(&val); // wait until child finishes

Child process:

val++; // val becomes 6
printf("%d\n", val); // prints 6
return val; // return val back to main process

Main process:

wait(&val); // val becomes 6
val++; // val becomes 7
printf("%d\n", val); // prints 7
return val; 



回答2:


if(fork()) 

Creates a child process. Each process gets a copy of the var. fork returns a non-zero value in parent process. so the if gets executed only for the parent process. and wait is only called for parent process. It waits for child process to complete execution.

Child process increments val prints it and returns. Once it returns parent process resumes and executes further by incrementing var, printing its value and then returning from it from the main().




回答3:


A fork() basically creates a new process. This means that all current values are going to be copied. This also concludes that val in your parent process isn't the val in your child process. This is the reason you'll have to communicate somehow with the child process, which you do by using wait.

      Parent Process       |       Child process
---------------------------|---------------------------
main()                     |
{                          | #########################
    int val = 5;           | // int val = parent.val;
**  int tmp = fork();      | ** int tmp = 0;
    if(tmp) // true        |    if(tmp) // false
        wait(&val);        |        // doesn't use wait
        // waits until     |    val++; // val = 6
        // child process   |    printf("%d\n", val); 
        // returns.        |    return val; // return 6
        // saves return    | ###########|############
        // value in val <---------------+
    val++; // val = 7      | #########################
    printf("%d\n", val);   | #########################
    return val;            | #########################
}

Whenever a process exits in Linux, the return value is stored temporary. As long as the parent process doesn't get this stored value the child process is still listed as zombie. This value can be acquired with wait. So whenever you use return <value> at the end of your application wait() in your caller will give you this return value.




回答4:


The above output is wrong.

Main process:


val = 5; 
wait(&val); // wait until child finishes

Child process:

val++; // val becomes 6
printf("%d\n", val); // prints 6
return val; // return val*256 back to main process

Main process:

wait(&val); // val becomes 6
val++; // val becomes 1792+1=1793
printf("%d\n", val); // prints 1793
return val; 

The above output is right if you add one line in a code !

Program 1: 
main() 
{ 
    val = 5; 
    if(fork()) 
    wait(&val); 

val=val/256 // if you add this line then the above output is correct

    val++; 
    printf("%d\n", val); 
    return val; 
}

it is due to

#define WEXITSTATUS(status) (((status)>>8) & 0xFF)


来源:https://stackoverflow.com/questions/13854788/explain-this-codes-working-how-the-child-process-returns-values-and-where

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!