fork download
  1.  
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/shm.h>
  5. #include <sys/ipc.h>
  6. #include <string.h>
  7.  
  8.  
  9. int main()
  10. {
  11. int shmid;
  12. char *shared_memory;
  13. char buff[100];
  14.  
  15.  
  16. shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);
  17.  
  18.  
  19. if (shmid == -1) {
  20. perror("shmget failed");
  21. return 1;
  22. }
  23.  
  24.  
  25. printf("Key of shared memory is %d\n", shmid);
  26.  
  27.  
  28. shared_memory = (char *)shmat(shmid, NULL, 0);
  29.  
  30.  
  31. if (shared_memory == (char *)-1) {
  32. perror("shmat failed");
  33. return 1;
  34. }
  35.  
  36.  
  37. printf("Enter some data to write to shared memory: ");
  38. read(0, buff, 100);
  39.  
  40.  
  41. strcpy(shared_memory, buff);
  42.  
  43.  
  44. printf("You wrote: %s\n", shared_memory);
  45.  
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Key of shared memory is 0
Enter some data to write to shared memory: You wrote: