【发布时间】:2017-02-13 12:23:17
【问题描述】:
我想了解一些示例程序,其中一些中断或信号在两个线程之间进行通信。我浏览并发现了一些系统调用,如 kill、tkill、tgkill 和 raise。但我的要求是不杀死进程它应该表现为中断。在我的代码中,我有这个阻塞调用。
fcntl(fd, F_SETFL,0);
read(fd,&dataReceived.Serial_input,1);
任何与我的要求相似的示例代码。请分享。在此先感谢
我的代码:
void *serial_function(void *threadNo_R)
{
int ImThreadNo = (int) threadNo_R;
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);//
if (fd == -1)
{
/* Could not open the port. */
perror("open_port: Unable to open /dev/ttyUSB1 - ");
}
fcntl(fd, F_SETFL,0);
while(1)
{
read(fd,&dataReceived.Serial_input,1);
printf("\n From serial fn: Serial_input is:%c\n",dataReceived.Serial_input);
dataReceived.t2=dataReceived.Serial_input;
if(V_buf.power_window_data.front_right_up>=1)
{
sprintf(cmd,"Window is raising=%d",V_buf.power_window_data.front_right_up);
do
{
writenornot = write( fd, &cmd[spot], 1 );
spot++;
} while (cmd[spot-1] != '\0' );
spot=0;
//
if (writenornot < 0)
{
printf("Write Failed \n");
}
else
printf("Write successfull \n");
// write( fd,"DOWN",4);
}
print_screen=1;
}
}
接收函数:
void *receive_function(void *threadNo_R)
{
int ImThreadNo = (int) threadNo_R;
while(1)
{
if(msgrcv(R_msgid,&V_buf,sizeof(struct vehicle)+1,1,0) == -1)
{
printf("\n\nError failed to receive:\n\n");
}
}
}
我想从接收函数发送信号,应该由串行函数处理。
【问题讨论】:
-
从长远来看,使用非阻塞 IO 和
select,您会更快乐,但是对于您已有的代码的最小更改,tkill是您想要的 - 您只需要安装SIGUSR1的 handler,设置为 not restart 系统调用,就可以了。 -
如何安装处理程序 SIGUSR1?
-
您应该能够自己回答这个问题。首先阅读
sigaction手册页。 -
是的会看看@zwol
-
kill 不会终止进程——它只是发送一个信号。当有许多其他 IPC 机制可用时,为什么要在线程之间发送中断。另外,如果有两个或多个线程将自己指定为信号处理程序并且它们都做不同的事情,那么您如何判断哪个线程正在处理它?