1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <time.h> #include <pthread.h> #include <fcntl.h> #include <sys/stat.h>
int getPipePath(int number, char **path) { if (asprintf(path, "/sr_evpipe%d", number) == -1) { return -1; }
return 0; }
void write_notify(int number) { char *path = NULL, buf[1] = {0}; int fd = -1, ret;
if (getPipePath(number, &path) < 0) { printf("error get pipe path"); return; }
if ((fd = open(path, O_WRONLY|O_NONBLOCK)) == -1) { free(path); return; }
free(path);
do { ret = write(fd, buf, 1); } while (!ret); if (ret == -1) { printf("error write"); return; }
printf("write a notify\r\n"); if (fd > -1) { close(fd); } }
void do_read(int fd) { int ret = 0; char buf[1]; do { ret = read(fd, buf, 1); } while (ret == 1); if ((ret == -1) && (errno != EAGAIN)) { printf("read error\r\n"); return; } printf("read ok , do something!\r\n"); }
void *pipe_listen_thread(void *arg) { fd_set rfds; struct timeval tv; time_t stop_time_in = 0;
int read = *(int *)arg; while (1) { tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(read, &rfds); int ret = select(read + 1, &rfds, NULL, NULL, &tv); if ((ret == -1) && (errno != EINTR)) { break; } else if ((!ret || ((ret == -1)&&(errno == EINTR)))) { printf("time out"); continue; } do_read(read); } }
void main() { int number = 0; char *path = NULL;
if (getPipePath(number, &path) < 0) { return; } unlink(path); mode_t um = umask(0); int ret = mkfifo(path, 00622); umask(um); if (ret == -1) { printf("mkfifo error"); free(path); return; } int readFd = open(path, O_RDWR | O_NONBLOCK); if (readFd == -1) { printf("open error"); free(path); return; }
free(path);
pthread_t tid; if (ret = pthread_create(&tid, NULL, pipe_listen_thread, &readFd)) { close(readFd); return; }
while (1) { sleep(5); write_notify(number); }
pthread_join(tid, NULL); close(readFd); return ; }
|