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.pswp.cn/news/446635.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446635.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446635.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++面向對象高級編程 學習一 不帶指針的類

復數類 complex 是一個不帶指針的類&#xff0c;其聲明如下&#xff1a; class complex { public: complex(double r0,double i0):re(r),im(i){} complex& operator (const complex&); double real()const{return re;} double imag()const{return im;}private: double…

codeforces 734A-C語言解題報告

734A題目網址 題目解析 1.輸入n個結果,再輸入每一個結果(A或D),統計A和D的數目,A>D,則輸出Anton,A<D,輸出Danik,AD,輸出Friendship 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main() { int n0,A0…

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 …

codeforces 271A-C語言解題報告

271A題目網址 題目解析 1.輸入一個年份,求這個年份之后的每一個數字都各不相同的年份 舉例: 輸入: 2013 輸出: 2014 2.求年份(四位數)的每一位數,再把這些數逐個比較 四位數求: 千位:n/1000 百位:n%1000/100 十位:n%100/10 個位:n%10/1; 代碼 #include<stdio.h> #in…

進程之間通信 共享內存

命令 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…

codeforces 677A-C語言解題報告

677A題目網址 題目解析 1.輸入n個數字,如果輸入的數字比h大,就加2,如果小于等于h,就加1 舉例: 輸入: 3 7 4 5 14 輸出: 4 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main() { int n0,h0,count0;int nu…

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…

C++四種類型轉換運算符:static_cast、dynamic_cast、const_cast和reinterpret_cast

參考鏈接 C四種類型轉換運算符&#xff1a;static_cast、dynamic_cast、const_cast和reinterpret_cast

英語口語 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;視頻壓縮編碼的基本技術人與世界的交互 從遠古時代開始&#…