7 c++ unix multithreading pipe
当信号从线程到达主进程时,我试图通过管道传输数据.
这可能吗?
如何才能做到这一点?
该方案应该如何实施?
Dou*_* T. 13
是的,可以通过管道.
第一步调用管道获取管道:
#include <unistd.h>
int main(...)
{
int fileDescriptors[2];
pipe(fileDescriptors);
Run Code Online (Sandbox Code Playgroud)
步骤2将fileDescriptors [0]传递给主进程,将fileDescriptors 1传递给线程.在Main中,等待通过从fileDescriptors [0]读取来写入管道
...
char msg[100];
read(fileDescriptors[0], msg, 100); // block until pipe is read
}
Run Code Online (Sandbox Code Playgroud)
步骤3,当信号发生时,从你的线程写入fileDescritpors 1
void signal_handler( int sig )
{
// Write to the file descriptor
if (sig == SIGKILL)
{
const char* msg = "Hello Mama!";
write(fileDescriptors[1], msg, strlen(msg));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9381 次 |
| 最近记录: |