| 
      .amico.
 |  | spikey |  
 
    Registered: Oct 2010 Posts: 34 (0.01 al dì)
 Location: Milano
 Corso: Informatica
 Anno: 3°
 Time Online: 1 Day, 20:29:12 [...]
 Status: Offline
 
    Edit | Report | IP: Logged | Ciao,
 dunque l'istruzione incriminata è questa inizializzazione:
 
 sem_init(mutex, 0, 0);
 
 se leggi la man page di sem_init guarda cosa ti dice sul secondo parametro:
 
 $ man sem_init
 
 int sem_init(sem_t *sem, int pshared, unsigned int value);
 
 The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes.
 
 If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be  located  at  some  address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap).
 
 If  pshared  is  nonzero,  then  the  semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)).  (Since a child created by fork(2) inherits its parent's memory mappings, it can  also  access the  semaphore.)   Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc.
 
 Per cui nel tuo caso, usando una shared memory tra processi, il secondo parametro pshared non va inizializzato a 0 bensì ad un valore != 0. Difatti trasformando la tua istruzione incriminata in:
 
 sem_init(mutex, 1, 0);
 
 tutto funziona alla perfezione!
  
 Ti do alcuni consigli per la vita:
 
 1) Prima di usare qualsiasi library call, system call, ecc... guarda sempre la relativa man page
 2) Studiati questi tool: ltrace e strace
 
 Ciao
 Last edited by spikey on 12-05-2015 at 13:22 |