Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 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. srand(time(0));
  26. for(int i = 0; i < buf_size; ++i)
  27. buf[i] = rand() % 26 + 64;
  28.  
  29. printf("%s\n", buf);
  30. write(((argument*)arg)->fd, (void*)buf, buf_size);
  31. sleep(1);
  32. }
  33. printf("Thread closed\n");
  34. pthread_exit((void*)1030);
  35. }
  36.  
  37. int main()
  38. {
  39. const char name[] = "my_fifo";
  40. int retval;
  41. argument arg;
  42. arg.flag = 1;
  43. pthread_t thread;
  44. if(!mkfifo(name, 0766))
  45. printf("channel created\n");
  46. arg.fd = open(name, O_CREAT | O_WRONLY);
  47. if(arg.fd != -1)
  48. printf("channel opened for WR\n");
  49. if(!pthread_create(&thread, NULL, func, (void*)&arg))
  50. printf("pthread created\n");
  51. getchar();
  52. arg.flag = 0;
  53. pthread_join(thread, (void**)&retval);
  54. close(arg.fd);
  55. unlink(name);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement