#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
FILE *fp;
unsigned seq;
void sigalrm_fn(int sig)
{
time_t tnow;
char fname[256];
tnow = time(NULL);
fclose(fp);
snprintf(fname, sizeof(fname), "%u_%ld.xml", seq++, tnow);
fp = fopen(fname, "w");
fprintf("I am the signal: %dn", sig);
sleep(3);
alarm(1);
}
int init()
{
time_t tnow;
char fname[256];
tnow = time(NULL);
seq = 1;
snprintf(fname, sizeof(fname), "%u_%ld.xml", seq++, tnow);
fp = fopen(fname, "w");
signal(SIGALRM, sigalrm_fn);
alarm(1);
return 0;
}
int main()
{
init();
// getchar();
pause();
while(1)
{
fprintf(fp, "I am the main: %un", seq);
}
return 0;
}
|