Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <sys/msg.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- int main(int argc, char const *argv[])
- {
- if (argc < 2) {
- return 1;
- }
- int *children = calloc(argc - 1, sizeof(int));
- int shmid = shmget(IPC_PRIVATE, sizeof(unsigned long long) * (argc - 1), 0666);
- unsigned long long *shmaddr = shmat(shmid, NULL, 0);
- for (int i = 0; i < argc - 1; ++i) {
- pid_t pid = fork();
- if (pid > 0) {
- children[i] = pid;
- } else if (!pid) {
- long long cur1 = strtoll(argv[i + 1], NULL, 10);
- unsigned long long sum = 0;
- unsigned long long cur = cur1;
- if (cur1 < 0) {
- cur *= (-1);
- }
- while (cur) {
- sum += (cur % 4);
- cur /= 4;
- }
- shmaddr[i] = sum;
- exit(0);
- }
- }
- for (int i = 0; i < argc - 1; ++i) {
- wait(NULL);
- }
- for (int i = 0; i < argc - 1; ++i) {
- printf("%lld\n", shmaddr[i]);
- fflush(stdout);
- }
- free(children);
- shmdt(shmaddr);
- shmctl(shmid, IPC_RMID, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement