当某一设备拔出,若上层应用希望迅速的检测到,可以采用信号的方式。上层应用将进程的pid号写入文件及注册相应的信号处理函数,底层则在相应的地方发送信号。测试代码如下:
- void test_write_pid(void)
- {
- fp = fopen("/var/test.pid", "w+");
- if(fp != NULL)
- {
- fprintf(fp, "%d", getpid());
- fclose(fp);
- }
- return;
- }
- void test_send_signal(void)
- {
- struct file *fp;
- char pid[8];
- struct task_struct *p = NULL;
- fp = filp_open("/var/test.pid", O_RDONLY, 0);
- if (IS_ERR(fp))
- return;
- if (fp->f_op && fp->f_op->read) {
- if (fp->f_op->read(fp, pid, 8, &fp->f_pos) > 0) {
- p = find_task_by_pid(simple_strtoul(pid, NULL, 10));
- if (NULL != p) {
- send_sig(SIGUSR1, p, 0);
- }
- }
- }
- filp_close(fp, NULL);
- return;
- }
阅读(373) | 评论(0) | 转发(0) |