Advertisement
_takumi

write.c

Mar 2nd, 2023 (edited)
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/mman.h>
  8. #include <fcntl.h>
  9.  
  10. int main(int argc, char *argv[]) {
  11.     srand(time(NULL));
  12.     char memn[] = "client.c"; //  имя объекта
  13.     char *addr;
  14.     int shm;
  15.     int mem_size = 100; //размер области
  16.     int buff[50];
  17.     //открыть объект
  18.     if ((shm = shm_open(memn, O_RDWR, 0666)) == -1 ) {
  19.         printf("Opening error\n");
  20.         perror("shm_open");
  21.         return 1;
  22.     } else {
  23.         printf("Object is open: name = %s, id = 0x%x\n", memn, shm);
  24.     }
  25.     addr = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, shm, 0);
  26.     if (addr == (char*)-1 ) {
  27.         printf("Error getting pointer to shared memory\n");
  28.         return 1;
  29.     }
  30.     for (int i = 0; i < 50; ++i) {
  31.         buff[i] = rand() % 1001;
  32.     }
  33.     memcpy(addr, buff, 50 * sizeof(int));
  34.     close(shm);
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement