fifo.c
资源名称:cgi-web.rar [点击查看]
上传用户:sxdewei
上传日期:2014-06-02
资源大小:67k
文件大小:2k
源码类别:
嵌入式Linux
开发平台:
Unix_Linux
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include "fifo.h"
- #include "sysconf.h"
- int open_read_fifo()
- {
- char fifo_name[MAX_FILE_NAME_LEN];
- int r_fd;
- memset(fifo_name, 0, MAX_FILE_NAME_LEN);
- sprintf(fifo_name, "/tmp/fifo%d", getpid());
- //printf("fifo file : %sn",fifo_name);
- if((mkfifo(fifo_name, O_CREAT|O_EXCL)<0) && (errno!=EEXIST))
- DEBUGMSG(1, ("cannot create %sn", fifo_name));
- r_fd = open(fifo_name, O_RDONLY|O_NONBLOCK,0);
- if(r_fd == -1)
- {
- DEBUGMSG(1, ("open read errorn"));
- return -1;
- }
- return r_fd;
- }
- void close_read_fifo()
- {
- char fifo_name[MAX_FILE_NAME_LEN];
- memset(fifo_name, 0, MAX_FILE_NAME_LEN);
- sprintf(fifo_name, "/tmp/fifo%d", getpid());
- if(0 != unlink(fifo_name) )
- {
- printf("unable to delete fifo file ,error: %s n",strerror(errno));
- }
- else{
- // printf("successed to delete fifo file %sn",fifo_name);
- }
- }
- int read_fifo(int r_fd)
- {
- int i = 0;
- fd_set file_set;
- struct timeval delay_time;
- char r_buf[MAX_CMD_LEN];
- int maxfd;
- int result;
- int ret_size;
- while(1)
- // while(i<READ_FIFO_DELAY_TIME) //change READ_FIFO_DELAY_TIME if necessary
- {
- FD_ZERO(&file_set);
- delay_time.tv_sec = 0;
- delay_time.tv_usec = 50000;
- FD_SET(r_fd, &file_set);
- maxfd = r_fd + 1;
- select(maxfd, &file_set, NULL, NULL, &delay_time);
- if(FD_ISSET(r_fd, &file_set))
- {
- ret_size = read(r_fd, r_buf, FIFO_PAGE_SIZE);
- if(ret_size > 0)
- {
- result = atoi(r_buf);
- return result;
- }
- }
- i++;
- }
- return 0;
- }
- int open_write_fifo()
- {
- int w_fd;
- w_fd = open(CGI_TO_MAIN_FIFO, O_WRONLY|O_NONBLOCK, 0);
- if(w_fd == -1)
- if(errno == ENXIO)
- DEBUGMSG(1, ("open error; no reading processn"));
- return w_fd;
- }
- void close_write_fifo(int w_fd)
- {
- close(w_fd);
- }
- int notify_config(int w_fd)
- {
- char cmd_str[MAX_CMD_LEN];
- int ret_size;
- memset(cmd_str, 0, MAX_CMD_LEN);
- sprintf(cmd_str, "%dn", getpid());
- ret_size = write(w_fd, cmd_str, strlen(cmd_str));
- return ret_size;
- }
- int send_cmd(int w_fd, int cmd)
- {
- char cmd_str[MAX_CMD_LEN];
- int ret_size;
- memset(cmd_str, 0, MAX_CMD_LEN);
- sprintf(cmd_str, "0 %d %dn", cmd, getpid());
- ret_size = write(w_fd, cmd_str, strlen(cmd_str));
- return ret_size;
- }