码迷,mamicode.com
首页 > 系统相关 > 详细

c进程学习日志

时间:2015-05-11 01:15:39      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

  

#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

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

 

c进程学习日志

标签:

原文地址:http://www.cnblogs.com/ashen/p/4493416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!