Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <semaphore.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <sys/mman.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <iostream>
- #include <cstring>
- #include <fstream>
- #include <stdio.h>
- #include <fcntl.h>
- using namespace std;
- #define MEMORY_ONE "/shm_one"
- #define SEMAPHORE_ONE "/sem_one"
- #define SIZE 4096
- int main() {
- int mc_fd;
- char * ptr;
- // Clean MEMORY and SEMAPHORE
- shm_unlink(MEMORY_ONE);
- sem_unlink(SEMAPHORE_ONE);
- // Create SHARED MEMORY
- mc_fd = shm_open(MEMORY_ONE, O_CREAT | O_RDONLY, 0666);
- if(mc_fd == -1) {
- cerr << "Fallimento nell'apertura della memoria condivisa" << endl;
- exit(-1);
- }
- // Map SHARED MEMORY
- ptr = (char *) mmap(NULL, SIZE, PROT_READ, MAP_SHARED, mc_fd, 0);
- if(ptr == MAP_FAILED) {
- cerr << "Fallimento nella mappatura" << endl;
- exit(-1);
- }
- // Create SEMAPHORE
- sem_t * sem_id = sem_open(SEMAPHORE_ONE,
- O_CREAT, S_IRUSR | S_IWUSR, 1);
- cout << "SEGMENTATION avviato..." << endl;
- while(true) {
- //sleep(6);
- // -------------------------------------------------------------
- sem_wait(sem_id);
- cout << "Start -> SEZIONE CRITICA!" << endl;
- cout << "BUFFER FOTO: " << ptr;
- sem_post(sem_id);
- cout << "Stop -> SEZIONE CRITICA!" << endl;
- // -------------------------------------------------------------
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement