笔记二:进程间的通信(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 ...
随机推荐
- python,数据类型和变量,数据类型和变量,集合,字符串拼接
可不可变: 可变:列表,字典 不可变:字符串,数字,元祖 访问顺序: 直接访问:数字 顺序访问:字符串,列表,元祖 映射:字典 存放元素个数 容器类型:列表,元祖,字典 原子:数字,字符串 集合 1. ...
- 深入理解css 笔记(9)
模块化 CSS 是指把页面分割成不同的组成部分,这些组成部分可以在多种上下文中重复使用,并且互相之间没有依赖关系.最终目的是,当我们修改其中一部分 css 时,不会对其他部分产生意料之外的影响. ...
- MyCat中间件的坑
首先说一下为什么选择MyCat,mysql分表分库的工具还有sharding-jdbc,是jar包的形式集成到项目的,可以相对灵活的配置自定义分片策略(PS:其实大部分业务场景是分片策略越简单越好啊, ...
- C# 读取json文件并转为List集合
using (System.IO.StreamReader file = System.IO.File.OpenText(pathForSaving)) { using (JsonTextReader ...
- 学习记录--C++多态性简答+编程题
#include<iostream> #include<string> //双目运算符:运算符作用域两个操作数 //定义一个复数类,重载"+",作为复数类的 ...
- #HDU2255#奔小康赚大钱(KM模板题)
Problem Description传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑 ...
- 声网王浩宇:RTE 场景下的 Serverless 架构挑战【RTE 2022】
前言 在「RTE2022 实时互联网大会」中,声网云原生边缘计算团队的负责人 @王浩宇 Dylan 以<RTE 场景下的 Serverless 架构挑战 -- 声网如何兼顾后端服务的可靠.高效和 ...
- TCC 分布式事务解决方案
更多内容,前往 IT-BLOG 一.什么是 TCC事务 TCC 是Try.Confirm.Cancel三个词语的缩写,TCC要求每个分支事务实现三个操作:预处理Try.确认Confirm.撤销Canc ...
- SpringBoot——Swagger2 接口规范
更多内容,前往 IT-BLOG 如今,REST和微服务已经有了很大的发展势头.但是,REST规范中并没有提供一种规范来编写我们的对外 REST接口 API文档.每个人都在用自己的方式记录 api文档, ...
- DES算法流程
初始置换IP 表格的使用方法: 将输入的64bit的明文从1开始标号,依次放入到IP初始置换表中数字对应的位置.填充完毕后,按照行优先的顺序从第1行开始依次读取获得输出. 16轮轮结构 整体结构 因为 ...