Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / listati / DirMonitor.c
1 /* global variables for shared memory segment */
2 struct DirProp {
3     int tot_size;    
4     int tot_files;   
5     int tot_regular; 
6     int tot_fifo;    
7     int tot_link;    
8     int tot_dir;     
9     int tot_block;   
10     int tot_char;    
11     int tot_sock;
12 } *shmptr;
13 key_t key;
14 int mutex;
15 /* main body */
16 int main(int argc, char *argv[]) 
17 {
18     int i, pause = 10;
19     ...
20     if ((argc - optind) != 1) {   /* There must be remaing parameters */
21         printf("Wrong number of arguments %d\n", argc - optind);
22         usage();
23     }
24     if (chdir(argv[1])) {         /* chdir to be sure dir exist */
25         perror("Cannot find directory to monitor");
26     }
27     Signal(SIGTERM, HandSIGTERM); /* set handlers for termination */
28     Signal(SIGINT, HandSIGTERM);
29     Signal(SIGQUIT, HandSIGTERM);
30     key = ftok("~/gapil/sources/DirMonitor.c", 1); /* define a key */
31     shmptr = ShmCreate(key, 4096, 0666, 0); /* get a shared memory segment */
32     if (!shmptr) {
33         perror("Cannot create shared memory");
34         exit(1);
35     }
36     if ((mutex = MutexCreate(key)) == -1) {        /* get a Mutex */
37         perror("Cannot create mutex");
38         exit(1);
39     }
40     /* main loop, monitor directory properties each 10 sec */
41     daemon(1, 0);           /* demonize process, staying in monitored dir */
42     while (1) {
43         MutexLock(mutex);   /* lock shared memory */
44         memset(shmptr, 0, sizeof(struct DirProp)); /* erase previous data */
45         dir_scan(argv[1], ComputeValues);          /* execute scan */
46         MutexUnlock(mutex); /* unlock shared memory */
47         sleep(pause);       /* sleep until next watch */
48     }
49 }