读书笔记之linux/unix系统编程手册(25)

进程的终止
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
文件输出默认是全缓冲,终端是行缓冲

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值