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;
- // Open SHARED MEMORY
- mc_fd = shm_open(MEMORY_ONE, O_RDWR, 0666);
- if(mc_fd == -1) {
- cerr << "Fallimento nell'apertura della memoria condivisa" << endl;
- exit(-1);
- }
- // Map SHARED MEMORY
- ptr = (char *) mmap(NULL, SIZE, PROT_WRITE, MAP_SHARED, mc_fd, 0);
- if(ptr == MAP_FAILED) {
- cerr << "Fallimento nella mappatura" << endl;
- exit(-1);
- }
- // Open SEMAPHORE
- sem_t * sem_id = sem_open(SEMAPHORE_ONE, 0);
- cout << "WEBCAM avviata..." << endl;
- // -----------------------------------------------------------------
- sem_wait(sem_id);
- cout << "Start -> SEZIONE CRITICA!" << endl;
- const char * message[] = {
- "Hello",
- "Shared",
- "Memory!"
- };
- ftruncate(mc_fd, sizeof(message));
- for (int i = 0; i < strlen(* message); i++) {
- sprintf(ptr, "%s ", message[i]);
- ptr += strlen(message[i]);
- }
- sem_post(sem_id);
- cout << "Stop -> SEZIONE CRITICA!" << endl;
- // -----------------------------------------------------------------
- cout << "WEBCAM disattivata..." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement