Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <dirent.h>
  10. #include <limits.h>
  11. #include <stdlib.h>
  12. #include <inttypes.h>
  13. #include <errno.h>
  14. #include <sys/wait.h>
  15. #include <signal.h>
  16. #include <math.h>
  17. #include <sys/msg.h>
  18. #include <sys/shm.h>
  19. #include <sys/sem.h>
  20.  
  21. enum { N = 3 };
  22.  
  23. int
  24. get_class(unsigned ip)
  25. {
  26. if (!(ip & (1U << 31))) {
  27. return 0;
  28. }
  29. if (!(ip & (1U << 30))) {
  30. return 1;
  31. }
  32. return 2;
  33. }
  34.  
  35. int
  36. main(int argc, char *argv[])
  37. {
  38. int semid = semget(IPC_PRIVATE, N, 0666);
  39. int shmid = shmget(IPC_PRIVATE, N * sizeof(unsigned), 0666);
  40. unsigned *mas = shmat(shmid, NULL, 0), addr;
  41. for (int i = 0; i < N; ++i) {
  42. if (!fork()) {
  43. while (!semop(semid, &(struct sembuf) {i, -1, 0}, 1)) {
  44. unsigned res;
  45. if (!i) {
  46. res = mas[i] % (1U << 24);
  47. } else if (i == 1) {
  48. res = mas[i] % (1U << 16);
  49. } else {
  50. res = mas[i] % (1U << 8);
  51. }
  52. printf("%d %x\n", i + 1, res);
  53. semop(semid, &(struct sembuf) {i, -1, 0}, 1);
  54. }
  55. shmdt(mas);
  56. return 0;
  57. }
  58. }
  59. while (scanf("%o", &addr) == 1) {
  60. int num = get_class(addr);
  61. mas[num] = addr;
  62. semop(semid, &(struct sembuf) {num, 2, 0}, 1);
  63. semop(semid, &(struct sembuf) {num, 0, 0}, 1);
  64. }
  65. shmdt(mas);
  66. semctl(semid, N, IPC_RMID, NULL);
  67. shmctl(shmid, IPC_RMID, NULL);
  68. while (wait(NULL) > 0);
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement