#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 = ;
my_pid=getpid();
parent_pid = getppid();
int i = ;
const char *p = "/usr/lsrcc/dMF/main";
for(i = ;i < ;++i){
te = fork();
if(te < ){
printf("fork err!\n");
return -;
}
if(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 PARENT\n");
count++;
}
else if( == 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进程学习日志的更多相关文章

  1. Cortex-M3学习日志(五) -- DAC实验

    终于逮了个忙里偷闲的机会,就再学一下LPC1768的外围功能吧,循序渐进是学习的基本规则,也许LPC1768的DAC与8位单片机16位单片机里面集成的DAC操作类似,但是既然这是懒猫的学习日志,就顺便 ...

  2. Linux学习日志——基本指令②

    文章目录 Linux学习日志--基本指令② 前言 touch cp (copy) mv (move) rm vim 输出重定向(> 或 >>) cat df(disk free) f ...

  3. GRE学习日志

    发现开博客园真的很有督促作用,今天也顺便开个GRE学习日志吧 2015-02-09:单词 2015-02-10:单词 2015-02-11:单词 2015-03-02:阅读 2015-03-04:阅读 ...

  4. Cortex-M3学习日志(六) -- ADC实验

    上一次简单的总结了一下DAC方面的知识,好吧,这次再来总结一下ADC方面的东东.ADC即Analog-to-Digital Converter的缩写,指模/数转换器或者模拟/数字转换器.现实世界是由模 ...

  5. webpack2学习日志

    webpack说容易也容易,说难也难,主要还是看个人,想学到什么样的程度,很多公司可能要求仅仅是会用就行,但是也有一些公司要求比较高,要懂一些底层的原理,所以还是要花一些时间的,看个人需求.这篇仅仅是 ...

  6. javascript学习日志:前言

    javascript学习日志系列的所有博客,主要理论依据是<javascript权威指南>(犀牛书第6版)以及<javascript高级程序设计第三版>(红色书),目前js行业 ...

  7. MobileForm控件的使用方式-用.NET(C#)开发APP的学习日志

    今天继续Smobiler开发APP的学习日志,这次是做一个title.toolbar.侧边栏三种效果 样式一 一.          Toolbar 1.       目标样式 我们要实现上图中的效果 ...

  8. 我的游戏学习日志3——三国志GBA

    我的游戏学习日志3——三国志GBA 三国志GBA由日本光荣公司1991~1995所推出<三国志>系列游戏,该作是光荣在GBA上推出的<三国志>系列作品的第一款.本游戏登场武将总 ...

  9. 【转】Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

    [转]Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性 Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性

随机推荐

  1. 使用C++部署Keras或TensorFlow模型

    本文介绍如何在C++环境中部署Keras或TensorFlow模型. 一.对于Keras, 第一步,使用Keras搭建.训练.保存模型. model.save('./your_keras_model. ...

  2. Jquery 过滤器(first,last,not,even,odd)的使用和区别

    Jquery 过滤器主要有first,last,not,even,odd等等,它们分别表示: 代码如下: $(function(){ $("#menu li:first").cli ...

  3. github删除项目or仓库

    1. 登录 github (要注册账号) 2. 登录后点击右上侧头像,选择 Your profile . 3. 选择Repositories,可以查看已有的库,选择要删除的库进入. 4. 选择Sett ...

  4. BZOJ 3672 [NOI2014]购票 (凸优化+树剖/树分治)

    题目大意: 略 题面传送门 怎么看也是一道$duliu$题= = 先推式子,设$dp[x]$表示到达$x$点到达1节点的最小花费 设$y$是$x$的一个祖先,则$dp[x]=min(dp[y]+(di ...

  5. CF449D Jzzhu and Numbers (状压DP+容斥)

    题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...

  6. 实战:vue项目中导入swiper插件

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4.常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: n ...

  7. js中的json操作

    js中的json操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScr ...

  8. 在Action中获取servlet API

    Struts2的Action组件是不依赖servlet API 的.那么当你在action中的业务需要处理HttpServletRequest和HttpServletResponse的时候(比如要对响 ...

  9. POJ——T2421 Constructing Roads

    http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24132   ...

  10. Spring boot 使用@Value注入属性

    Spring boot 使用@Value注入属性 学习了:http://blog.csdn.net/hry2015/article/details/72353994 如果启动的时候报错: spring ...