Dsy Network www | forum | my | didattica | howto | wiki | el goog | stats | blog | dona | rappresentanti
Homepage
 Register   Calendar   Members  Faq   Search  Logout 
.dsy:it. : Powered by vBulletin version 2.3.1 .dsy:it. > Didattica > Corsi N - Z > Sistemi operativi I > Esercizio Sleep-Sort con semafori
  Last Thread   Next Thread
Author
Thread    Expand all | Contract all    Post New Thread    Post A Reply
Collapse
richard.greco
.novellino.

User info:
Registered: Oct 2014
Posts: 2 (0.00 al dì)
Location:
Corso: Informatica per la comunicazione digitale
Anno: 1
Time Online: 0:26:17 [...]
Status: Offline

Post actions:

Edit | Report | IP: Logged
Esercizio Sleep-Sort con semafori

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <sys/stat.h>

int main(){
int ID = shmget(IPC_PRIVATE, sizeof(sem_t), S_IRUSR | S_IWUSR), i, j=0;
sem_t *mutex = (sem_t * )shmat(ID, NULL, 0);
sem_init(mutex, 0, 0);
pid_t pid;

printf("Inserire numero (terminare con 0): ");
scanf("%d", &i);
while(i!=0){
j++;
if((pid = fork())==0){
printf("\nSto aspettando pid=%d, i=%d\n", getpid(), i);
sem_wait(mutex);
sem_post(mutex);
printf("\nVado pid=%d, i=%d\n", getpid(), i);
sleep(i);
printf("%d ", i);
shmdt(mutex);
return 0;
}
printf("Inserire numero (terminare con 0): ");
scanf("%d", &i);
}

printf("\npadre?");
sem_post(mutex);

for(i=0; i<j; i++)
wait(NULL);
printf("\n");
sem_destroy(mutex);
shmdt(mutex);
shmctl(ID, IPC_RMID, NULL);
return 0;
}

Non capisco come mai non funziona, qualcuno saprebbe aiutarmi? in sostanza il processo padre non esce dal while e non raggiunge mai il "sem_post", che sbloccherebbe tutti i figli. Non raggiunge neanche la printf addirittura.

09-05-2015 18:08
Click Here to See the Profile for richard.greco Click here to Send richard.greco a Private Message Find more posts by richard.greco Add richard.greco to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
Collapse
spikey
.amico.

User info:
Registered: Oct 2010
Posts: 34 (0.01 al dì)
Location: Milano
Corso: Informatica
Anno:
Time Online: 1 Day, 20:29:12 [...]
Status: Offline

Post actions:

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

12-05-2015 13:03
Click Here to See the Profile for spikey Click here to Send spikey a Private Message Find more posts by spikey Add spikey to your buddy list Printer Friendly version Email this Article to a friend Reply w/Quote
All times are GMT. The time now is 18:43.    Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread | Add to Bookmarks

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

Powered by: vBulletin v2.3.1 - Copyright ©2000 - 2002, Jelsoft Enterprises Limited
Mantained by dsy crew (email) | Collabora con noi | Segnalaci un bug | Archive | Regolamento | Licenze | Thanks | Syndacate
Pagina generata in 0.033 seconds (73.48% PHP - 26.52% MySQL) con 26 query.