Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<errno.h>
- #include<iostream>
- #include<string>
- #include<sys/types.h>
- #include<sys/ipc.h>
- #include<unistd.h>
- #include<fcntl.h>
- #include<semaphore.h>
- using namespace std;
- int main( int argc, char** argv ) {
- sem_t* mysem;
- int oflag = O_CREAT;
- mode_t mode = 0644;
- const char semname[] = "/mysem";
- unsigned value = 1;
- int sts;
- mysem = sem_open(semname,oflag,mode,value);
- char c = cin.get();
- char buf[1];
- cout << "Process 1 checking semaphore..." << endl;
- sem_wait( mysem );
- cout << "Process 1 in critical section..." << endl;
- int fd = open( "file.txt", O_RDWR );
- read( fd, buf, 1 );
- cout << buf[0] << endl;
- cin.get();
- cin.get();
- close( fd );
- cout << "Unlock the remaphore" << endl;
- sem_post( mysem);
- cout << "Crticl section unlocked" << endl;
- sem_unlink( mysem );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement