#include #include #include #include #include #include #include #include #include "common.h" static int shm_id = 0; void free_shmem( int i ) { shmctl( shm_id, IPC_RMID, NULL); _exit(1); } int main( int argc, char* argv[] ) { struct inf_t *data; time_t start; key_t key; int shmflg; size_t size = sizeof(struct inf_t); start = time(NULL); key = getuid() + 6661; shmflg = 0600 | IPC_CREAT; massert( (shm_id = shmget(key, size, shmflg)) < 0, "shmget" ); signal( SIGINT, free_shmem ); data = (struct inf_t*)shmat( shm_id, NULL, 0 ); if( data == (struct inf_t*)(-1)) { perror("shmat"); shmctl( shm_id, IPC_RMID, NULL); exit(-1); } memset(data, 0, size); set_id(data); while( 1 ) { if( update_inf( data, start ) < 0 ) perror("getloadavg"); sleep(1); } return 0; }