Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include<errno.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<sys/types.h>
  5. #include<sys/ipc.h>
  6. #include<unistd.h>
  7. #include<fcntl.h>
  8. #include<semaphore.h>
  9.  
  10. using namespace std;
  11.  
  12. int main( int argc, char** argv ) {
  13.  
  14.  
  15. sem_t* mysem;
  16. int oflag = O_CREAT;
  17. mode_t mode = 0644;
  18. const char semname[] = "/mysem";
  19. unsigned value = 1;
  20. int sts;
  21.  
  22. mysem = sem_open(semname,oflag,mode,value);
  23.  
  24. char c = cin.get();
  25.  
  26. char buf[1];
  27.  
  28. cout << "Process 2 checking semaphore..." << endl;
  29. sem_wait( mysem );
  30. cout << "Process 2 in critical section" << endl;
  31. int fd = open( "file.txt", O_RDWR );
  32. read( fd, buf, 1 );
  33. cout << buf[0] << endl;
  34. sem_post( mysem);
  35. close( fd );
  36. cout << "The process unlocked the critical sectioin" << endl;
  37. cin.get();
  38.  
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement