Guest User

Untitled

a guest
Nov 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include<iostream>
  2. #include<unistd.h>
  3. #include<sys/shm.h>
  4. #include<sys/stat.h>
  5. #include<sys/wait.h>
  6. using namespace std;
  7.  
  8. int main(){
  9. int segid, pid;
  10. char *memory;
  11. const int size = 2048;
  12. segid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
  13. if(segid == -1)
  14. cout << "Shared memory allocation failed\n";
  15. else{
  16. memory = (char*)shmat(segid, NULL, 0);
  17. pid = fork();
  18. if(pid == -1)
  19. cout << "Fork creation failed\n";
  20. else if(pid == 0){
  21. cout << "Child writing to shared memory\n";
  22. sprintf(memory, "Hello World");
  23. }
  24. else {
  25. wait(NULL);
  26. cout << "Parent reading from shared memory\n";
  27. cout << "Message: " << memory << "\n";
  28. }
  29. shmdt(memory);
  30. shmctl(segid, IPC_RMID, NULL);
  31. }
  32.  
  33. return 0;
  34. }
Add Comment
Please, Sign In to add comment