Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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.  
  13. int main( int argc, char** argv ) {
  14.  
  15.  
  16. sem_t* mysem;
  17. int oflag = O_CREAT;
  18. mode_t mode = 0644;
  19. const char semname[] = "/mysem";
  20. unsigned value = 1;
  21. int sts;
  22.  
  23. mysem = sem_open(semname,oflag,mode,value);
  24.  
  25. char c = cin.get();
  26.  
  27. char buf[1];
  28.  
  29. cout << "Process 1 checking semaphore..." << endl;
  30. sem_wait( mysem );
  31. cout << "Process 1 in critical section..." << endl;
  32. int fd = open( "file.txt", O_RDWR );
  33. read( fd, buf, 1 );
  34. cout << buf[0] << endl;
  35. cin.get();
  36. cin.get();
  37. close( fd );
  38. cout << "Unlock the remaphore" << endl;
  39. sem_post( mysem);
  40.  
  41. cout << "Crticl section unlocked" << endl;
  42. sem_unlink( mysem );
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement