Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <time.h>
- #include <string.h>
- #include <stdint.h>
- #include <dirent.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <inttypes.h>
- #include <errno.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <math.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- enum { N = 3 };
- int
- get_class(unsigned ip)
- {
- if (!(ip & (1U << 31))) {
- return 0;
- }
- if (!(ip & (1U << 30))) {
- return 1;
- }
- return 2;
- }
- int
- main(int argc, char *argv[])
- {
- int semid = semget(IPC_PRIVATE, N, 0666);
- int shmid = shmget(IPC_PRIVATE, N * sizeof(unsigned), 0666);
- unsigned *mas = shmat(shmid, NULL, 0), addr;
- for (int i = 0; i < N; ++i) {
- if (!fork()) {
- while (!semop(semid, &(struct sembuf) {i, -1, 0}, 1)) {
- unsigned res;
- if (!i) {
- res = mas[i] % (1U << 24);
- } else if (i == 1) {
- res = mas[i] % (1U << 16);
- } else {
- res = mas[i] % (1U << 8);
- }
- printf("%d %x\n", i + 1, res);
- semop(semid, &(struct sembuf) {i, -1, 0}, 1);
- }
- shmdt(mas);
- return 0;
- }
- }
- while (scanf("%o", &addr) == 1) {
- int num = get_class(addr);
- mas[num] = addr;
- semop(semid, &(struct sembuf) {num, 2, 0}, 1);
- semop(semid, &(struct sembuf) {num, 0, 0}, 1);
- }
- shmdt(mas);
- semctl(semid, N, IPC_RMID, NULL);
- shmctl(shmid, IPC_RMID, NULL);
- while (wait(NULL) > 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement