POSIX消息队列可以注册空队列有消息到达时所触发的信号,而信号触发对应的信号处理函数。

下面是一份基本的消息队列和信号处理结合的代码(修改自UNIX网络编程:进程间通信)

#include <stdio.h>
#include <stdlib.h> #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <signal.h>
#include <pthread.h> mqd_t queue;
void* buff;
struct mq_attr attr;
struct sigevent sigev; static void sig_user1(int signo) {
printf("current pid:%d, threadid:%u\n", getpid(), pthread_self());
int n, prio;
/* signal will be unregistered from previous registered message queue
when first message arrived.
So register signal again if we want trigger the signal handler again */
mq_notify(queue, &sigev); /* read msg */
int res = mq_receive(queue, buff, attr.mq_msgsize, &prio);
if (res < ) {
perror("receive msg fail");
exit(-);
}
printf("prio: %d, msg: %s\n", prio, (char*)buff);
return;
} int main(int argc, char** argv) {
char* name = "/testmq";
if (argc > ) {
name = argv[];
} queue = mq_open(name, O_RDONLY); if (queue < ) {
perror("open message queue fail");
return -;
} int res = mq_getattr(queue, &attr);
if (res != ) {
perror("get message queue fail");
return -;
} buff = malloc(attr.mq_msgsize); signal(SIGUSR1, sig_user1); sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGUSR1; mq_notify(queue, &sigev); printf("[%d.%d] start to waiting\n", getpid(), pthread_self());
for (;;) {
pause();
} return ;
}

运行输出:

hgf@ubuntu:~/ipc$ ./mqnotify /haha
[25328.1410565952] start to waiting
current pid:, threadid:

可以看到主线程和执行信号处理的线程是同一个,这估计也是书上说以上程序存在问题,因为有些函数调用不应该放在信号处理函数当中。因为它们可能使用了互斥资源可能造成死锁,比如printf,当其正在处理时又来一个信号于是再次调用printf然后可能上一个printf还没有释放对一些互斥资源的所有权造成死锁。

UNIX IPC: POSIX 消息队列 与 信号的更多相关文章

  1. UNIX IPC: POSIX 消息队列

    首先在我的MAC OSX上试了一下虽然有_POSIX_MESSAGE_PASSING的宏定义,但是用gcc编译会提示没有mqueue.h头文件,先放一边.在Ubuntu上使用正常,不过POSIX消息队 ...

  2. Linux IPC POSIX 消息队列

    模型: #include<mqueue.h> #include <sys/stat.h> #include <fcntl.h> mq_open() //创建/获取消 ...

  3. Unix IPC之Posix消息队列(1)

    部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...

  4. Linux IPC实践(7) --Posix消息队列

    1. 创建/获取一个消息队列 #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For m ...

  5. IPC通信:Posix消息队列

    IPC通信:Posix消息队列 消息队列可以认为是一个链表.进程(线程)可以往里写消息,也可以从里面取出消息.一个进程可以往某个消息队列里写消息,然后终止,另一个进程随时可以从消息队列里取走这些消息. ...

  6. Linux进程间通信(IPC)编程实践(十二)Posix消息队列--基本API的使用

    posix消息队列与system v消息队列的区别: (1)对posix消息队列的读总是返回最高优先级的最早消息,对system v消息队列的读则能够返回随意指定优先级的消息. (2)当往一个空队列放 ...

  7. Linux环境编程之IPC进程间通信(五):Posix消息队列1

    对于管道和FIFO来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...

  8. 进程间通信--POSIX消息队列

    相关函数: mqd_t mq_open(const char *name, int oflag); mqd_t mq_send(mqd_t mqdes, const char *msg_ptr, si ...

  9. [转]Linux进程通信之POSIX消息队列

    进程间的消息队列可以用这个实现,学习了下. http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&am ...

随机推荐

  1. [CSS3] 动画暗角按钮

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. C++多线程编程二

    1. 死锁与解锁: #include <iostream> #include <thread> #include <mutex> using namespace s ...

  3. SaltStack 基础

    介 SaltStack是基于Python开发的一套C/S架构配置管理工具.它的底层使用ZeroMQ消息队列pub/sub方式通信,使用SSL证书签发的方式进行认证管理.号称世界上最快的消息队列Zero ...

  4. 符合Python风格的对象

    array和bytes的转换 - 每个array必须有一个type_code,以此为依据解析底层字节序列 - array有一个frombytes方法,可以把字节序列按type_code转换成Array ...

  5. 《Implementing QuantLib》译后记

    目录 <Implementing QuantLib>译后记 初心 瞎忙 收获 彩蛋 展望 就在几天之前,经历了一年时间断断续续的坚持,<Implementing QuantLib&g ...

  6. django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')

    环境介绍 Django (2.1)  Python 3.5.5 mysqlclient (1.4.2.post1) Mysql 5.6.28 RHEL 7.3 在migrate时候报错 model代码 ...

  7. 终于解决了贴吧手机版的一个重大BUG

    终于解决了贴吧手机版的一个重大BUG 别诧异虽然同一个域名,但是,PC 和手机打开完全不一样的体验 http://tieba.yunxunmi.com/ 吃点夜校准备做梦去!! 发现 我云贴吧 一个  ...

  8. C#原生压缩和解压缩方法

    string path = AppDomain.CurrentDomain.BaseDirectory; string startPath = @"c:\Client"; stri ...

  9. jquery的animate关于background-position属性

    jQuery 的 animate 虽然能直接使用 CSS 的方式来进行动画,但有些属性其实是不支持的,例如:background-position. 谷歌支持 background-position- ...

  10. Hive的Shell里hive> 执行操作时,出现FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask错误的解决办法(图文详解)

    不多说,直接上干货! 这个问题,得非 你的hive和hbase是不是同样都是CDH版本,还是一个是apache版本,一个是CDH版本. 问题详情 [kfk@bigdata-pro01 apache-h ...