Kackhaufen200

task1_a.c

Apr 26th, 2020
291
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #define SHMSZ 27
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     int shmid;
  15.     key_t key;
  16.     int *shm, *s;
  17.     char path[50];
  18.     int choice[50] = {0};
  19.  
  20.     key = 2020;
  21.  
  22.     if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
  23.         perror("shmget");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
  28.         perror("shmat");
  29.         exit(EXIT_FAILURE);
  30.     }
  31.  
  32.     s = shm;
  33.  
  34.     *s = strtol(argv[1], NULL, 10);
  35.  
  36.     strcpy(path, "/tmp/");
  37.     strcat(path, "task1");
  38.  
  39.     mkfifo(path, 0666);
  40.  
  41.     int file = open(path, O_RDONLY);
  42.     read(file, choice, sizeof(choice));
  43.  
  44.     printf("Result: ");
  45.  
  46.     for(int i = 0; choice[i] != '\0'; i++) {
  47.         printf("%d", choice[i]);
  48.     }
  49.     //printf("%d", choice);
  50.     printf("\n");
  51.  
  52.     return EXIT_SUCCESS;
  53. }
RAW Paste Data