Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/wait.h>
  4. #include <stdlib.h>
  5. #include <signal.h>
  6. #include <time.h>
  7. #include <math.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/ipc.h>
  11. #include <sys/msg.h>
  12. #include <time.h>
  13. #include <sys/shm.h>
  14.  
  15.  
  16. bool* mem;
  17. int id;
  18.  
  19. void f() {
  20.     mem[id] = 0;
  21. }
  22.  
  23. int main(int argc, char* argv[]) {
  24.     signal(SIGKILL, f);
  25.     id = atoi(argv[1]);
  26.     int delay = atoi(argv[2]);
  27.     if (id == 0) {
  28.         int shm_id = shmget(42, 3, IPC_CREAT | 0666);
  29.         mem = (bool*)shmat(shm_id, null, 0);
  30.         mem[0] = 0;
  31.         mem[1] = 0;
  32.         mem[2] = 0;
  33.     } else {
  34.         int shm_id = shmget(42, 3, 0666);
  35.         mem = (bool*)shmat(shm_id, 0, 0);
  36.         while (1) {
  37.             sleep(delay);
  38.             if (mem[0] || mem[1] || mem[2])
  39.                 printf("Файл занят\n");
  40.             else {
  41.                 mem[id] = 1;
  42.                 printf("Запись в файл...\n");
  43.                 FILE* f = fopen("temp", "a");
  44.                 fprintf(f, "%d\n", id);
  45.                 sleep(1);
  46.                 fclose(f);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement