Signals (shmata) - Asygxrona symvanta pou emfanizontai tyxaia sthn diergasia
-software intrerupts me isxyrh emplokh tou leitourgikou systhmatos
-SIGXXX(X)
p.x.
-SIGTSP -SIGALRM
-SIGUSR1 -SIGABRT
-SIGUSR2 -SIGTSP
_____________
|process |
| |
|-------- |
|funct | |
|-------| | signal
| ----|<-------------------
|___________|
signal handler: an symvei to "signal", kane "funct" (me paysh ths diergasias)
Enw to programma trexei kanonika, h diergasia tha diakopei kai tha ektelestei h synarthsh. kai sth synexeia,
an yparxei h dynatothta, tha synxistei h ektelesh tou programmatos
Energeies kata th lhpsh enos signal:
-IGNORE (agnohse)
-CATCH (SIGNAL HANDLER) -----> grafoume th synarthsh-xeiristh
-DEFAULT SIGNAL HANDLER-------> orismenos apo to leitourgiko systhma
Paradeigmata signals:
>ctrl-c SIGINT termatismos diergasias
>ctrl-z SIGTSTP anastolh diergasias
>fg SIGCONT synexish diergasias
<c code>
void (*signal (int signo, void (*funct) (int)) (int)
</c code>
signo: onoma tou shmatos
funct:
-SIG_IGN agnohse to signal
-SIG_DFL kathorise dieythynsh synarthshs se signal-handler
h synarthsh leei sto leitourgiko systhma: "an symvei to signal signo, ektelese thn synarthsh funct"
<c code>
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int signo); //apostolh shmatos signo sthn diergasia pid
int raise(int signo); //apostolh tou singal signo sthn kalousa diergasia
int alarm(int time); //SIGALRM time in seconds
int pause(void); //anastolh trexousas diergasias mexri na lhfthei kapoio signal
void sig_usr(int signo)
{
if(signo == SIGUSR1)
printf("received SIGUSR1");
else if(signo == SIGUSR2)
printf("received SIGUSR2");
else
printf("received signal %d", signo);
}
int main(void)
{
signal(SIGUSR1, sig_usr);
signal(SIGUSR2, sig_usr);
for( ; ; )
pause();
}
</c code
terminal:
$alpha.out&
pid=4858
$kill -USR1 4858 //SIGUSR1
$kill -USR2 4858 //SIGUSR2
$kill 4858 //SIGTERM (termatismos)
<c code>
#include <sys/types.h>
#include <signal.h>
void sighandler(int signo)
{
switch(signo)
{
case SIGUSR1:
printf("received SIGUSR1");
break;
case SIGUSR2:
printf("received SIGUSR2");
break;
default:
break;
}
}
int main(void)
{
int i, parrent_id, child_id, status;
signal(SIGUSR1, sighandler);
signal(SIGUSR2, sighandler);
parrent_id = getpid();
child_id = fork();
if(child_id == 0)
{
kill(parrent_id, SIGUSR1);
for( ; ; )
pause();
}
else
{
kill(child_id, SIGUSR2);
kill(child_id, SIGTERM);
wait(&status); //enallatika, waitpid(child_id, &status);
}
}
</c code>