prepared by Meixian Li for lecture presented by Paul Eggert on October 22th, 2014
$ mv log oldlog
$ > log
Problem:
//piece of code for apache write to log
close(fd);
fd = open("log", O_WRONLY);
checklog();
write(fd, "...", n);
checklog();
write(fd, "...", m);
//checklog
void checklog(){
if(stat("log", &st) < 0 ||st.st_sid ==0){
close(fe);
fd=open("log",...);}
}
Evaluation of solution: using polling method, make it inefficient, and the solution is slow.
pid_t p = fork();
if(p>0){
sleep(30);
kill(p, SIGINT);
}
waitpid(p, NULL, 0);
typedef void(*sighandler_t) (int);//take sig return void
sighandler signal(int, sighandler_t);//(sig_name, old_handler) -> new_handler
*Special value for sighandler_t:
SIG_DFL(Default), SIG_IGN(ignore, discard signals).
int
main(void){
signal(SIGALRM, bing);
alarm (30);
//signal(SIGALRM, bing);
//shouldn't call here, it's possible for race condition,
//that signal never gets called after 30s.
//complicated code...
return;
}
void bing(int sig){
//printf("bing\n"); during the complicated code:
//main->printf->delicate->bing->printf
//exit(27); go and flush every thing, safe things only.
write(1,"Bing\n", 6);
_exit(27);
}
Notes:
//gzip foo -> foo.gz during which type control+C
int
main(void){
//one way: signal(SIGINT, SIG_IGN);...lost benefit of it
//theother way: signal(SIGINT, cleanup);-> lost all the data.
int in=open("foo", O_RONLY);
int out = open("foo.gz", O_WRONLY);
magic_gzip(in, out); //-> control+C
close("in");
close("out");
pthread_sigmask(SIG_BLOCK, ...);
unlink("foo");
}
void
cleanup(int sig){
unlink("foo.gz");
_exit(97)
}
thread | process |
pthread_create | fork |
pthread_join | waitpid |
pthread_exit | _exit |