Linux process 2: process creation (fork, vfork), the difference between fork and vfork, process exit (exit)

Process creation (fork, vfork), the difference between fork and vfork, process exit (exit)

1. Use the fork function to create a process:

The fork function is successfully called and returns twice. The
return value is 0, which means that the current process is a child process. The
return value is a non-negative number, which means that the current process is the parent process. The
call fails and returns -1 //pid=fork();

#include <unistd.h>
Pid_t fork(void);

2. Get the current process ID number:

#include <sys/types.h>
#include <unistd.h>
pid_t pid;
pid=getpid();
printf("my pid :%d\n",pid);

Code demonstration demo2.c:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    
    
pid_t pid;
pid=getpid();
fork();//创建一个进程,两个进程在运行,返回两个pid
if(pid==getpid())
{
    
    
 printf("fujincheng\n");
}else
{
    
    
 printf("zijincheng\n");
}
printf("my pid :%d\n当前pid:%d\n",pid,getpid());
return 0;
}

operation result:
Insert picture description here
3. What happened to the creation process?

Fork: copy one copy, then execute two processes
and copy one copy of memory,Copy-on-write, The new space is allocated only when the child process moves, and
cannot share the same space: for example: a=10; a=a+10;//errors.
You can share the text segment: code segment

4. Summary of application scenarios and forks for creating new processes

The general purpose of Fork to create a child process:

A: The parent process wants the child process to copy itself, so that the parent and child processes execute different code segments at the same time.This is the most common in the network service process:
-----The parent process waits for the client's service request. When such a request arrives, the parent process calls fork to make the child process handle the request. The parent process continues to wait for the next service request to arrive.

B: A process needs to execute a different program. This pairshellIt is a common situation. under these circumstances,Exec is called immediately after the child process returns from fork

Demo code: network service process:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
    
    
int data;
pid_t pid;
//pid=getpid();
while(1)
{
    
    
 printf("请输入数据:\n");
 scanf("%d",&data);
 if(data==1)
 {
    
    
  pid=fork();
  if(pid>0)
  {
    
    
   printf("fujincheng\n");
  }else
  {
    
    
   printf("zijincheng\n");
   data+=100;//子进程也可以使用while(1)进行执行
  }
  printf("%d\n",data);
 }else{
    
    
  printf("do nothing\n");
 }
}
return 0;
}

Code implementation:
Every time 1 is entered, one more subprocess is created to complete the task.

operation result:
Insert picture description here
5.vfork creation process

The vfork function can also create a process. What is the difference with fork?

Key difference 1:
Vfork directlyUse parent process storage space, No copy
Key difference 2:
Vfork guaranteesChild process runs first, When the child process calls exit to launch, the parent process is executed.

Code demo:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
    
    
  int cnt=0;
  pid_t pid;
  pid=vfork();//创建子进程
  if(pid>0)
  {
    
    
   while(1){
    
    
   printf("fujincheng\n");
   sleep(1);
   }
  }else if(pid==0)
  {
    
    
   while(1){
    
    
   printf("ZIjincheng\n");
   sleep(1);
   cnt++;
   if(cnt==3)
   {
    
    
     exit(0);
   }
   }
  }
 return 0;
}

Code implementation: when cnt==3 in the child process, exit, and then execute the parent process

operation result:
Insert picture description here
6. Process exit

Normal exit:-------exit(0);-------0 is return status
A, main function calls return
B, process calls exit() and standard c library
C, process calls _exit() Or _Exit(), belongs to the system call

supplement:

A, the last thread of the process returns to
B, and the last thread calls pthread_exit

Exit abnormally:

A, call abort
B, when the process receives certain signals: ctrl+c
C, the last thread responds to the cancellation request

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108974316