Linux中fork()函数的使用

函数功能

fork()函数的详细介绍可以用man命令来查看,下面截取一部分重要的介绍。

#include <sys/types.h>      
#include <unistd.h>

pid_t fork(void);

fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process.

The child process and the parent process run in separate memory spaces. At the time of fork() both memory spaces have the same content. Memory writes, file mappings (mmap(2)), and unmap‐pings (munmap(2)) performed by one of the processes do not affect the other.

RETURN VALUE
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

NOTES
Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent’s page tables, and to create a unique task structure for the child.

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

int main(){
    pid_t pid;
    pid = fork();
    if(pid == 0){  /* child */
        pid = getpid();
        printf("child's pid:%d\r\n",pid);
        exit(0);
    }
    else{ /* parent */
        pid = getpid();
        printf("parent's pid:%d\r\n",pid);
        wait(NULL);                /* Wait for child */
        exit(0);
    }
    return 0;
}

上面的exit()函数是退出进程,exit(0)表示正常退出。创建的子进程执行完之后会自动退出,因此if对应的else可以不要。
wait()函数一般都要和fork()函数配套使用,作用等待子进程退出。

一次调用,两次返回

fork()函数一次调用,两次返回,通过返回值来判断返回的是那个进程,因此不同的执行代码应该放到不同的进程中,如果不做区分,fork()后面的代码会被执行两次,例如下面的例子。

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

int main(){
    fork();
    printf("Hello World!\r\n");
    return 0;
}

上面的代码只有一次打印,但是调用fork()的原因,会打印两次。
下面的代码会打印四次:

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

int main(){
    fork();
    fork();
    printf("Hello World!\r\n");
    return 0;
}

明确创建关系

由于fork()函数会两次返回,因此在创建新进程的时候一定明确哪个是父进程,子进程由哪个进程创建,特别是存在多个进程的时候。例如下面的代码中两个子进程都是由同一个父进程创建的:

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

int main(){
    pid_t pid;
    pid = fork();
    if(pid == 0){/*child*/
        /*code*/
        printf("child 1 pid:%d\r\n",getpid());
        exit(0);
    }
    pid = fork();
    if(pid == 0){/*child*/
        /*code*/
        printf("child 2 pid:%d\r\n",getpid());
        exit(0);
    }
    while(pid = wait(NULL),pid != -1){} /*wait for child*/
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忆昔z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值