美文网首页
linux应用程序进程操作(fork vfork)

linux应用程序进程操作(fork vfork)

作者: 嵌入式工作 | 来源:发表于2018-11-07 11:16 被阅读0次

1fork ,fork的子进程和父进程同时运行,运行顺序不确定

2 vfork先运行子进程,再运行父进程

3 fork 运行如下

test@ubuntu:~/test$ ./fort 
i am father :15503 
i am child :15504 
test@ubuntu:~/test$ 

fork程序

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

int main(void)
{
    pid_t child;
    /*创建子程序*/
    if((child=fork())==-1)
    {
        printf("fork error :%d \n",strerror(errno));
        exit(1);
        
    }else if( 0 == child)/*子进程*/
    {
        printf("i am child :%d \n",getpid());
        exit(0);
        
    }else 
    {
        printf("i am father :%d \n",getpid());
        exit(0);
        
    }

}

4.vfork,运行如下:

test@ubuntu:~/test$ gcc -o vf vfork_test.c 
test@ubuntu:~/test$ ./vf
child process start...
i am child 15481
i am father :15480 
test@ubuntu:~/test$ 

代码

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

int main(void)
{
    
    pid_t child;
    
    
    if((child = vfork())==-1)
    {
        printf("vfork error :%d \n",strerror(errno));
        exit(1);
        
    }else if(0==child)/*子进程优先运行*/
    {
        printf("child process start...\n");
        sleep(2);
        printf("i am child %d\n",getpid());
        exit(0);
    }else 
    {
        printf("i am father :%d \n",getpid());
        exit(0);
    }
    
}

相关文章

  • Linux中fork,vfork和clone详解(区别与联系)

    fork,vfork,cloneUnix标准的复制进程的系统调用时fork(即分叉),但是Linux,BSD等操作...

  • linux应用程序进程操作(fork vfork)

    1fork ,fork的子进程和父进程同时运行,运行顺序不确定 2 vfork先运行子进程,再运行父进程 3 fo...

  • Linux中fork,vfork和clone详解(区别与联系)

    fork,vfork,clone Unix标准的复制进程的系统调用时fork(即分叉),但是Linux,BSD等操...

  • Linux--fork与wait

    fork与exec 在Linux中,都是通过fork与vfork系统调用来创建子进程,并且在fork完之后,通常会...

  • 四、进程操作

    创建、结束进程创建进程使用fork系统调用,结束进程使用exit。理解fork调用的意义,理解vfork调用的意义...

  • linux-08-进程管理2

    今天:进程结束 -fork() /exit退出进程/wait()父进程等待子进程/vfork()Unix/Linu...

  • fork与vfork的区别2020-05-04

    1.数据共享方面: fork ():子进程拷贝父进程的数据段,代码段 vfork( ):子进程与父进程共享...

  • 26.进程的诞生和消亡

    进程的诞生 (1)进程0和进程1(2)fork(3)vfork 进程的消亡 (1)正常终止和异常终止(2)进程在运...

  • Linux进程笔记

    进程创建方式 1 fork函数 2 vfork 3 使用exec函数执行一段程序(了解) 进程的状态就绪/运行/阻...

  • python 杂记

    进程 fork()函数Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊。普通的函数调用,调用...

网友评论

      本文标题:linux应用程序进程操作(fork vfork)

      本文链接:https://www.haomeiwen.com/subject/isvmxqtx.html