SHARE
TWEET

Untitled

a guest Jun 27th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <semaphore.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <sys/stat.h>
  5. #include <sys/mman.h>
  6. #include <sys/shm.h>
  7. #include <unistd.h>
  8. #include <iostream>
  9. #include <cstring>
  10. #include <fstream>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13.  
  14. using namespace std;
  15.  
  16. #define MEMORY_ONE "/shm_one"
  17. #define SEMAPHORE_ONE "/sem_one"
  18. #define SIZE 4096
  19.  
  20. int main() {
  21.  
  22.     int mc_fd;
  23.     char * ptr;
  24.    
  25.     // Clean MEMORY and SEMAPHORE
  26.     shm_unlink(MEMORY_ONE);
  27.     sem_unlink(SEMAPHORE_ONE);
  28.    
  29.     // Create SHARED MEMORY
  30.     mc_fd = shm_open(MEMORY_ONE, O_CREAT | O_RDONLY, 0666);
  31.     if(mc_fd == -1) {
  32.         cerr << "Fallimento nell'apertura della memoria condivisa" << endl;
  33.         exit(-1);
  34.     }
  35.    
  36.     // Map SHARED MEMORY
  37.     ptr = (char *) mmap(NULL, SIZE, PROT_READ, MAP_SHARED, mc_fd, 0);
  38.     if(ptr == MAP_FAILED) {
  39.         cerr << "Fallimento nella mappatura" << endl;
  40.         exit(-1);
  41.     }
  42.    
  43.     // Create SEMAPHORE
  44.     sem_t * sem_id = sem_open(SEMAPHORE_ONE,
  45.                               O_CREAT, S_IRUSR | S_IWUSR, 1);
  46.  
  47.     cout << "SEGMENTATION avviato..." << endl;
  48.     while(true) {
  49.         //sleep(6);
  50.         // -------------------------------------------------------------
  51.         sem_wait(sem_id);
  52.         cout << "Start -> SEZIONE CRITICA!" << endl;
  53.  
  54.         cout << "BUFFER FOTO: " << ptr;
  55.        
  56.         sem_post(sem_id);
  57.         cout << "Stop -> SEZIONE CRITICA!" << endl;
  58.         // -------------------------------------------------------------
  59.     }
  60.    
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top