如何将信号从一个程序发送到另一个程序?

时间:2022-04-13 00:35:39

i am using message queue as an ipc between 2 programs. Now i want to send data from one program to another using message queue and then intimate it through a signal SIGINT.

我使用消息队列作为2个程序之间的IPC。现在我想使用消息队列将数据从一个程序发送到另一个程序,然后通过SIGINT信号将其发送给它。

I dont know how to send a signal from one program to another . Can anybody pls provide a sample code if they have the solution.

我不知道如何将信号从一个程序发送到另一个程序。任何人都可以提供示例代码,如果他们有解决方案。

4 个解决方案

#1


11  

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

#2


5  

Signal in linux can be send using kill system call just check this link for documentation of kill system call and example. you can see man -2 kill also. and it's not advisable to use SIGINT use SIGUSR1 or SIGUSR2

linux中的信号可以使用kill system调用发送,只需检查此链接以获取kill system调用和示例的文档。你也可以看到男人-2杀人。并且不建议使用SIGINT使用SIGUSR1或SIGUSR2

#3


1  

Note that by using the sigqueue() system call, you can pass an extra piece of data along with your signal. Here's a brief quote from "man 2 sigqueue":

请注意,通过使用sigqueue()系统调用,您可以传递额外的数据和信号。以下是“man 2 sigqueue”的简短引用:

The value argument is used to specify an accompanying item of data (either an integer or a pointer value) to be sent with the signal, and has the following type:

value参数用于指定随信号一起发送的伴随数据项(整数或指针值),并具有以下类型:

     union sigval {
         int   sival_int;
         void *sival_ptr;
     };

This is a very convenient way to pass a small bit of information between 2 processes. I agree with the user above -- use SIGUSR1 or SIGUSR2 and a good sigval, and you can pass whatever you'd like.

这是在两个进程之间传递一小部分信息的一种非常方便的方法。我同意上面的用户 - 使用SIGUSR1或SIGUSR2和一个好的sigval,你可以传递你想要的任何东西。

You could also pass a pointer to some object in shared memory via the sival_ptr, and pass a larger object that way.

您还可以通过sival_ptr将指针传递给共享内存中的某个对象,并以这种方式传递更大的对象。

#4


-4  

system("kill -2 `pidof <app_name_here>` ");

#1


11  

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

#2


5  

Signal in linux can be send using kill system call just check this link for documentation of kill system call and example. you can see man -2 kill also. and it's not advisable to use SIGINT use SIGUSR1 or SIGUSR2

linux中的信号可以使用kill system调用发送,只需检查此链接以获取kill system调用和示例的文档。你也可以看到男人-2杀人。并且不建议使用SIGINT使用SIGUSR1或SIGUSR2

#3


1  

Note that by using the sigqueue() system call, you can pass an extra piece of data along with your signal. Here's a brief quote from "man 2 sigqueue":

请注意,通过使用sigqueue()系统调用,您可以传递额外的数据和信号。以下是“man 2 sigqueue”的简短引用:

The value argument is used to specify an accompanying item of data (either an integer or a pointer value) to be sent with the signal, and has the following type:

value参数用于指定随信号一起发送的伴随数据项(整数或指针值),并具有以下类型:

     union sigval {
         int   sival_int;
         void *sival_ptr;
     };

This is a very convenient way to pass a small bit of information between 2 processes. I agree with the user above -- use SIGUSR1 or SIGUSR2 and a good sigval, and you can pass whatever you'd like.

这是在两个进程之间传递一小部分信息的一种非常方便的方法。我同意上面的用户 - 使用SIGUSR1或SIGUSR2和一个好的sigval,你可以传递你想要的任何东西。

You could also pass a pointer to some object in shared memory via the sival_ptr, and pass a larger object that way.

您还可以通过sival_ptr将指针传递给共享内存中的某个对象,并以这种方式传递更大的对象。

#4


-4  

system("kill -2 `pidof <app_name_here>` ");