Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/mman.h>
- #define buf_size 16
- struct argument
- {
- int flag;
- int fd;
- };
- void* func(void* arg)
- {
- printf("thread enter\n");
- char buf[buf_size];
- while(((argument*)arg)->flag)
- {
- if(read(((argument*)arg)->fd, (void*)buf, buf_size) > 0)
- printf("%s\n", buf);
- }
- printf("thread closed\n");
- pthread_exit((void*)1031);
- }
- int main()
- {
- const char name[] = "my_fifo";
- int retval;
- argument arg;
- arg.flag = 1;
- pthread_t thread;
- mkfifo(name, 0766);
- arg.fd = open(name, O_CREAT | O_RDONLY | O_NONBLOCK);
- pthread_create(&thread, NULL, func, (void*)&arg);
- getchar();
- arg.flag = 0;
- pthread_join(thread, (void**)&retval);
- close(arg.fd);
- unlink(name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement