首页 > 系统相关 >C++ 跨进程发送信号

C++ 跨进程发送信号

时间:2023-10-07 11:01:29浏览次数:32  
标签:proc cmd C++ 发送 result str 进程 include buf

跨进程发送信号

接受信号的进程

// sig_wait.cpp
#include <iostream>
// #include <thread>
#include <csignal>
#include <unistd.h>
using namespace std;

void signal_handler_no_parameter()
{
    cout << "get signal : SIGURE1 " << endl;
    exit(1);
}

void signal_handler_int_parameter(int sig_num)
{
    cout << "get signal SIGUSR2 and sig_num = " << sig_num << endl;
    exit(1);
}

int main()
{
    signal(SIGUSR1, (sighandler_t)signal_handler_no_parameter);
    signal(SIGUSR2, signal_handler_int_parameter);

    while(true)
    {
        cout << "Waiting signal..." << endl;
        sleep(3);
    }

    return 0;
}

发送进程

// sig_send.cpp
#include <iostream>
#include <csignal>
#include <cstring>
#include <string>
#include <unistd.h>
using namespace std;

void get_cmd_result(const string &str_cmd, string &str_result)
{
    FILE* cmd_result = popen(str_cmd.c_str(), "r");

    char *buf = new char[256];
    memset(buf, 0 , sizeof(buf));

    fread(buf, 1, sizeof(buf), cmd_result);
    str_result = buf;

    pclose(cmd_result);
    delete buf;
    buf = nullptr;
}

int send_proc_USR1(const string &proc_name)
{
    string str_cmd = "ps -a | grep " + proc_name + " | grep -v grep | awk '{print $1}'";
    string proc_pid;

    get_cmd_result(str_cmd, proc_pid);
    cout << "proc_pid : " << proc_pid << endl;

    if(proc_pid.size() == 0)
    {
        cout << "There is no " << proc_name << " thread, and exit" << endl;
        return -1;
    }

    string str_emit_SIGUSR1_to_proc = "kill -USR1 " + proc_pid;
    cout << "execute cmd : " << str_emit_SIGUSR1_to_proc << endl;
    return system(str_emit_SIGUSR1_to_proc.c_str());

    return 0;
}

int main()
{
    while(true)
    {
        if(0 > send_proc_USR1("sig_wait")){
            break;
        }
        sleep(2);
    }

    return 0;
}

标签:proc,cmd,C++,发送,result,str,进程,include,buf
From: https://www.cnblogs.com/wanghao-boke/p/17745796.html

相关文章

  • 进程间的五种通信方式
    进程间通信(IPC,InterProcessCommunication)是指在不同进程之间传播或交换信息。IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中Socket和Streams支持不同主机上的两个进程IPC。以Linux中的C语言编程为例。一、管道管道,通常......
  • vscode单步调试Android c++源码
    vscode单步调试Androidc++源码  目录步骤1.运行gdbclient.py脚本2.复制生成的launch.json并新建/home/jetson/android_aosp/aosp/.vscode/launch.json3.运行gdb即可,打断点参考 步骤注意:这个过程需要在Android源码环境中运行,可以使用adb端口转发工具,来......
  • python进程之间共享数据
    python进程之间共享数据Value#Value是multiprocessing库提供的对象类​#示例:frommultiprocessingimportProcess,Value​​deftask(num:Value):  #提供锁解决同步问题  withnum.get_lock():    num.value+=1    print(f'process_num={num......
  • vscode c++ 编译运行配置(信息学竞赛OIer专用)
    vscodec++编译运行OI专用配置在你的文件夹下建立一个名为\(\tt.vscode\)的文件夹。目录是这样的:\(\tt.vscode\)\(\tt|--c\_cpp\_properties.json\)\(\tt|--launch.json\)\(\tt|--tasks.json\)\(\tt.vscode/c\_cpp\_properties.json\){"configurations&qu......
  • Linux进程间通信
    匿名管道pipe具有亲缘关系的两个进程间通信,半双工通信,要实现全双工通信需要创建两个pipe。相关系统调用函数名作用fork()复制一个子进程。pipe()创建一个管道。close()用于关闭管道读/写端。write()向管道写入。read()从管道读出。实例#inclu......
  • odoo16开启gevent多进程,提高性能及启用消息推送
    原文地址: https://www.odooai.cn/blog/odoo-install-deploy-6/odoo-gevent-web-socket-boost-setup-286odoo16有一个最大的性能提升,就是从longpolling改成了websocket的方式来推送消息。这个改进顺应了互联网应用的趋势,同时这也是我们整合AI服务的基础,因为AI都是通过消息推送来......
  • C++将角度转为复数
    1.角度转复数,使用std::polar#include<iostream>#include<complex>#include<cmath>intmain(){floattheta=45;floattheta_pi=theta*(M_PI/180);std::cout<<"is"<<std::polar(1.0f,theta_pi)<<......
  • 十四天学会C++之第四天(面向对象编程基础)
    类和对象是什么?在C++中,类是一种用户定义的数据类型,它可以包含数据成员(也就是属性)和成员函数(也就是方法)。类是一种模板或蓝图,用于创建具体的对象。对象是类的实例,它是根据类的定义创建的,可以用来表示现实世界中的各种事物。对象具有类定义的属性和行为。面向对象编程思想面向对象编......
  • Python异步编程并发比较之循环、进程、线程、协程
    服务端现在有一个api接口http://127.0.0.1:18081/hello批量请求该接口,该接口中有一个5s的阻塞。使用循环,多进程,多线程,协程等四种方式,一共请求10次,比较总的请求耗时。importtimefromflaskimportFlaskapp=Flask(__name__)@app.route('/hello')defhello_world():......
  • C++断言之assert和static_assert的区别
    C++断言之assert和static_assert的区别参考链接:c++11:static_assert与assert_夜夜夜夜-CSDN博客_static_assert断言分为动态断言和静态断言2种。c++11引入了static_assert关键字,用来实现编译期间的断言,叫静态断言。1.static_assert静态断言static_assert(常量表达式,要提示......