11.07.2014 Views

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

ts.tv_nsec = 0;<br />

while(1)<br />

nanosleep (&ts, NULL);<br />

exit(0);<br />

Esercizio 6:<br />

Esempio di utilizzo di un segnale (SIGUSR1) <strong>per</strong> modificare il comportamento di un processo.<br />

Il programma ad <strong>in</strong>tervalli di 1 secondo esegue un conteggio che viene <strong>in</strong>vertito quando si<br />

riceve il segnale (SIGUSR1). Per <strong>in</strong>viare i segnali al processo eseguire il comando kill -10<br />

<strong>in</strong> un altro shell.<br />

#<strong>in</strong>clude <br />

<strong>in</strong>t stato=1;<br />

<strong>in</strong>t i=0;<br />

void handler(<strong>in</strong>t signo)<br />

{<br />

stato = !stato;<br />

}<br />

<strong>in</strong>t ma<strong>in</strong>()<br />

{<br />

sigset_t set;<br />

struct sigaction action, old_action;<br />

/* azzera tutti i f<strong>la</strong>g <strong>del<strong>la</strong></strong> maschera sa_mask nel<strong>la</strong> struttura action */<br />

sigemptyset(&action.sa_mask);<br />

action.sa_handler = handler;<br />

action.sa_f<strong>la</strong>gs = 0;<br />

/* assegna action <strong>per</strong> <strong>la</strong> gestione di SIGUSR1 */<br />

if (sigaction(SIGUSR1, &action, &old_action) == -1)<br />

<strong>per</strong>ror("sigaction error");<br />

for(;;)<br />

{<br />

if (stato)<br />

pr<strong>in</strong>tf("%d\n", i++);<br />

else<br />

pr<strong>in</strong>tf("%d\n", i--);<br />

}<br />

sleep(1);<br />

exit(0);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!