Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <mqueue.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <memory.h>
  9. #include <asm/errno.h>
  10. #include <errno.h>
  11. #include <sys/stat.h>
  12.  
  13. static void tfunc(union sigval sv)
  14. {
  15. mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
  16.  
  17. struct sigevent sev;
  18. sev.sigev_notify = SIGEV_THREAD;
  19. sev.sigev_notify_function = tfunc;
  20. sev.sigev_notify_attributes = NULL;
  21. sev.sigev_value.sival_ptr = sv.sival_ptr;
  22. if (mq_notify(mqdes, &sev) == -1){
  23. perror("Napaka pri registraciji obvestil ");
  24. exit(EXIT_FAILURE);
  25. }
  26.  
  27. struct mq_attr attr;
  28. if (mq_getattr(mqdes, &attr) == -1){
  29. perror("Napaka pri pridobivanju atributov ");
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. char* msg = malloc(attr.mq_msgsize + 1);
  34. if (msg == NULL){
  35. perror("Napaka pri rezerviranju pomnilnika ");
  36. exit(EXIT_FAILURE);
  37. }
  38.  
  39. while(1) {
  40. memset(msg, 0, attr.mq_msgsize + 1);
  41.  
  42. ssize_t nr = mq_receive(mqdes, msg, attr.mq_msgsize, NULL);
  43. if (nr == -1) {
  44. if(errno == EAGAIN){
  45. break;
  46. }else{
  47. perror("Napaka pri pridobivanju sporocila ");
  48. exit(EXIT_FAILURE);
  49. }
  50. }
  51.  
  52. printf("Vrsta: %s\n", msg);
  53. }
  54.  
  55. free(msg);
  56. }
  57.  
  58. volatile int stop = 0;
  59. void signal_handler(int sig)
  60. {
  61. if(sig == SIGINT)
  62. {
  63. stop = 1;
  64. }
  65. }
  66.  
  67. void zapri_cev(int fd, char* ime){
  68. close(fd);
  69. unlink(ime);
  70. }
  71.  
  72. mqd_t zapri_vrsto(int mq, char* ime){
  73. mq_close(mq);
  74. mq_unlink(ime);
  75. }
  76.  
  77. int main(int argc, char *argv[]) {
  78. mqd_t mqdes;
  79.  
  80. if (argc != 3) {
  81. fprintf(stderr, "Usage: %s <ime_cevi> <ime_vrste>\n", argv[0]);
  82. exit(EXIT_FAILURE);
  83. }
  84.  
  85. char *ime_cevi = argv[1], *ime_vrste = argv[2];
  86.  
  87. printf("Ime cevi: %s, ime vrste: %s\n", ime_cevi, ime_vrste);
  88.  
  89. mqdes = mq_open(ime_vrste, O_RDONLY|O_CREAT|O_NONBLOCK, 0666);
  90. if (mqdes == (mqd_t) -1){
  91. perror("Napaka pri ustvarjanju cevi ");
  92. return EXIT_FAILURE;
  93. }
  94.  
  95. if(mkfifo(ime_cevi, 0666) == -1){
  96. perror("Napaka pri ustvarjanju cevi ");
  97. zapri_vrsto(mqdes, ime_vrste);
  98. return EXIT_FAILURE;
  99. }
  100.  
  101. int vh_cev = open(ime_cevi, O_RDONLY|O_NONBLOCK);
  102. if(vh_cev == -1)
  103. {
  104. perror("Napaka pri odpiranju cevi ");
  105. zapri_vrsto(mqdes, ime_vrste);
  106. unlink(ime_cevi);
  107. return EXIT_FAILURE;
  108. }
  109.  
  110. if (fcntl(vh_cev,F_SETFL,O_ASYNC|O_NONBLOCK) == -1) {
  111. perror("Napaka v fcntl ");
  112. zapri_vrsto(mqdes, ime_vrste);
  113. zapri_cev(vh_cev, ime_cevi);
  114. return EXIT_FAILURE;
  115. }
  116.  
  117. if (fcntl(vh_cev,F_SETOWN,getpid()) == -1){
  118. perror("Napaka v fcntl ");
  119. zapri_vrsto(mqdes, ime_vrste);
  120. zapri_cev(vh_cev, ime_cevi);
  121. return EXIT_FAILURE;
  122. }
  123.  
  124. struct sigaction sa;
  125. memset(&sa,0,sizeof(sigaction));
  126. sa.sa_handler = signal_handler;
  127. sigaction(SIGINT, &sa, 0);
  128. sigaction(SIGIO, &sa, 0);
  129.  
  130. struct sigevent sev;
  131. sev.sigev_notify = SIGEV_THREAD;
  132. sev.sigev_notify_function = tfunc;
  133. sev.sigev_notify_attributes = NULL;
  134. sev.sigev_value.sival_ptr = &mqdes;
  135. if (mq_notify(mqdes, &sev) == -1){
  136. perror("Napaka pri registraciji obvestil ");
  137. zapri_vrsto(mqdes, ime_vrste);
  138. zapri_cev(vh_cev, ime_cevi);
  139. return EXIT_FAILURE;
  140. }
  141.  
  142. int l;
  143. char buffer[1024];
  144. while(1){
  145. pause();
  146.  
  147. if(stop == 1){
  148. zapri_cev(vh_cev, ime_cevi);
  149. zapri_vrsto(mqdes, ime_vrste);
  150. exit(EXIT_SUCCESS);
  151. }
  152.  
  153. int first = 1;
  154. while(1){
  155. while(1)
  156. {
  157. l = read(vh_cev, buffer, sizeof(buffer));
  158. if(l>0)
  159. {
  160. if(first){
  161. printf("Cev: ");
  162. fflush(stdout);
  163. first = 0;
  164. }
  165. write(1,buffer,l);
  166. }else{
  167. break;
  168. }
  169. }
  170.  
  171. if(l < 0)
  172. {
  173. if(errno != EAGAIN && errno != EWOULDBLOCK)
  174. {
  175. perror("Tezava pri branju iz cevi ");
  176. zapri_cev(vh_cev, ime_cevi);
  177. zapri_vrsto(mqdes, ime_vrste);
  178. exit(EXIT_FAILURE);
  179. }
  180. }else{
  181. break;
  182. }
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement