詳解c++中signal信號(hào)攜帶數(shù)據(jù)的接收與發(fā)送
在之前的文章c++ signal 發(fā)送信號(hào)中,只是說(shuō)明了給指定進(jìn)程發(fā)送信號(hào),而無(wú)法攜帶數(shù)據(jù),今天說(shuō)明下如果和指定進(jìn)程發(fā)送信號(hào),同時(shí)可以攜帶簡(jiǎn)單數(shù)據(jù)。
1. 發(fā)送端使用的接口
int sigqueue(pid_t pid, int sig, const union sigval value);
參數(shù)說(shuō)明
@pid:給指定進(jìn)程的pid
@sig:發(fā)送的信號(hào),為如下1-64
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
@value:主要為下邊的聯(lián)合體賦值,簡(jiǎn)單的整形可以通過(guò)sival_int 直接賦值,其他大的數(shù)據(jù),則需要通過(guò)將地址賦值給sival_ptr。
union sigval { int sival_int; void *sival_ptr; };
返回值:
成功返回0,否則返回-1
2. 接收端使用的接口
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
參數(shù)說(shuō)明:
@signum: 則為接收的信號(hào),與發(fā)送時(shí)的信號(hào)保持一致
@act:主要用來(lái)注冊(cè)我們的接收函數(shù)
@oldact:一般設(shè)置為NULL
struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); };
@@sa_handler: // 只是接收信號(hào)的函數(shù)
@@sa_sigaction:// 接收信號(hào),而且接收攜帶的數(shù)據(jù),同時(shí)設(shè)置sa_flags為SA_SIGINFO
@@sa_mask: 用來(lái)設(shè)置處理函數(shù)是否阻塞
這里只是簡(jiǎn)單說(shuō)明下,詳細(xì)問(wèn)問(wèn)度娘
發(fā)送端使用示例
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h> void DoCmdGetPid(const std::string& cmd, std::string& stringPid) { FILE *pFile = popen(cmd.c_str(), "r"); char *buff = new char[256]; memset(buff, 0, sizeof(buff)); fread(buff, 1, sizeof(buff), pFile); stringPid = buff; pclose(pFile); delete[] buff; } void SendSig() { const std::string& procName = "recv"; std::string cmd = "ps -a | grep " + procName + " |grep -v grep | awk '{print $2}'"; std::string strPid; DoCmdGetPid(cmd, strPid); if (strPid.empty()) { std::cout << "not process\n"; return; } union sigval sv; sv.sival_int = 66; int res = sigqueue(std::stoi(proc_pid), 38, sv); printf("res:%d\n", res); return; } int main() { while (1) { SendSig(); sleep(5); } return 0; }
接收端使用示例
#include <signal.h> #include <unistd.h> #include <iostream> void signal_handler(int s, siginfo_t* pSi, void* ucontext) { if (SIGRTMIN + 4 == s) { std::cout << "recv sig38\n"; printf("value:%d\n", pSi->si_value.sival_int); printf("value:%d\n", pSi->si_int); } } int main() { struct sigaction act; act.sa_sigaction = signal_handler; act.sa_flags = SA_SIGINFO; if (sigaction(SIGRTMIN + 4, &act, nullptr) < 0) {} while(true) { std::cout << "waiting signal..." << std::endl; sleep(3); } return 0; }
到此這篇關(guān)于詳解c++中signal信號(hào)攜帶數(shù)據(jù)的接收與發(fā)送的文章就介紹到這了,更多相關(guān)c++ signal攜帶數(shù)據(jù)的接收與發(fā)送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?中如何結(jié)束?while?(cin>>str)?的輸入
這篇文章主要介紹了C++?中如何結(jié)束?while?(cin>>str)?的輸入,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07深度解析C語(yǔ)言中數(shù)據(jù)的存儲(chǔ)
本文詳細(xì)介紹了C語(yǔ)言中數(shù)據(jù)的存儲(chǔ),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C++中jsoncpp庫(kù)和nlohmann-json庫(kù)實(shí)現(xiàn)JSON與字符串類(lèi)型轉(zhuǎn)換
jsoncpp是ROS自帶的一個(gè)JSON庫(kù),它提供了一些函數(shù)來(lái)解析和生成JSON數(shù)據(jù),在ROS中,可以使用jsoncpp庫(kù)來(lái)實(shí)現(xiàn)JSON與字符串類(lèi)型之間的轉(zhuǎn)換,這篇文章主要介紹了jsoncpp庫(kù)和nlohmann-json庫(kù)實(shí)現(xiàn)JSON與字符串類(lèi)型轉(zhuǎn)換,需要的朋友可以參考下2023-08-08C++?STL實(shí)現(xiàn)非變易查找算法的示例代碼
C++?STL?中的非變易算法(Non-modifying?Algorithms)是指那些不會(huì)修改容器內(nèi)容的算法,是C++提供的一組模板函數(shù),下面我們就來(lái)看看這一算法的應(yīng)用吧2023-08-08