#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. HDU 1028 Ignatius and the Princess III(母函数整数拆分)

    链接:传送门 题意:一个数n有多少种拆分方法 思路:典型母函数在整数拆分上的应用 /********************************************************** ...

  2. 小白都能看懂的Linux系统下安装配置Zabbix

    实验环境: 操作系统:Centos 7.6 服务器ip:192.168.10.100 运行用户:root 网络环境:Internet Zabbix是一个基于web界面的提供分布式系统监控及网络功能的企 ...

  3. SUSE Linux Enterprise 11 SP4系统安装过程 字符界面

    首先开启虚拟机之后显示这个界面: (1) 进入之后显示下面界面,点击Installation安装. (2)进入Welcome界面,选择语言,默认Einglish(US)不需要改动:点击I Agree ...

  4. [转]Python常用字符串

    转自:http://blog.csdn.net/daemonpei/article/details/6325762 字符串相关操作: + :string1+string2 #联接字符串,将后一个串链接 ...

  5. jquery在文本框之后添加红*

    var addHtml="<span class='text_red'>*</span>";function req(re){ if(re.parent(& ...

  6. HDU 4165

    一块药看成括号配对就行了.很明显的直接求卡特兰数. 今晚看了HDU 3240的题,有一点思路,但无情的TLE.想不到什么好方法了,看了别人的解答,哇...简直是天才的做法啊....留到星期六自己思考一 ...

  7. Unity学习笔记 之 发射小球碰撞物体的代码记录

    绑定在摄像机上的脚本 using UnityEngine; using System.Collections; public class abc : MonoBehaviour { //设置移动速度 ...

  8. leetcode题解||ZigZag Conversion问题

    problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...

  9. _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)

    博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...

  10. Google Maps API 将开始收费

    Google Maps API 将开始收费 一.总结 一句话总结:国外的话openstreetmap或许不错 国内的话就高德吧 二.Google Maps API 将开始收费 曾经免费的 Google ...