进程的终止
1.无论进程是否正常终止,都会发生如下动作:
(1)关闭所有打开的文件描述符、目录流、信息目录描述符
(2)作为文件描述符关闭的后果之一,将释放该进程所持有的任何文件锁
(3)分离任何已连接的systemV共享内存段,且对应于各段的计数器值将减一
(4)进程为每个systemV信号量所设置的值会被加到信号量值中
(5)将关闭进程打开的任何posix有名信号量
(6)将关闭该进程打开的任何posix消息队列
(7)作为进程退出的后果之一,如果某进程组成为孤儿,且该组中存在任何已停止的进程
(8)取消该进程所创建的任何内存映射
(9)移除任何内存锁
2.退出处理程序可与生命周期的任意时点注册,并在该进程调用exit()正常终止时自动执行,如果程序直接调用_exit()或因信号而异常终止,则不会调用退出处理程序,应避免在退出处理程序内部调用exit()
test@ubuntu:~/share/xinhaoliang$ cat a
Ciao
hello world
文件输出默认是全缓冲,终端是行缓冲
1.无论进程是否正常终止,都会发生如下动作:
(1)关闭所有打开的文件描述符、目录流、信息目录描述符
(2)作为文件描述符关闭的后果之一,将释放该进程所持有的任何文件锁
(3)分离任何已连接的systemV共享内存段,且对应于各段的计数器值将减一
(4)进程为每个systemV信号量所设置的值会被加到信号量值中
(5)将关闭进程打开的任何posix有名信号量
(6)将关闭该进程打开的任何posix消息队列
(7)作为进程退出的后果之一,如果某进程组成为孤儿,且该组中存在任何已停止的进程
(8)取消该进程所创建的任何内存映射
(9)移除任何内存锁
2.退出处理程序可与生命周期的任意时点注册,并在该进程调用exit()正常终止时自动执行,如果程序直接调用_exit()或因信号而异常终止,则不会调用退出处理程序,应避免在退出处理程序内部调用exit()
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("hello world\n");
write(STDOUT_FILENO, "Ciao\n", 5);
if(fork() == -1)
{
exit(0);
}
//both parent and children do below
return 0;
}
输出结果:
test@ubuntu:~/share/xinhaoliang$ ./a.out > a
<pre name="code" class="cpp">test@ubuntu:~/share/xinhaoliang$ cat a
Ciao
hello world
hello world
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("hello world\n");
write(STDOUT_FILENO, "Ciao\n", 5);
int pid = fork();
if(pid == -1)
{
exit(0);
}else if(pid > 0)
{
_exit(0);
}
//both parent and children do below
return 0;
}
test@ubuntu:~/share/xinhaoliang$ ./a.out > a
test@ubuntu:~/share/xinhaoliang$ cat a
Ciao
hello world
文件输出默认是全缓冲,终端是行缓冲