fork()

The first program in the detailed explanation of the fork() function in Linux has been modified as follows:

#include <unistd.h>
#include <stdio.h>
int main ()
{
    pid_t fpid; //fpid表示fork函数返回的值
    int count=0;

	printf("========================\n");
    fpid=fork();
	printf("------------------------\n");
    if (fpid == 0) {
        printf("i am the child process, my process id is %d\n",getpid());
        printf("我是儿子\n");//对某些人来说中文看着更直白。
        count++;
        printf("fpid = %d\n", fpid);
    } else if (fpid > 0 ){
        sleep(3);
        printf("i am the parent process, my process id is %d\n",getpid());
        printf("我是他爹\n");
        count++;
        printf("fpid = %d\n", fpid);
    } else {
        printf("error in fork!");
    }

    printf("\n -------------\n统计结果是: %d\n",count);
    return 0;
}

The results of the operation are as follows:

========================
------------------------
- ----------------------
i am the child process, my process id is 13
I am a son
fpid = 0
----------- -
statistical results are: 1
i AM at The parent Process, my Process the above mentioned id iS 12
I was his father
FPID = 13
-------------
statistical results are: 1

Guess you like

Origin blog.csdn.net/sy_123a/article/details/108099888