Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <unistd.h>
  8.  
  9. using namespace std;
  10.  
  11. struct sharedMemory
  12. {
  13.     int right;
  14.     int flag[2];
  15. };
  16.  
  17. int Id;
  18. sharedMemory* segment;
  19.  
  20. // I omitted the other functions (like proccess()), they work fine... I guess the error is somewhere here, in the main() structure
  21.  
  22. void delete(int errorCode)
  23. {
  24.     (void) shmdt((char *) segment);
  25.     (void) shmctl(Id, IPC_RMID, NULL);
  26.     exit(0);
  27. }
  28.  
  29. int main()
  30. {
  31.     Id = shmget(IPC_PRIVATE, sizeof(sharedMemory)*100, 0600);
  32.     if (Id == -1)
  33.     {
  34.         cout << "Shared memory couldn't be allocated. The end."
  35.         << endl;
  36.         return -1;
  37.     }
  38.     segment = (sharedMemory *) shmat(Id, NULL, 0);
  39.     segment->right = 0;
  40.     segment->flag[0] = 0;
  41.     segment->flag[1] = 0;
  42.     sigset(SIGINT, delete);
  43.     if (fork() == 0)
  44.     {
  45.         proccess(0);
  46.         exit(0);
  47.     }
  48.     if (fork() == 0)
  49.     {
  50.         proccess(1);
  51.         exit(0);
  52.     }
  53.     wait();
  54.     wait();
  55.     delete(0);
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement