笔记二:进程间的通信(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 ...
随机推荐
- C语言程序设计基础 实验3 函数
C语言程序设计基础 实验3 函数 一.实验目的 1. 理解函数的本质:模块化,实现代码复用 2. 掌握函数定义.声明.调用的语法 3. 理解并掌握函数的形参.实参,以及函数调用和返回的过程 4. ...
- Rsync+NFS实战,解决NFS单点问题
1.环境准备 主机 ⻆⾊ ip web01 NFS客户端.RSYNC客户端 172.16.1.7 nfs NFS服务端.RSYNC客户端 172.16.1.31 backup NFS服务端.RSYNC ...
- C/C++ 数据结构优先级队列的实现(使用二级指针)
#include <iostream> #include <Windows.h> #include <iomanip> //优先级队列的实现 using names ...
- Oracle 取Group By 第一条
select *from (select emp.*,row_number() over(partition by deptno order by rownum) cn from emp)where ...
- kubernetes集成GPU原理
这里以Nvidia GPU设备如何在Kubernetes中管理调度为例研究, 工作流程分为以下两个方面: 如何在容器中使用GPU Kubernetes 如何调度GPU 容器中使用GPU 想要在容器中的 ...
- Golang 实现 RTP
在 Coding 之前我们先来简单介绍一下 RTP(Real-time Transport Protocol), 正如它的名字所说,用于互联网的实时传输协议,通过 IP 网络传输音频和视频的网络协议. ...
- CF916E 解题报告
被这道题搞了一个晚上,还好搞出来了qwq 令人耳目一新的阅读体验 题目简述 翻译已经很简单了. 前置知识 DFS序,LCA,线段树,不需要标签中的树剖! DFS序更新信息及判断祖先 如果你还不知道DF ...
- 使用 DeepSpeed 和 Hugging Face 🤗 Transformer 微调 FLAN-T5 XL/XXL
Scaling Instruction-Finetuned Language Models 论文发布了 FLAN-T5 模型,它是 T5 模型的增强版.FLAN-T5 由很多各种各样的任务微调而得,因 ...
- 千亿参数开源大模型 BLOOM 背后的技术
假设你现在有了数据,也搞到了预算,一切就绪,准备开始训练一个大模型,一显身手了,"一朝看尽长安花"似乎近在眼前 -- 且慢!训练可不仅仅像这两个字的发音那么简单,看看 BLOOM ...
- Gitee 码云与Git 交互
一.进入码云官方网站,注册用户 官网地址:https://gitee.com/ 二.创建远程仓库 [1]点击右上角的 + 号进行创建