c进程学习日志

  

#include<unistd.h>
#include<sys/types.h>
#include<pwd.h>
#include<stdio.h>
int main(int argc,char **argv)
{
    pid_t my_pid,parent_pid;
    pid_t te ;
    my_pid=getpid();
    parent_pid=getppid();
    printf("mypid is [%d]\t parentid is [%d]\n",my_pid,parent_pid);
    te =  fork();
    printf("*********begin*************\n");
    my_pid=getpid();
    parent_pid=getppid();
    printf("mypid is [%d]\t parentid is [%d]\t te is %d\n",my_pid,parent_pid,te);
    printf("*********end*************\n");
#if 0    
    te =  fork();
    my_pid=getpid(); 
    parent_pid=getppid(); 
    printf("mypid is [%d]\t parentid is [%d]\t te is %d\n",my_pid,parent_pid,te);
    getchar();
#endif

 

RESULT:

mypid is [1705] parentid is [1521]
*********begin*************
mypid is [1705] parentid is [1521] te is 1706
*********end*************
root@ubuntu:/usr/lsrcc/tempUserFun# *********begin*************
mypid is [1706] parentid is [1] te is 0
*********end*************

结果分析:调用fork一次,fork返回2次,一个是父进程,一个是子进程。

#include<unistd.h>
#include<sys/types.h>
#include<pwd.h>
#include<stdio.h>
int main(int argc,char **argv)
{
    pid_t my_pid,parent_pid;
    pid_t te ;
    my_pid=getpid();
    parent_pid=getppid();
    printf("mypid is [%d]\t parentid is [%d]\n",my_pid,parent_pid);
    te =  fork();
    printf("*********begin*************\n");
    my_pid=getpid();
    parent_pid=getppid();
    printf("mypid is [%d]\t parentid is [%d]\t te is %d\n",my_pid,parent_pid,te);
    printf("*********end*************\n");
#if 1    

    printf("*********2THbegin*************\n");
    te =  fork();
    my_pid=getpid(); 
    parent_pid=getppid(); 
    printf("mypid is [%d]\t parentid is [%d]\t te is %d\n",my_pid,parent_pid,te);

    printf("********2TH*end*************\n");
    getchar();
#endif








}

RESULT:

*********begin*************
mypid is [1801] parentid is [1521] te is 1802
*********end*************//第一次fork父进程
*********2THbegin*************
mypid is [1801] parentid is [1521] te is 1803//第2次fork的父进程(在第一次fork的父进程基础上)
********2TH*end*************
mypid is [1803] parentid is [1801] te is 0//第2次fork的子进程(在第一次fork的父进程基础上)
********2TH*end*************
*********begin*************
mypid is [1802] parentid is [1801] te is 0
*********end*************//第1次fork的子进程
*********2THbegin*************
mypid is [1802] parentid is [1801] te is 1804//第2次fork的父进程(在第2次fork的父进程基础上)
********2TH*end*************
mypid is [1804] parentid is [1802] te is 0//第2次fork的子进程(在第2次fork的父进程基础上)
********2TH*end*************

系统运行结束后有6个进程。

#include<unistd.h>
#include<sys/types.h>
#include<pwd.h>
#include<stdio.h>
int main(int argc,char **argv)
{
    pid_t my_pid,parent_pid;
    pid_t te ;
    static int count = 0;
    my_pid=getpid();
    parent_pid = getppid();
    int i = 0;
    const char *p = "/usr/lsrcc/dMF/main";
    for(i = 0;i < 2;++i){
        te =  fork();
        if(te < 0){
            printf("fork err!\n");
            return -1;
        }
        if(te > 0){

            printf("i is %i \nmypid is [%d]\t parentid is [%d]\t te is %d\n",i,my_pid,parent_pid,te);
            printf("this is PARENT\n");
            count++;
        }
        else if(0 == te){
            printf("i is %i\nmypid is [%d]\t parentid is [%d]\t te is %d\n",i,my_pid,parent_pid,te);
            printf("this is CHILD\n");
            count++;
            execlp(p,NULL);
        }

    }
    printf("count is %d\n",count);

}

RESULT:

i is 0
mypid is [2798] parentid is [2361] te is 2799
this is PARENT
i is 1
mypid is [2798] parentid is [2361] te is 2800
this is PARENT
count is 2
root@ubuntu:/usr/lsrcc/tempUserFun# i is 1
i is 0
mypid is [2798] parentid is [2361] te is 0
this is CHILD
This is mytool1 print hello
mypid is [2798] parentid is [2361] te is 0
this is CHILD
This is mytool1 print hello
This is mytool2 print hello
This is mytool2 print hello

//主要提供一个子进程调用的例子

 

转载于:https://www.cnblogs.com/ashen/p/4493416.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值