点击(此处)折叠或打开
- /*
- pipe-write
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- /*
- * if do write first, write-app will be blocked until read-app start.
- * in runtime, when read-app closed, then write-app will be receive signal(SIGPIPE), and then terminate self.
- */
- #define FIFO_FFPLAY "/tmp/my_fifo"
- int main()
- {
- int fd = mkfifo(FIFO_FFPLAY, 0777);
- if (fd == 0) {
- printf("create FIFO OK.\n");
- }
- else {
- if (EEXIST != errno) {
- printf("mkfifo failed. \n");
- exit(-1);
- }
- }
-
- fd = open(FIFO_FFPLAY, O_WRONLY);
- if (fd < 0) {
- printf("open FIFO failed. \n");
- exit(-1);
- }
-
- printf("Write data to FIFO.\n");
-
- char line[128] = {0};
- int idx = 0;
- while (1) {
- sprintf(line, "%04d.\n 000", idx++);
- printf(line);
- write(fd, line, strlen(line));
- usleep(1000000);
- }
-
- close(fd);
- //unlink(FIFO_FFPLAY);
-
- return 0;
- }
点击(此处)折叠或打开
- /*
- pipe-read
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #define FIFO_FFPLAY "/tmp/my_fifo"
- int main()
- {
- int fd = mkfifo(FIFO_FFPLAY, 0777);
- if (fd == 0) {
- printf("create FIFO OK.\n");
- }
- else {
- if (EEXIST != errno) {
- printf("mkfifo failed. \n");
- exit(-1);
- }
- }
-
- fd = open(FIFO_FFPLAY, O_RDONLY);
- if (fd < 0) {
- printf("open FIFO failed. \n");
- exit(-1);
- }
-
- FILE *file = fdopen(fd, "r");
- printf("reading data from FIFO.\n");
-
- char line[128] = {0};
- errno = 0;
- while (1) {
- if (fgets(line, 127, file)) {
- //if (read(fd, line, 127) > 0) {
- printf("### [%s] ###\n", line);
- }
- usleep(100000);
- }
-
- close(fd);
- //unlink(FIFO_FFPLAY);
-
- return 0;
- }