#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. Java语言特点与学习

    Java语言是一款面向对象的一款高级语言是由Sun Microsystems公司(现已被oracle公司收购).由James Gosling和同事们共同研发,并在1995年正式推出,据oracle官方 ...

  2. 嵌入式平台选择:树莓派 or BeagleBone Black(BBB)

    原文链接: Michael Leonard 翻译: 极客范- 小道空空 译文链接: http://www.geekfan.net/5246/ 嵌入式平台选择:树莓派 or BeagleBone Bla ...

  3. UVA401-Palindromes(紫书例题3.3)

    A regular palindrome is a string of numbers or letters that is the same forward as backward. For exa ...

  4. [读书笔记] Python 数据分析 (八)画图和数据可视化

    ipython3 --pyplot pyplot: matplotlib 画图的交互使用环境

  5. Java并发和多线程4:使用通用同步工具CountDownLatch实现线程等待

    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown ...

  6. 【codeforces 452D】Washer, Dryer, Folder

    [题目链接]:http://codeforces.com/problemset/problem/452/D [题意] 洗衣服有3个步骤,洗,干,叠; 有对应的3种洗衣机,分别有n1,n2,n3台,然后 ...

  7. SQL SERVER-in,between,like

    and 1 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式 ( 可以使用正则表达式) select * from [User] where UserName like '%r' -- 以 ...

  8. 洛谷——P1351 联合权值

    https://www.luogu.org/problem/show?pid=1351 题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i , ...

  9. BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...

  10. HDU 3723

    把向上看成+1,向下看成-1.可以知道符合卡特兰数的一般解释了.记作Can(i) 中间平过的即是0.亦即是C(n,2*i),i表示向上的数. 于是总的就是sum(C(n,2*i)*Can(i)),i从 ...