piffy

memoria_condivisa_spedisci

Aug 31st, 2014
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7.  
  8. #define SHM_SIZE 1024  /* La memoria condivisa รจ di 1Kb*/
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     key_t key=42;
  13.     int shmid;
  14.     char *data;
  15.     int mode;
  16.  
  17.     /*  creare il segmento */
  18.     if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
  19.         perror("shmget");
  20.         exit(1);
  21.     }
  22.  
  23.     /* aggancarsi al segmento per ottenerne il puntatore*/
  24.     data = shmat(shmid, (void *)0, 0);
  25.     if (data == (char *)(-1)) {
  26.         perror("shmat");
  27.         exit(1);
  28.     }
  29.  
  30.     /* Usare la memoria condivisa*/
  31.     strcpy(data,"Nuovo dato");
  32.     /* sganciare il segment: */
  33.     if (shmdt(data) == -1) {
  34.         perror("shmdt");
  35.         exit(1);
  36.     }
  37.  
  38.     return 0;
  39. }
Add Comment
Please, Sign In to add comment