home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
Esignal.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-11-07
|
2KB
|
84 lines
/* Esignal.c
Eseguendo il comando kill -USR1 pid del processo (non SIGUSR1)
si determina se atime e' cambiato in qualche argomento.
Deve eseguire in background */
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
time_t get_atime(char *);
void restat();
time_t * atimes; /* array che memorizza i precedenti atime */
/* Per rendere accessibili argc and argv dal signal handler. */
int nargs;
char ** argvcopy;
int main(int argc, char ** argv)
{
int i;
/* Ignoro momentaneamente il signal SIGUSR1 */
signal( SIGUSR1, SIG_IGN);
if( argc < 2 )
{
fprintf(stderr, "Sintassi: Esignal file1 file2 ...\n");
exit(1);
}
nargs = argc;
argvcopy = argv;
/* Legge gli attributi iniziali dei file in argomento */
atimes = (time_t *) malloc ( (argc - 1)* sizeof(time_t) );
for ( i = 0; i < argc - 1; i++ )
atimes[i] = get_atime(argv[i+1]);
/* Pronto per ricevere il signal. */
signal( SIGUSR1, restat );
while(1) pause();
}
time_t get_atime(char * file)
{
/* Return -1 se stat non e' possibile, st_atime altrimenti */
struct stat buffer;
return ( stat(file, &buffer) < 0) ? (time_t) -1 : buffer.st_atime;
}
void restat()
{
int i;
time_t newtime;
/* Se c'e' stata una modifica in un file lo segnala e
cambia la entry relativa in atime */
for (i = 0; i < nargs - 1; i++ )
if( (newtime = get_atime(argvcopy[i+1])) != atimes[i] )
{
if( newtime == -1 )
printf("stat error per %s\n", argvcopy[i+1]);
else
printf("Nuovi valori per %s\n", argvcopy[i+1]);
atimes[i] = newtime;
}
/* In-class exercise: what's wrong with this? */
}
/*
Prova di esecuzione:
krypton > Esignal Esignal.c &
[1] 18882
krypton > kill -USR1 18882
krypton > touch Esignal.c
krypton > kill -USR1 18882
Nuovi valori per Esignal.c
krypton >
*/