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 = ;
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进程学习日志的更多相关文章
- Cortex-M3学习日志(五) -- DAC实验
终于逮了个忙里偷闲的机会,就再学一下LPC1768的外围功能吧,循序渐进是学习的基本规则,也许LPC1768的DAC与8位单片机16位单片机里面集成的DAC操作类似,但是既然这是懒猫的学习日志,就顺便 ...
- Linux学习日志——基本指令②
文章目录 Linux学习日志--基本指令② 前言 touch cp (copy) mv (move) rm vim 输出重定向(> 或 >>) cat df(disk free) f ...
- GRE学习日志
发现开博客园真的很有督促作用,今天也顺便开个GRE学习日志吧 2015-02-09:单词 2015-02-10:单词 2015-02-11:单词 2015-03-02:阅读 2015-03-04:阅读 ...
- Cortex-M3学习日志(六) -- ADC实验
上一次简单的总结了一下DAC方面的知识,好吧,这次再来总结一下ADC方面的东东.ADC即Analog-to-Digital Converter的缩写,指模/数转换器或者模拟/数字转换器.现实世界是由模 ...
- webpack2学习日志
webpack说容易也容易,说难也难,主要还是看个人,想学到什么样的程度,很多公司可能要求仅仅是会用就行,但是也有一些公司要求比较高,要懂一些底层的原理,所以还是要花一些时间的,看个人需求.这篇仅仅是 ...
- javascript学习日志:前言
javascript学习日志系列的所有博客,主要理论依据是<javascript权威指南>(犀牛书第6版)以及<javascript高级程序设计第三版>(红色书),目前js行业 ...
- MobileForm控件的使用方式-用.NET(C#)开发APP的学习日志
今天继续Smobiler开发APP的学习日志,这次是做一个title.toolbar.侧边栏三种效果 样式一 一. Toolbar 1. 目标样式 我们要实现上图中的效果 ...
- 我的游戏学习日志3——三国志GBA
我的游戏学习日志3——三国志GBA 三国志GBA由日本光荣公司1991~1995所推出<三国志>系列游戏,该作是光荣在GBA上推出的<三国志>系列作品的第一款.本游戏登场武将总 ...
- 【转】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新特性
随机推荐
- css3背景渐变以及图片混合渲染模式(二)
http://avnpc.com/pages/photoshop-layer-blending-algorithm http://www.html5cn.org/forum.php?mod=viewt ...
- Pyhton学习——Day26
#多态:多态指的是一类事物有多种形态# import abc# class Animal(metaclass = abc.ABCMeta):# 同一类事物:动物# @abc.abstractclass ...
- ES modules
注意:这篇文章讲的是正经的es module规范 及浏览器的实现!webpack项目中es module会被parse成commonjs,和这个没大关系! 总结: ES模块加载的主要过程: 构造 —— ...
- vue 删除某个元素和删除某些元素
今天做项目使用前端vue框架,需要循环遍历去删除一些数组元素.开始思想局限,一直纠结如何去循环删除,犹豫循环删除数组值下标会发生变化,并不是一种好的方法. 方法一:使用forEach 和 splice ...
- Redis - Python操作Redis
目录 Python操作Redis 一. Redis安装和基本使用 二. Python操作Redis API使用 1.操作模式 2.连接池 3.Django配置Redis 4.操作 Python操作Re ...
- Django -查询数据库相关操作
一. 内置Admin 依赖APP: django.contrib.auth django.contrib.contenttypes django.contrib.messages django.con ...
- oracle 控制语句
PL输出语句 set serverout on; -- 开启PL的输出语句功能declare n number:=1; -- 声明一个number型的变量n,并赋值为1 v varchar2(20): ...
- RabbitMQ学习总结(4)——分发任务在多个工作者之间实例教程
一.Work Queues(using the Java Client) 走起 在第上一个教程中我们写程序从一个命名队列发送和接收消息.在这一次我们将创建一个工作队列,将用于分发耗时的任务在多个工 ...
- 我一不小心把ubuntu里的ps这样的命令删掉了,应该怎么重装呢
sudo apt-get --reinstall install procps
- BZOJ——1787: [Ahoi2008]Meet 紧急集合
http://www.lydsy.com/JudgeOnline/problem.php?id=1787 题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...