*为什么上传的图片是这个造型,有谁造怎么修改么?
APUE:10.2关于SIGHUP发送的三种情况
1.终端接口检测到一个连接断开,则将此信号送给与该终端相关的控制进程(会话首进程)。
2.会话首进程终止,此信号送给前台进程组中的每个进程。
3.父进程终止后,进程组成孤儿进程组,则向孤儿进程组发送SIGHUP信号,然后发送SIGCONT信号。
-
void daemonize(){
-
/*进程1 第一个fork 进程,进程2第二个fork 进程,进程3 守护进程。*/
-
int fd = 0 ;
-
int i = 0 ;
-
size_t len = 0;
-
pid_t pid;
-
struct sigaction sa;
-
struct rlimit r;
-
char buff[] = "this is daemon test program\n";
-
-
signal(SIGHUP, sighup);
-
umask(0);
-
-
if ( 0 > (pid = fork())) {
-
printf("fork error\n");
-
exit(0);
-
}else if (0 != pid) {
-
sleep(100);
-
exit(0);
-
}
-
//这里可能会由于进程1的会话首进程退出接受到SIGHUP信号
-
setsid();
-
//这里可能会由于进程2的会话首进程(即进程2,因为刚刚setsid过)退出接受到SIGHUP信号
-
//ignore SIGHUP signal.
-
sa.sa_flags = 0;
-
sigemptyset(&sa.sa_mask);
-
sa.__sigaction_u.__sa_handler = SIG_IGN;
-
sigaction(SIGHUP, &sa, NULL);//这里其实不设置igrone进程3(即守护进程)也不收到SIGHUP信号,因为进程3是后台进程,没有连接控制终端
-
-
if ( 0 > (pid = fork())) {
-
printf("fork error\n");
-
exit(0);
-
}else if (0 != pid) {
-
exit(0);
-
}
-
getrlimit(RLIMIT_NOFILE, &r);
-
-
//close standard input/output(close controlling terminal) and other files.
-
for (i = 0 ; i < r.rlim_cur; ++i) {
-
close(i);
-
}
-
i = 0;
-
//assign input/output/errout to /dev/null
-
fd = open("/dev/null", O_RDWR);
-
fd = dup(0);
-
fd = dup(0);
-
-
-
len = strlen(buff);
-
while (i++ < 30) {
-
size_t t = 0;
-
fd = open("/Users/panghongjiang/test.txt", O_WRONLY);
-
t = write(fd, buff, len);
-
close(fd);
-
if (t < len) {
-
exit(0);
-
}
-
-
sleep(1);
-
}
-
}
SIGHUP信号测试:
-
void sighup(int signo) {
-
printf("pid=[%d]\n",getpid());
-
exit(0);
-
}
-
int main(){
-
signal(SIGHUP, sighup);
-
pause();
-
return 0;
-
}
命令:
shell> unix_test > test1.txt &(后台进程)
shell> unix_test > test2.txt &
(后台进程)
shell> unix_test > test3.txt &
(后台进程)
shell> unix_test > test4.txt &
(后台进程)
shell> unix_test > test5.txt (前台进程)
kill -9 【shell的pid】则test5.txt中写入
pid=xxx,表示最后一个unix_test进程获取了SIGHUP信号,而其他unix_test用ps - axj命令PPID已经变成1,过继给了init或launch进程。
阅读(6217) | 评论(0) | 转发(0) |