Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <unistd.h>
- using namespace std;
- struct sharedMemory
- {
- int right;
- int flag[2];
- };
- int Id;
- sharedMemory* segment;
- // I omitted the other functions (like proccess()), they work fine... I guess the error is somewhere here, in the main() structure
- void delete(int errorCode)
- {
- (void) shmdt((char *) segment);
- (void) shmctl(Id, IPC_RMID, NULL);
- exit(0);
- }
- int main()
- {
- Id = shmget(IPC_PRIVATE, sizeof(sharedMemory)*100, 0600);
- if (Id == -1)
- {
- cout << "Shared memory couldn't be allocated. The end."
- << endl;
- return -1;
- }
- segment = (sharedMemory *) shmat(Id, NULL, 0);
- segment->right = 0;
- segment->flag[0] = 0;
- segment->flag[1] = 0;
- sigset(SIGINT, delete);
- if (fork() == 0)
- {
- proccess(0);
- exit(0);
- }
- if (fork() == 0)
- {
- proccess(1);
- exit(0);
- }
- wait();
- wait();
- delete(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement