进程(二)fork函数

使用fork函数创建一个进程

pid_t fork(void);

fork函数调用成功,返回两次
返回值为0,     代表当前进程是子进程
返回值非负数,  代表当前进程为父进程

调用失败,返回-1

getpid()

进程标识符 pid

每个进程都有一个唯一的非负整数表示唯一ID
叫做pid,类似与我们的身份证
pid=0;成为交换进程,表示进程调度
pid=1;表示init进程 作用系统的初始化

getpid获得当前进程pid号
getppid获得父进程pid号

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

int main()
{
        pid_t pid;
        pid = getpid();
        printf("my pid is %d\n",pid);
        while(1);
        return 0;
}

 fork()函数使用

1.查看父子进程的pid

fork()函数调用成功 返回两次

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

int main()
{
        pid_t pid;
        pid = getpid();
        fork();
        printf("my pid is %d,current pro id:%d\n",pid,getpid());
        return 0;
}

运行结果为:

2.判断fork函数返回两次是否相等 查看父子进程pid

forkusetest3.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
        pid_t pid;
        pid_t pid2;
        pid = getpid();
        printf("before fork:pid = %d\n",pid);
        fork();
        pid2 = getpid();
        if(pid == pid2)
        {
                printf("this is father print,father pid = %d\n",getpid());
        }
        else
        {
                printf("this is child print,child pid = %d\n",getpid());
        }
        return 0;
}

运行结果为:

3.实现fork()函数返回值的多少来判断父子进程

forkusetest4.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
        pid_t pid;
        printf("father pid is %d\n",getpid());
        pid = fork();
        if(pid > 0)
        {
                printf("this is father print,father pid = %d\n",getpid());

        }
        else if(pid == 0)
        {
                printf("this is child print,child pid = %d\n",getpid());
        }
        return 0;
}
~                    

运行结果为:

4.查看父子进程返回值为多少

创建出来了父进程 pid号是 45613 返回值retpid是45614 是子进程的pid
创建了子进程 pid号是 45614 retpid是 0

通过运行结果可以看出 父进程的pid是45613 通过父进程创建出来子进程的pid号
子进程的pid号是父进程创建出来的 并且子进程的fork的返回值为0

 

vi forkusetest5.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
        pid_t pid;
        pid_t pid2;
        pid_t retpid;
        pid = getpid();
        printf("before fork:pid = %d\n",pid);
        retpid = fork();
        pid2 = getpid();
        printf("after fork:pid = %d\n",pid2);
        if(pid == pid2)
        {
                printf("this is father print pid = %d,retpid = %d\n",getpid(),retpid);
        }
        else
        {
                printf("this is child print pid = %d,retpid = %d\n",getpid(),retpid);
        }
        return 0;
}

运行结果为

5.进程中fork 对堆栈BSS 代码段 数据段的解析

使用的是把所有的文件都从父进程全拷贝到子进程    后期是写实拷贝  copy on write

而新的是如果子进程不改变data的大小 他们两个进行data的数据段共享
只有子进程对data动手脚的时候才会采用在子进程的内存空间里面备份一个data

forkusetest6.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
        pid_t pid;
        int data = 10;
        printf("father pid is %d\n",getpid());
        pid = fork();
        if(pid > 0)
        {
                printf("this is father print,father pid = %d\n",getpid());
        }
        else if(pid == 0)
        {
                printf("this is child print,child pid = %d\n",getpid());
                data = data + 100;
        }
        printf("data = %d\n",data);
        return 0;
}

运行结果为:

fork创建一个子进程的一般目的

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

int main()
{
        pid_t pid;
        int data = 10;
        while(1)
        {
                printf("please input a data\n");
                scanf("%d",&data);
                if(data==1)
                {
                        pid = fork();

                        if(pid > 0)
                        {

                        }
                        else if(pid == 0)
                        {
                                while(1){
                                        printf("do net request,pid = %d\n",getpid());
                                        sleep(3);
                                }
                        }

                }else{
                        printf("wait , do nothing\n");
                }
        }
        return 0;
}

执行结果为: 

./forktest 

please input a data
1    //第一次创建
please input a data
do net request,pid = 45916
2
wait , do nothing
please input a data
3
wait , do nothing  //等待客户端连入
please input a data
4do net request,pid = 45916

wait , do nothing
please input a data
1    //第二次创建
please input a data
do net request,pid = 45917
1    //第三次创建
please input a data
do net request,pid = 45918
1    //第四次创建
please input a data
do net request,pid = 45919
1    //第五次创建
please input a data
do net request,pid = 45920//全在等待客户端连入
do net request,pid = 45916
do net request,pid = 45917
do net request,pid = 45918
2
wait , do nothing
please input a data
do net request,pid = 45919
do net request,pid = 45920
do net request,pid = 45916
do net request,pid = 45917

猜你喜欢

转载自blog.csdn.net/weixin_46016743/article/details/115001761
今日推荐