您现在的位置是:亿华云 > IT科技

用户态进程/线程的创建 Fork/vfork/Pthread_Create

亿华云2025-10-03 02:46:05【IT科技】6人已围观

简介forkfork 函数创建子进程成功后,父进程返回子进程的 pid,子进程返回0。具体描述如下:fork返回值为-1, 代表创建子进程失败 fork返回值为0,代表子进程创建成功,这个分

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 结构就可以参与调度。关于内核态的任务创建,我们下文见。

云服务器

很赞哦!(5)