Linux进程之间通信 信号

  • 2) SIGINT
    程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程。
     
  • 3) SIGQUIT
    和SIGINT类似, 但由QUIT字符(通常是Ctrl-\)来控制. 进程在因收到SIGQUIT退出时会产生core文件, 在这个意义上类似于一个程序错误信号。
     
  • 15) SIGTERM
    程序结束(terminate)信号, 与SIGKILL不同的是该信号可以被阻塞和处理。通常用来要求程序自己正常退出,shell命令kill缺省产生这个信号。如果进程终止不了,我们才会尝试SIGKILL。
     
  • 19) SIGSTOP
    停止(stopped)进程的执行. 注意它和terminate以及interrupt的区别:该进程还未结束, 只是暂停执行. 本信号不能被阻塞, 处理或忽略. 

代码

  • 子进程运行到3的时候,会停止,父进程唤醒子进程,子进程接着运行,当子进程到4的时候,中断退出,所有的进程全部结束
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <wait.h>int main() {pid_t pid = 0;int ret;int i =1;if ((pid = fork()) < 0) {perror("fork error!\n");exit(1);}if (pid == 0) {printf("children pid = %d\n", getpid());//raise(SIGINT)for (i = 1; i < 10; i++) {sleep(1);printf("children printf %d ...\n", i);if (i == 3) {printf("stop!\n");raise(SIGSTOP);//进程停止信号}if (i == 4) {printf("contine\n");raise(SIGINT);//进程中断信号}printf("本次id = %d\n", i);}printf("children exit!\n");exit(0);} else {printf("children process id = %d\n", pid);printf("father process pid = %d\n", getpid());sleep(5);if ((waitpid(pid, nullptr, WNOHANG)) == 0) {ret = kill(pid, SIGCONT);if (ret == 0)//向子进程发送 继续运行的信号printf("信号发送成功 %d \n", pid);else {perror("信号发送成功 ");}
//            printf("\n");}return 0;}
}

  • 前面是要捕捉的信号,后面是针对捕捉的信号对应的处理措施,自己写的一个函数 

定义自己信号处理函数

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <wait.h>void cancel(int aig){printf("当前程序取消了ctrl+C功能\n");
}
int main() {signal(SIGINT,cancel);while (1){printf("*\n");sleep(1);}return 0;
}

代码

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <wait.h>void ouch(int aig){printf("OUCH! - I got signal %d\n",aig);
}
int main() {struct sigaction act;act.sa_handler = ouch;//设置信号处理函数sigemptyset(&act.sa_mask);//初始化信号集act.sa_flags = 0;sigaction(SIGINT,&act, nullptr);while (1){printf("Hello World!\n");sleep(1);}return 0;
}
  • 输出结果如下图所示
  • 使用 ctrl + \  停止 

参考链接

  • C语言waitpid()函数:中断(结束)进程函数(等待子进程中断或
  • C语言sigaction()函数:查询或设置信号处理方式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446635.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux进程之间通信 消息队列

使用命令 ipcs -q 查看对应的消息队列代码 文件接收者 #include <sys/types.h> #include <stdio.h> #include <unistd.h> #include <string> #include <signal.h> #include <wait.h> #include <sys/msg.h> #include <cstring&g…

c++面向对象高级编程 学习二 带指针的类

带指针的类&#xff0c;必须要自己写拷贝构造和赋值构造 拷贝构造&#xff1a;参数和类的类型一样的构造函数 赋值构造&#xff1a;重写操作符&#xff0c;且其参数和类的类型一样 class String { public: String(const char* cstr 0); String(const String& str); Strin…

英语口语 week11 Tuesday

英语文章 It was a cold and gloomy winter afternoon, people with their chilled hands tucked into their pockets or hidden in their sleeves. Fred was in a depressed mood, just like the weather,for he failed to get any award in the debate competition When he …

进程之间通信 共享内存

命令 ipcs 命令查看共享内存、消息队列、管道等相关信息ipcs -m 查看共享内存的信息代码 创建共享内存共享内存 关联 进程分离共享内存删除共享内存 #include <sys/shm.h> #include <iostream>#define BUF_SIZE 1024int main() {int share_id 0;//创建共享内存i…

c++面向对象高级编程 学习三 堆、栈和内存泄漏

栈&#xff0c;是存在于某作用域的一块内存空间。在函数体内声明的任何变量&#xff0c;其所使用的内存空间均来自于栈。 堆&#xff0c;是指由操作系统提供的一块global内存空间&#xff0c;程序可动态分配获得若干内存空间块。 new操作符生成的对象所占用的内存空间即是从堆中…

clion编写C++ 使用多线程时候,CMakeLists.txt书写,引用-pthread

添加如下一行 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") 具体的例子 cmake_minimum_required(VERSION 3.17) project(mutex_learn)set(CMAKE_CXX_STANDARD 14)set(BOOST_ROOT "/usr/local/include/boost") #添加头文件搜索路径 include_direc…

c++面向对象高级编程 学习四 静态、类模板、函数模板

静态static&#xff1a;静态数据和静态函数&#xff0c;在内存中只有一份&#xff0c;不会随着创建对象的数目的增加而增加 static数据&#xff1a;比如银行的account类中&#xff0c;账户名是普通数据&#xff0c;100个对象会有100个账户名&#xff0c;但利率都是相同的&#…

线程的编程

完整代码 #include <sys/shm.h> #include <iostream> #include <unistd.h> #include <pthread.h>void * child1(void *arg){pthread_t tid pthread_self();printf("1 thread %lu \n",tid);}int main(int argc,char* argv[]) {int result{…

英语口语 week11 Friday

英语文章 I very much like simplicity in life. For me, college is far more than a place to improve my intellectual abilities Every weekend, I usually have a walk along the way to the front gate of Mount Qingcheng, enjoying the intense aromas of flowers on …

c++面向对象高级编程 学习五 组合、委托与继承

组合 composition 表示has a queue类中有一个deque容器&#xff0c;这种关系叫做 组合 queue中的六个函数都是调用c的函数完成的 template <class T> class queue { ... protected: deque<T> c; // 底層容器 public: // 以下完全利用 c 的操作函數完成 bool empt…

英语口语 week12 WednesDay

英语文章 Chengdu, a city with a long history, has always enjoyed the reputation as " The Land of Abundance" . It has been noted as one of the most livable cities in China, partly resulting from its favorable natural conditions and wealthy produc…

c++面向对象高级编程 学习六 虚函数

虚函数&#xff1a;在成员函数前面加上virtual&#xff0c;函数就变成了虚函数 继承函数&#xff1a;子类可以调用父类的函数&#xff0c;叫做继承了函数&#xff0c;即函数的调用权 三种函数&#xff1a; non-virtual 函数&#xff1a; 你不希望 derived class 重新定义 (ov…

C++ 数据结构 线性链表

#pragma once 减少头文件组合&#xff0c;降低编译出错的概率作用等效于 #ifndef FUNC_H #define FUNC_H代码主体#endif 线性表的定义 排队问题 简单的线性表 (物理 或者逻辑结构)1&#xff0c;数组2&#xff0c;链表线性表相关操作&#xff1a;1&#xff0c;线性表初始化2&a…

英语口语 week12 Thursday

英语文章 As the pace of life quickens with technological advancements, people are occupied by all kinds of trivial matters. They seem forever on the go. There’s no difficulty in imagining that the hustle and bustle of everyday life can make us lose focus…

H.264/AVC视频编解码技术详解 第一章 视频信息与压缩编码

H.264/AVC视频编解码技术详解系列笔记 是对 H.264/AVC视频编解码技术详解 课程的学习 文章目录人与世界的交互视频信号的表示方法视频压缩编码视频信息为什么可以被压缩&#xff1f;视频压缩编码的分类&#xff1a;视频压缩编码的基本技术人与世界的交互 从远古时代开始&#…

英语口语 week13 Monday

英语文章 Competitions between businesses can be very aggressive in contemporary society. However, the competition for talented personnel is thought to be the key to business competition. Wise employers consider their employees as the company’s core asset…

文件系统的由来

启蒙篇 文件的由来 磁盘上保存的是一对十六进制的数据&#xff0c;如何切分数据形成不同的文件&#xff0c;也就是如何确定一个文件的起始和终止位置&#xff1f;将相关数据打包在一起形成一个文件&#xff0c;比如从什么位置开始到什么位置结束&#xff0c;是一张图片、一段…

英语口语 week13 Wednesday

英语文章 Despite his extraordinary success in writing fairy tales,Hans Christian Andersen preferred to living in a way of simplicity and frugality. He often wore an old hat when he went out. One day, a well-dressed man stopped Andersen on the street, inte…

操作系统 IO管理

学什么&#xff1f; I/O input / output 输入&#xff1a;鼠标 键盘 手柄 触摸屏 摄像头 MTC 扫描仪输出&#xff1a;显示器 打印机 耳机 音响 既是输入也是输出&#xff1a;光驱 网卡 磁盘 U盘硬件&#xff1a;设备如何把数据返回到PC机&#xff0c;但是不同种类的设…

c++面向对象高级编程 学习九 pointer-like classes

c的class设计出来有两种形式&#xff0c;一种像指针&#xff0c;一种像函数 智能指针里包含普通指针&#xff0c;要写 * 和 -> 的函数 sp->method(); //sp-> 经 T* operator*() const 函数&#xff0c;得到px //由于 箭头符号&#xff08;->&#xff09;作用下去…