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

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。