Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <sys/mman.h>
  10.  
  11. #define buf_size 16
  12.  
  13. struct argument
  14. {
  15. int flag;
  16. int fd;
  17. };
  18.  
  19. void* func(void* arg)
  20. {
  21. printf("thread enter\n");
  22. char buf[buf_size];
  23. while(((argument*)arg)->flag)
  24. {
  25. if(read(((argument*)arg)->fd, (void*)buf, buf_size) > 0)
  26. printf("%s\n", buf);
  27. }
  28. printf("thread closed\n");
  29. pthread_exit((void*)1031);
  30. }
  31.  
  32. int main()
  33. {
  34. const char name[] = "my_fifo";
  35. int retval;
  36. argument arg;
  37. arg.flag = 1;
  38. pthread_t thread;
  39. mkfifo(name, 0766);
  40. arg.fd = open(name, O_CREAT | O_RDONLY | O_NONBLOCK);
  41. pthread_create(&thread, NULL, func, (void*)&arg);
  42. getchar();
  43. arg.flag = 0;
  44. pthread_join(thread, (void**)&retval);
  45. close(arg.fd);
  46. unlink(name);
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement