linux下的父子进程的验证代码
来源:互联网 发布:android 数据库路径 编辑:程序博客网 时间:2024/06/04 18:00
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
pid_t pid = fork(); //调用fork函数,这是有两个返回值的特殊函数,可以将程序分成两个进程
if (-1 == pid)
{
perror("fork");
return 1;
}
if (pid == 0) //pid 为0, 进入子进程
printf("a = %d", a);
}
else //进入父进程
{
return 0;
#include<sys/types.h>
#include<unistd.h>
int main()
{
a = 10;
printf("main pid = %d\n", getpid());pid_t pid = fork(); //调用fork函数,这是有两个返回值的特殊函数,可以将程序分成两个进程
if (-1 == pid)
{
perror("fork");
return 1;
}
if (pid == 0) //pid 为0, 进入子进程
{
a = 11;
printf("chlid pid = %d ppid = %d\n", getpid(), getppid());printf("a = %d", a);
}
else //进入父进程
{
printf("parent return value = %d pid = %d\n", pid, getpid());
printf ("a = %d", a);
}return 0;
}
值得注意的是,子进程和父进程是相互独立的地址空间,所以互相没有影响,但是父进程的return pid 的值为子进程的pid。但是,子进程的输出的a为11,父进程的为10,这说明,两个进程是互不影响的。
阅读全文
0 0
- linux下的父子进程的验证代码
- Linux下的管道和父子进程的应用
- linux下父子进程间的通信——管道
- 寻根1:unix/linux下的父子进程交互
- 父子进程共享模式下的epoll_wait
- 神奇的linux:父子进程管道通讯
- linux之父子进程的输出
- Linux下父子进程拾遗
- 有关Linux下父子进程之间的几个思考与结论
- linux系统下 fork()系统调用: 关于父子进程缓存问题的小坑
- 有关Linux下父子进程内容
- Linux下父子进程匿名管道通信
- Linux父子进程对键盘IO的管理 实验一则
- Linux 父子进程在没有同步时出现的问题
- Linux shell脚本中父子进程与变量的分析
- Linux shell脚本中父子进程与变量的分析
- Linux shell脚本中父子进程与变量的分析
- Linux 基于父子进程的TCP服务器应用编程
- 嵌入式笔试题(3)
- windows phone listbox的点击事件
- [bzoj1293][SCOI2009]生日礼物 单调队列优化dp
- 选择排序
- ZOJ--1003:Crashing_Balloon(因子分解)
- linux下的父子进程的验证代码
- 训练赛---咏叹
- Maven快速入门
- 旧版漏洞 Bug 被发现可致 Win7/8.1 “崩溃”
- MySQL入门--删除数据库、修改数据库编码集、显示数据库创建信息
- URL与资源(http权威指南第二章)
- 【转】Windows Phone在隔离存储里存取图片文件
- 18. Python开发--函数杂货铺(内置函数,递归)
- 二叉树