Advertisement
DimaT1

osi

Apr 8th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/ipc.h>
  5. #include <sys/shm.h>
  6.  
  7. #define MAX_LEN   10000
  8. #define SELF_FILE "06-1-write.c"
  9.  
  10. int main(int argc, char *argv[]) {
  11.     char c;
  12.     FILE *self;
  13.     int i = 0;
  14.  
  15.     char *array;
  16.     int shmid;
  17.     int new = 1;
  18.     char pathname[] = SELF_FILE;
  19.     key_t key;
  20.  
  21.     if ((key = ftok(SELF_FILE, 0)) < 0) {
  22.         printf("Can\'t generate key");
  23.         exit(-1);
  24.     }
  25.  
  26.     if ((shmid = shmget(key, MAX_LEN * sizeof(char), 0666 | IPC_CREAT | IPC_EXCL)) < 0) {
  27.         if (errno != EEXIST) {
  28.             printf("Can\'t create shared memory\n");
  29.             exit(-1);
  30.         } else {
  31.             if ((shmid = shmget(key, MAX_LEN * sizeof(char), 0)) < 0) {
  32.                 printf("Can\'t find shared memory\n");
  33.                 exit(-1);
  34.             }
  35.             new = 0;
  36.         }
  37.     }
  38.  
  39.     if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
  40.         printf("Can't attach shared memory\n");
  41.         exit(-1);
  42.     }
  43.  
  44.     self = fopen(SELF_FILE, "r");
  45.     if (!self) {
  46.         printf("Can't read file");
  47.         exit(-1);
  48.     }
  49.  
  50.     while ((c = getc(self)) != EOF) {
  51.         array[i++] = c;
  52.     }
  53.     fclose(self);
  54.    
  55.     if (shmdt(array) < 0) {
  56.         printf("Can't detach shared memory\n");
  57.         exit(-1);
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67. #include <errno.h>
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <sys/ipc.h>
  71. #include <sys/shm.h>
  72.  
  73. #define MAX_LEN   10000
  74. #define SELF_FILE "06-1-write.c"
  75.  
  76. int main(int argc, char *argv[]) {
  77.     char c;
  78.     int i;
  79.  
  80.     char *array;
  81.     int shmid;
  82.     int new = 1;
  83.     char pathname[] = SELF_FILE;
  84.     key_t key;
  85.  
  86.     if ((key = ftok(SELF_FILE, 0)) < 0) {
  87.         printf("Can't generate key");
  88.         exit(-1);
  89.     }
  90.  
  91.     if ((shmid = shmget(key, MAX_LEN * sizeof(char), 0666 | IPC_CREAT | IPC_EXCL)) < 0) {
  92.         if (errno != EEXIST) {
  93.             printf("Can't create shared memory\n");
  94.             exit(-1);
  95.         } else {
  96.             if ((shmid = shmget(key, MAX_LEN * sizeof(char), 0)) < 0) {
  97.                 printf("Can't find shared memory\n");
  98.                 exit(-1);
  99.             }
  100.             new = 0;
  101.         }
  102.     }
  103.  
  104.     if ((array = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
  105.         printf("Can't attach shared memory\n");
  106.         exit(-1);
  107.     }
  108.  
  109.     for (i = 0; i < MAX_LEN; ++i) {
  110.         putchar(array[i]);
  111.     }
  112.  
  113.     if (shmdt(array) < 0) {
  114.         printf("Can't detach shared memory\n");
  115.         exit(-1);
  116.     }
  117.  
  118.     shmctl(shmid, IPC_RMID, NULL);
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement