前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用户态进程/线程的创建 fork/vfork/pthread_create

用户态进程/线程的创建 fork/vfork/pthread_create

作者头像
刘盼
发布2021-07-05 21:00:45
1.1K0
发布2021-07-05 21:00:45
举报

fork

fork 函数创建子进程成功后,父进程返回子进程的 pid,子进程返回0。具体描述如下:

  • fork返回值为-1, 代表创建子进程失败
  • fork返回值为0,代表子进程创建成功,这个分支是子进程的运行逻辑
  • fork返回值大于0,这个分支是父进程的运行逻辑,并且返回值等于子进程的 pid

我们看下通过 fork 系统调用来创建子进程的例子:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
 
int main()
{
   pid_t pid = fork();
 
   if(pid == -1){
       printf("create child process failed!\n");
       return -1;
   }else if(pid == 0){
       printf("This is child process!\n");
   }else{
       printf("This is parent process!\n");
       printf("parent process pid = %d\n",getpid());
       printf("child process pid = %d\n",pid);
   }
 
   getchar();
 
   return 0;
}

运行结果:

$ ./a.out
This is parent process!
parent process pid = 25483
child process pid = 25484
This is child process!

从上面的运行结果来看,子进程的pid=25484, 父进程的pid=25483。

在前面介绍内存缺页异常的时候,提到写时复制 COW 是一种推迟或者避免复制数据的技术,主要用在 fork 系统调用里,当执行 fork 创建新子进程时,内核不需要复制父进程的整个进程地址空间给子进程,而是让父进程和子进程共享同一个副本,只有写入时,数据才会被复制。我们用一个简单里的例子描述下:

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

int peter = 10;

int main()
{
  pid_t pid = fork();

  if(pid == -1){
      printf("create child process failed!\n");
      return -1;
  }else if(pid == 0){
      printf("This is child process, peter = %d!\n", peter);
      peter = 100;
      printf("After child process modify peter = %d\n", peter);
  }else{
      printf("This is parent process = %d!\n", peter);
  }

  getchar();

  return 0;
}

执行结果:

$ ./a.out
This is parent process = 10!
This is child process, peter = 10!
After child process modify peter = 100

从运行结果可以看到,不论子进程如何去修改 peter 的值,父进程永远看到的是自己的那一份。

vfork

接下来看下使用 vfork 来创建子进程:

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

int peter = 10;

int main()
{
  pid_t pid = vfork();

  if(pid == -1){
      printf("create child process failed!\n");
      return -1;
  }else if(pid == 0){
      printf("This is child process, peter = %d!\n", peter);
      peter = 100;
      printf("After child process modify peter = %d\n", peter);
      exit(0);
  }else{
      printf("This is parent process = %d!\n", peter);
  }

  getchar();

  return 0;
}

运行结果:

$ ./a.out
This is child process, peter = 10!
After child process modify peter = 100
This is parent process = 100!

从运行结果中可以看出,当子进程修改了 peter=100 之后,父进程中打印 peter 的值也是100。

pthread_create

现在我们知道了创建进程有两种方式:fork,vfork。那么创建线程呢?

线程的创建接口是用 pthread_create:

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

int peter = 10;

static pid_t gettid(void)
{
 return syscall(SYS_gettid);
}

static void* thread_call(void* arg)
{
 peter = 100;
 printf("create thread success!\n");
 printf("thread_call pid = %d, tid = %d, peter = %d\n", getpid(), gettid(), peter);
 return NULL;
}

int main()
{
 int ret;
 pthread_t thread;

 ret = pthread_create(&thread, NULL, thread_call, NULL);
 if(ret == -1)
     printf("create thread faild!\n");

 ret = pthread_join(thread, NULL);
 if(ret == -1)
     printf("pthread join failed!\n");

 printf("process pid = %d, tid = %d, peter = %d\n", getpid(), gettid(), peter);

 return ret;
}

运行结果:

$ ./a.out
create thread success!
thread_call pid = 9719, tid = 9720, peter = 100
process pid = 9719, tid = 9719, peter = 100

从上面的结果可以看出:进程和线程的 pid 都是相同的。当线程修改了 peter = 100 之后,父进程中打印 peter 的值也是100。

进程线程创建总图

上面介绍了用户态创建进程和线程的方式,以及各个方式的特点。关于其底层的实现本质,我们后面会详细讲解。这里先提供一下三者之间的关系,可见三者最终都会调用 do_fork 实现。

但是内核态没有进程线程的概念,内核中只认 task_struct 结构,只要是 task_struct 结构就可以参与调度。关于内核态的任务创建,我们下文见。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 人人都是极客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • fork
  • vfork
  • pthread_create
  • 进程线程创建总图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档