笔记二:进程间的通信(fork、孤儿进程,僵死进程等)
以下两段fork函数简单的使用
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "stdlib.h"
int main()
{
pid_t pid;
pid=fork();
printf("hello linux\n");
usleep(200); //200微秒
printf("11111111111\n");
while(1);
return 0;
}
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "stdlib.h"
int main()
{
pid_t pid;
pid=fork();
if(pid >0)
{
printf("parent process run\n");
}
if(pid ==0)
{
printf("child process run\n");
}
while(1);
return 0;
}
什么是僵尸进程?什么是孤儿进程?
/*僵尸进程 查看进程pid状态等消息 可以终端使用命令ps aux或者ps axj*/
#include "signal.h"
#include "sys/types.h"
#include "stdio.h"
int main()
{
pid_t pid;
pid = fork();
if(pid < 0)
{
printf("创建进程失败\n");
return -1;
}
/*对于父进程首先是睡眠(8秒)S状态,8秒后进入R(运行)状态
对于子进程就变成了T状态;
*/
if(pid > 0)
{
sleep(8);//睡眠状态;
/*如果设置的waitpid设置为waitpid(pid,NULL,0)为阻塞,那么将不会产生僵尸进程,父进程会为其回收*/
if(waitpid(pid,NULL,WNOHANG) == 0)//等待子进程结束;可以设置为非阻塞(WNOHANG);
{
kill(pid,9);//杀死子进程,父进程没有结束,子进程将变成Z状态,因为父进程没有给子进程回收资源,子进程将变为僵尸进程;
}
while(1);
}
if(pid == 0)
{
printf("raise function before\n");
raise(SIGTSTP);//暂停程序,相当于ctrl+z; T状态
printf("raise function after\n");
exit(0);
}
}
以下为exit()和_exit()基本使用
#include "stdio.h"
#include "unistd.h"
#include "stdlib.h"
int main()
{
printf("hello linux");
// fflush(stdout);
//_exit(0);
exit(0);
printf("1111111111111");
return 0;
}
#include <sys/types.h>
pid_t wait(int *status)
status 是一个整型指针,指向的对象用来保存子进程退出时的状态。
以下是关于wait函数的使用
#include "sys/wait.h"
#include "sys/types.h"
#include "stdio.h"
int main()
{
printf("wait before\n");
wait(NULL);
printf("wait after\n");
return 0;
}
#include "sys/types.h"
#include "stdio.h"
#include "unistd.h"
int main()
{
pid_t pid;
pid=fork();
if(pid <0 )
{
printf("creat process failure\n");
return -1;
}
if(pid ==0)//child process
{
printf("this is child process\n") ;
while(1);
}
if(pid >0) //parent process
{
printf("this is parent process\n");
printf("parent process:wait before\n");
wait(NULL);
printf("parent process:wait after\n");
while(1);
}
return 0;
}
解析:
#include "sys/types.h"
#include "sys/wait.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
pid_t pid;
int ret;
pid = fork();
if(pid <0)
{
printf("creat process failure\n");
return -1;
}
if(pid == 0) //child process1
{
printf("this is a child process,pid=%d\n",getpid());
sleep(10);
exit(1);
}
if(pid >0)
{
pid_t pid1;
pid1=fork();
if(pid1<0)
{
printf("creat child process1 failure\n");
exit(2);
}
if(pid1 ==0)
{
printf("this is a child process1 run,pid=%d\n",getpid());
sleep(20);
exit(3);
}
printf("this is a parent process\n");
printf("wait before\n");
ret=wait(NULL);
printf("wait after,first wait ret=%d\n",ret);
ret=wait(NULL);
printf("second wait after,ret=%d\n",ret);
while(1);
return 0;
}
同wait
例子:
#include "sys/types.h"
#include "sys/wait.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
pid_t pid;
int ret;
int status;
pid = fork();
if(pid <0)
{
printf("creat process failure\n");
return -1;
}
if(pid == 0) //child process1
{
printf("this is a child process,pid=%d\n",getpid());
sleep(10);
exit(1);
}
if(pid >0)
{
pid_t pid1;
pid1=fork();
if(pid1<0)
{
printf("creat child process1 failure\n");
exit(2);
}
if(pid1 ==0)
{
printf("this is a child process1 run,pid=%d\n",getpid());
sleep(20);
exit(3);
}
printf("this is a parent process\n");
printf("wait before\n");
ret=waitpid(pid1,&status,0);
printf("wait after,first wait ret=%d,status=%d\n",ret,WEXITSTATUS(status));
// ret=wait(&status);
// printf("second wait after,ret=%d,status=%d\n",ret,WEXITSTATUS(status));
while(1);
return 0;
}
}
伪代码:
execl例子
#include "stdio.h"
#include "unistd.h"
int main()
{
printf("exec before\n");
execl("./xxx","./xxx",NULL);
printf("exec after\n");
return 0;
}
execv例子
#include "stdio.h"
#include "unistd.h"
int main()
{
char *buf[]={"./child/xxx",NULL};
printf("exec before\n");
execv("./child/xxx",buf);
printf("exec after\n");
return 0;
}
execvp例子
#include "stdio.h"
#include "unistd.h"
int main()
{
char *buf[]={"xxx",NULL};
printf("exec before\n");
execvp("xxx",buf);
printf("exec after\n");
return 0;
}
笔记二:进程间的通信(fork、孤儿进程,僵死进程等)的更多相关文章
- c 进程间的通信
在上篇讲解了如何创建和调用进程 c 进程和系统调用 这篇文章就专门讲讲进程通信的问题 先来看一段下边的代码,这段代码的作用是根据关键字调用一个Python程序来检索RSS源,然后打开那个URL #in ...
- Python网络编程(进程池、进程间的通信)
线程池的原理: 线程池是预先创建线程的一种技术.线程池在还没有任务到来之前, 创建一定数量的线程,放入空闲队列中.这些线程都是处于睡眠状态, 即均为启动,不消 ...
- Linux进程间的通信
一.管道 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: A. 管道是半双工的,数据只能向一个方向流动: B. 需要双工通信时,需要建立起两个管道: C. 只能用于父子进程或者兄弟 ...
- [Socket]Socket进程间的通信
转自:http://blog.csdn.net/giantpoplar/article/details/47657303 前面说到的进程间的通信,所通信的进程都是在同一台计算机上的,而使用socket ...
- c++ 网络编程(三) LINUX/windows 进程间的通信原理与实现代码 基于多进程的服务端实现
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9613027.html 锲子:进程与线程是什么,他们的区别在哪里: 1 进程概念 进程是程序的一 ...
- 进程间的通信—套接字(socket)
前面说到的进程间的通信,所通信的进程都是在同一台计算机上的,而使用socket进行通信的进程可以是同一台计算机的进程,也是可以是通过网络连接起来的不同计算机上的进程.通常我们使用socket进行网 ...
- python全栈开发day32-进程创建,进程同步,进程间的通信,进程池
一.内容总结 1.进程创建 1) Process:两种创建一个新进程的方法: 1.实例化Process,通过args=(,)元组形式传参,2创建类继承Process,类初始化的时候传参数 2) p.j ...
- Android进程间的通信
1.概述:由于android系统中应用程序之间不能共享内存.因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一些.在android SDK中提供了4种用于跨进程通讯的方式.这4种方式正好对应于 ...
- Nginx学习——Nginx进程间的通信
nginx进程间的通信 进程间消息传递 共享内存 共享内存还是Linux下提供的最主要的进程间通信方式,它通过mmap和shmget系统调用在内存中创建了一块连续的线性地址空间,而通过munmap或者 ...
- Python 35 进程间的通信(IPC机制)、生产者消费者模型
一:进程间的通信(IPC):先进先出 管道:队列=管道+锁 from multiprocessing import Queue q=Queue(4) q.put(['first',],block=T ...
随机推荐
- 腾讯云等Linux环境下Redis安装配置
1.下载redis解压安装命令教程 https://www.cnblogs.com/hunanzp/p/12304622.html 2.配置远程连接 修改bind 127.0.0.0 为 bind ...
- Java开发词汇
Java基础常见英语词汇(70个) OO: object-oriented ,面向对象 OOP: object-oriented programming,面向对象编程 JDK:Java develop ...
- PHP 合并两个二维数组
思路:遍历二维数组,合并两个二维数组的值,赋值给新数组 function mergeArray($arr1,$arr2){ $newArr = []; foreach($arr1 as $v1){ f ...
- kong数据库postgre centos安装一条龙
安装版本可以参考kong.conf PostgreSQL版本,这里要求9.5以上 https://www.cnblogs.com/zhi-leaf/p/11432054.html 安装rpm文件 #y ...
- flask动态csv接口——编码问题
@xxx_blueprint.route("/file", methods=["GET"]) def group_trend(): def generate() ...
- SDK测试标准
测试分类 具体测试项 测试内容 测试方法 文档测试 接口清单 接口清单是否完整,正确,包含提供给开发者的协议所有字段的定义和解释 人工检查 更新说明 要说明新增,删除的接口定义 Demo示例 显示如何 ...
- sdut——4541:小志志和小峰峰的日常(取石子博弈模板题 4合1)
小志志和小峰峰的日常 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 小志志和小峰峰特别喜欢一起讨论一些很好玩的问题. ...
- mac快捷键和win10快捷键和mma快捷手册
不定期更新 来自知乎,b站等 mac下的快捷键 如果你mac接了个不一致的键盘,mac会让你检测,(按左ctrl右边的键,按右ctrl左边的键),之后会进行键位映射,这也太复杂了,我拒绝记录. com ...
- PicGo + Gitee(码云)实现markdown图床 (转载)
https://zhuanlan.zhihu.com/p/102594554 备忘录 我配置图床的时候参考的是这篇文章.我暂时使用的是这种方案. 因为考虑到有的文章要多平台发布,我建议你选择markd ...
- Windows 10 G 神州网信政府版
神州网信政府版2018版:Win10 CMGE_V0-H.1020.000.iso校验码:9484e568c6505f9c4ad5b9fcf7ec8d83588eebfb38089f53e33011 ...