Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <signal.h>
  6. #include <pqueue.h>
  7. #include <time.h>
  8.  
  9. #define PQUEUE_H
  10.  
  11. char *filename = "queue.dat";
  12.  
  13.  
  14. int id=0;
  15. int *itemId = &id;
  16. int bufforSize=3;
  17.  
  18.  
  19. typedef struct item item;
  20. struct item {
  21. int id;
  22. };
  23.  
  24.  
  25. item *
  26. produce() {
  27. time_t t;
  28. int timeX = rand() % 5 + 1;
  29. item *it = (item *)malloc(sizeof(item));
  30.  
  31. sleep(timeX);
  32. it->id = *itemId;
  33. (*itemId) += 1;
  34. return it;
  35. }
  36.  
  37. void
  38. consume(item *it) {
  39. time_t t;
  40. int timeX = rand() % 5 + 1;
  41. sleep(timeX);
  42. *itemId -=1;
  43. printf("-----%d-----\n",*itemId);
  44. free(it);
  45. }
  46.  
  47.  
  48. void
  49. producer(pid_t childPid, pqueue **phead) {
  50. if(*itemId != bufforSize){
  51.  
  52. item* producedElem = produce();
  53.  
  54. int k = rand() % 100 + 1;
  55. printf("Im in producer!\n");
  56.  
  57. qinsert(phead, producedElem, k);
  58. printf("%d\n",(*phead)->k);
  59. qserialize(*phead, sizeof(producedElem), filename);
  60. }
  61. else
  62. {
  63. printf("########################## ELSE ########################## \n");
  64. sleep(5);
  65. kill(childPid, SIGSTOP);
  66. }
  67. //qserialize(pqueue *head, size_t ndata, char *filename);
  68. //otworz plik, wpisz, wyjd
  69.  
  70.  
  71. }
  72.  
  73. void
  74. consumer(pqueue **phead) {
  75. //qpop()
  76. printf("Hi, consumere here!\n");
  77.  
  78. //qunserialize()
  79. //qunserialize(&qu, sizeof(item), filename);
  80. //consume();
  81.  
  82.  
  83.  
  84. }
  85.  
  86. int
  87. main(int argc, char **argv) {
  88. time_t t;
  89. srand(time(0));
  90. pid_t pid;
  91. pqueue *qu = NULL;
  92. int ifBuffFree = 1;
  93. qserialize(qu, sizeof(item), filename);
  94. int forkRes=fork();
  95. while(1){
  96. if(forkRes==0)
  97. {
  98. printf("Child here! Lets go to producer!\n");
  99. if(*itemId== BUFSIZ){ ifBuffFree=0;}
  100. producer(getpid(),&qu);
  101. printf("-----%d-----\n",*itemId);
  102. sleep(3);
  103. }
  104. else if(forkRes>0)
  105. {
  106.  
  107. printf("Parent here! Lets go to consumer!\n");
  108. //while(!)
  109. qunserialize(&qu, sizeof(item), filename);
  110. consumer(&qu);
  111. qserialize(qu, sizeof(item), filename);
  112. //qunserialize(&qu, sizeof(item), filename);
  113. //qunserialize(pqueue **phead, size_t ndata, char *filename);
  114. sleep(3);
  115. }
  116. else
  117. {
  118. printf("Aaaaa");
  119. }
  120. }
  121.  
  122. /* watch -n 1 ps -l --forest */
  123.  
  124. /* create empty queue */
  125.  
  126.  
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement