Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/shm.h>
  3. #include <sys/ipc.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/sem.h>
  10. #include <string.h>
  11. #include <wait.h>
  12.  
  13. //-----------------------------------------------
  14. //-----------------------------------------------
  15. struct plist
  16. {
  17. char name[50];
  18. int wtime;
  19. int dtime;
  20. };
  21.  
  22. struct process
  23. {
  24. char name[50];
  25. int count;
  26. };
  27.  
  28. int find(struct process a, struct plist *b, int c)
  29. {
  30. for(int i = 0; i < c; i++)
  31. {
  32. if(strcmp(a.name, b[i].name) == 0)
  33. {
  34. return i;
  35. }
  36. }
  37. }
  38.  
  39. //-----------------------------------------------
  40. //-----------------------------------------------
  41.  
  42.  
  43. int main()
  44. {
  45.  
  46. //------------------------------------------------------------
  47.  
  48. struct plist list[15];
  49. struct process dirty;
  50. char rubish[50];
  51.  
  52. //-------------------------------------------------------
  53.  
  54. FILE* fi;
  55. fi = fopen("washer.txt", "r");
  56. int w = 0;
  57. while(fscanf(fi, "%s %d\n", list[w].name, &list[w].wtime) == 2)
  58. {
  59. w++;
  60. }
  61. fclose(fi);
  62. //-------------------------------------------------------
  63.  
  64. fi = fopen("scratcher.txt", "r");
  65. int s = 0;
  66. while(fscanf(fi, "%s %d\n", rubish, &list[s].dtime) == 2)
  67. {
  68. s++;
  69. }
  70. fclose(fi);
  71.  
  72. //------------------------------------------------------------
  73.  
  74. int semid;
  75. int N;
  76. N = atoi(getenv("TABLE_LIMIT"));
  77. struct sembuf buf[] = { {0, N, 0}, {0, -1, 0}, {0, 1, 0}};
  78. key_t key;
  79. key = ftok("test.c", 0);
  80. //printf("%d\n", key);
  81. semid = semget(key, 1, 0666 | IPC_CREAT);
  82. semop(semid, &buf[0], 1);
  83.  
  84. //-------------------------------------------------------------
  85. int fd[2];
  86. int pid;
  87. pipe(fd);
  88. //-------------------------------------------------------------
  89.  
  90. pid = fork();
  91. int a;
  92. int b;
  93.  
  94. //-------------------------------------------------------------
  95.  
  96. if (pid == 0)
  97. {
  98. fi = fopen("dirty.txt", "r");
  99. close(fd[0]);
  100. while(fscanf(fi, "%s %d\n", dirty.name, &dirty.count) == 2)
  101. {
  102. a = find(dirty, list, w);
  103. //printf("a = %d\n", a);
  104. for(int i = 0; i < dirty.count; i++)
  105. {
  106. printf("Начал мыть %s\n", list[a].name);
  107. sleep(list[a].wtime);
  108. write(fd[1], &a, 4);
  109. semop(semid, &buf[1], 1);
  110. printf("Помыл и поставил на стол %s %d\n", list[a].name, semctl(semid, 0, GETVAL, 0));
  111.  
  112. }
  113.  
  114. }
  115.  
  116. exit(0);
  117. }
  118.  
  119. else
  120. {
  121. close(fd[1]);
  122. while(read(fd[0], &b, 4) != 0)
  123. {
  124. semop(semid, &buf[2], 1);
  125. printf("Взял со стола %s %d\n", list[b].name, semctl(semid, 0, GETVAL, 0));
  126. sleep(list[b].dtime);
  127. printf("Вытер %s\n", list[b].name);
  128. }
  129. semctl(semid, 0, IPC_RMID, 0);
  130. }
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement