create的各种形式(create的固定搭配)


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 = %dn",getpid());
       printf("child process pid = %dn",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 = %dn", 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 的值,父进程永远看到的是自己的那一份。

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

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 = %dn", 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。

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

C/C++Linux服务器开发/后台架构师【零声教育】-学习视频教程-腾讯课堂

【文章福利】:小编整理了一些个人觉得比较好的学习书籍、视频资料共享在群文件里面,有需要的可以自行添加哦!~点击加入(832218493需要自取)

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

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 = %dn", 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 = %dn", 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。

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

进程线程创建总图

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

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

但是内核态没有进程线程的概念,内核中只认 task_struct 结构,只要是 task_struct 结构就可以参与调度。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论