2021-03-21 进程内容学习

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




int main()
{

        pid_t pid;

//      pid_t fork();

        pid = getpid();

        printf("my pid is : %d\n",pid);


        while(1);
        return 0;
}
~                                                                                                     
~      

获取进程id

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




int main()
{

        pid_t pid;

        pid_t pid2;
//      pid_t fork();

        pid = getpid();

        printf("before fork  pid is  %d\n", pid);

        fork();
        pid2 = getpid();

        printf("after fork  pid is  %d\n", pid2);
        if(pid==pid2){

                printf("it is father print, current pid is %d\n",getpid());

        }
        else{

                printf("it is child print, current pid  is %d \n",getpid());
        }


        return 0;
}
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                                                                                                     
~                                                                                                     
~            

fork的一些基本用法

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



int main()
{

        pid_t pid;

//      pid_t fork();

        int cnt = 0;



        pid = vfork();

        if(pid > 0){

                while(1){

                        printf("cnt = %d \n",cnt);

                        printf("this is father print , pid =%d\n",getpid());

                        sleep(1);
                        }

        }

        else if(pid  == 0){
                while(1){
                        printf("it is child print, current pid  is %d \n",getpid());
                        sleep(1);
                        cnt++;
                        if(cnt == 5){
                                exit(0);
                        }
                }

        }



    
        return 0;
}
~                                                                                                     
~     

vfork 的一些使用`

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



int main()
{

        pid_t pid;

//      pid_t fork();

        int cnt = 0;
        int status = 10;


        pid = fork();

        if(pid > 0){
                wait(&status);
                printf("child quit , child status is %d",WEXITSTATUS(status));
                while(1){

                        printf("cnt = %d \n",cnt);

                        printf("this is father print , pid =%d\n",getpid());

                        sleep(1);
                        }

        }

        else if(pid  == 0){
                while(1){
                        printf("it is child print, current pid  is %d \n",getpid());
                        sleep(1);
                        cnt++;
                        if(cnt == 5){
                                exit(0);
                        }
                }

        }




        return 0;
}
~                                                                                                     
~     

引入wait 使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值