关于进程间通信的一些小方法

来源:互联网 发布:中国的下一场战争知乎 编辑:程序博客网 时间:2024/04/28 16:11

关于进程间通信的一些小方法:

 

1.判断信号量是否存在:sem_id = semget(SEM_KEY, 0, 0) 等于 1 则存在,不等于1,不存在。

 

2.安装信号处理器。

 

action.sa_handler = catch_ctrl_c;

sigemptyset(&action.sa_mask);

action.sa_flags = 0;

if(sigaction(SIGINT, &axtion, NULL) == -1)

perror("can not install the handler for SIGINT");

 

void catch_ctrl_c(){

if(semctl(sim_id, 0, IPR_RMID, O) == -1){

perror("Remove Semaphore");

exit(0);

}  

原创粉丝点击