命名管道
主要原理就是实现了生产者消费者问题,实现进程同步功能。为啥要用命名管道呢?就是想摆脱管道的束缚,实现任意进程间的通信。
代码如下
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cassert>
#include <fcntl.h>
#include <csignal>
#include <iostream>
#define PIPE_SYNC "/tmp/syncfifo"
#define FILE_MODE (0666)
//#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
typedef struct{
pid_t pid;
}StorePid;
bool create_sync_pipe()
{
if (access(PIPE_SYNC, F_OK) == -1)
{
int ret = mkfifo(PIPE_SYNC, FILE_MODE);
if (ret != 0){
printf("Create PIPE_SYNC fail (%s) (%d)\n", __func__, __LINE__);
unlink(PIPE_SYNC);
return false;
}
}else{
printf(" PIPE_SYNC exist (%s) (%d)\n", __func__, __LINE__);
chmod(PIPE_SYNC, FILE_MODE);
}
return true;
}
bool del_sync_pipe()
{
if (access(PIPE_SYNC, F_OK) != -1){
printf(" PIPE_SYNC exist del it (%s) (%d)\n", __func__, __LINE__);
unlink(PIPE_SYNC);
}
return true;
}
void signalHandler( int signum )
{
printf("Interrupt signal (%d) received.\n",signum);
del_sync_pipe();
// cleanup and close up stuff here
// terminate program
exit(signum);
}
bool send_cudapid(StorePid* params)
{
if (params == nullptr)
return false;
if (access(PIPE_SYNC, F_OK) == -1){
printf(" PIPE_SYNC not exist (%s) (%d)\n", __func__, __LINE__);
assert(0);
return false;
}
int pipe_fd = open(PIPE_SYNC, O_WRONLY);
if (pipe_fd != -1)
{
int ret = write(pipe_fd, params, sizeof(StorePid));
if (ret == -1)
{
printf("[%s][%d][ Send_cudapid fail!][ret:%d]\n", __func__, __LINE__, ret);
}
else
{
close(pipe_fd);
#ifdef CS_DEBUG
printf("[%s][%d][Send_cudapid success ][ret:%d]\n", __func__, __LINE__, ret);
#endif
}
}
else
{
printf("[%s][%d][open PIPE_SYNC fail!][ret:%d]\n", __func__, __LINE__, pipe_fd);
return false;
}
return true;
}
bool get_cudapid(StorePid* data)
{
if (data == nullptr)
return false;
if (access(PIPE_SYNC, F_OK) == -1){
printf(" PIPE_SYNC not exist (%s) (%d)\n", __func__, __LINE__);
assert(0);
return false;
}
int ret;
int pipe_fd = open(PIPE_SYNC, O_RDONLY);
if (pipe_fd != -1)
{
do
{
ret = read(pipe_fd, data, sizeof(StorePid));
#ifdef CS_DEBUG
printf("[%s][%d][read PIPE_SYNC ][ret:%d][pid:%d]\n", __func__, __LINE__, ret, getpid());
#endif
} while (ret != sizeof(StorePid));
close(pipe_fd);
#ifdef CS_DEBUG
printf("[%s][%d][Get_cudapid success begin exe cuda process!]\n", __func__, __LINE__);
#endif
}
else
{
printf("[%s][%d][open PIPE_SYNC fail!]\n", __func__, __LINE__);
return false;
}
return true;
}
bool send_msg_to_exe_cuda(void)
{
if (access(PIPE_SYNC, F_OK) == -1){
printf(" PIPE_SYNC not exist (%s) (%d)\n", __func__, __LINE__);
assert(0);
return false;
}
char buff = 1;
int pipe_fd = open(PIPE_SYNC, O_WRONLY);
if (pipe_fd != -1)
{
int ret = write(pipe_fd, &buff, 1);
if (ret == -1)
{
printf("[%s][%d][write PIPE_SYNC fail!][ret:%d]\n", __func__, __LINE__, ret);
return false;
}
else
{
close(pipe_fd);
#ifdef CS_DEBUG
printf("[%s][%d][write PIPE_SYNC success ][ret:%d]\n", __func__, __LINE__, ret);
#endif
}
}
else
{
printf("[%s][%d][opene PIPE_SYNC fail!][ret:%d]\n", __func__, __LINE__, pipe_fd);
return false;
}
return true;
}
bool get_msg_to_exe_cuda(void)
{
char buffer = 0;
int ret;
if (access(PIPE_SYNC, F_OK) == -1){
printf(" PIPE_SYNC not exist (%s) (%d)\n", __func__, __LINE__);
assert(0);
return false;
}
int pipe_fd = open(PIPE_SYNC, O_RDONLY);
if (pipe_fd != -1)
{
do
{
ret = read(pipe_fd, &buffer, 1);
#ifdef CS_DEBUG
printf("[%s][%d][read PIPE_SYNC][ret:%d][pid:%d]\n", __func__, __LINE__, ret, getpid());
#endif
} while (ret != 1 || buffer == 0);
close(pipe_fd);
#ifdef CS_DEBUG
printf("[%s][%d][get msg success begin exe cuda process!]\n", __func__, __LINE__);
#endif
return true;
}
else
{
printf("[%s][%d][open pipe fail,pid:%d exit!]\n", __func__, __LINE__, getpid());
return false;
}
}
int main()
{
pid_t pid;
signal(SIGINT, signalHandler);
if(!create_sync_pipe()){
printf("[%s][%d]\n",__func__,__LINE__);
return 1;
}
pid = fork();
if(pid == 0){
StorePid data;
data.pid = getpid();
if(send_cudapid(&data)){
printf("[%s][%d] send pid success pid:%d\n",__func__,__LINE__,data.pid);
}
//sleep(1);
if(get_msg_to_exe_cuda()){
printf("[%s][%d] get mesg success begin to .........\n",__func__,__LINE__);
sleep(3);
del_sync_pipe();
exit(0);
//while(1);
}
exit(1);
}
else{
//sleep(5);
StorePid pdata;
if(get_cudapid(&pdata)){
printf("[%s][%d] parent get pid:%d\n",__func__,__LINE__,pdata.pid);
}
//sleep(1);
if(send_msg_to_exe_cuda()){
printf("[%s][%d] send mesg success begin to .........\n",__func__,__LINE__);
}
//del_sync_pipe();
waitpid(pid,NULL,0);
}
return 0;
}
执行结果
注意问题
(1)这里我们创建的管道文件/tmp/syncfifo 每次创建然后删除,不能保证一些异常情况下代码会正常执行,导致创建的文件不能删除,这样如果换了用户,执行可能会有文件权限的问题,导致open文件打不开。为什么会这样呢?其实我们创建的问题件权限0666是所有用户所有用户组都有读写权限。但是由于系统下umask 机制,导致创建的文件权限并不是0666。具体当前用户umask是啥,用umask查看,如下图所示:
普通用户
创建后的文件权限会减去umask的值,因此最终创建的文件权限如下图所示
su用户:
注册异常处理handler函数
用户态实现注册异常处理,比如ctr +c 等信号的处理事件
signal(SIGINT, signalHandler);
SIGINT 为信号向量,signalHandler为回调函数,本文中防止ctr+c 程序元异常退出导致管道文件没有删除,所以在回调里面也调用了删除管道的函数。