异常控制流

异常控制流(ECF)
1,这里异常指允许程序进行非本地跳转(违反通常的调用/返回栈规则的跳转)。
2,异常处理完成后,发生三种情况:
(1)执行当前指令Icurr
(2)执行下一条指令Inext
(3)处理程序终止被中断的程序。
3,每种异常=》惟一的非服整数的异常号。
系统启动时,操作系统分配和初始化一张称为异常表的跳转表,使得表目k包含异常k的处理程序的地址。
异常表的起始地址放在叫做异常表基址寄存器的特殊CPU寄存器里。

[img]http://dl.iteye.com/upload/attachment/251045/ca9aa66a-a1de-3479-80dc-899417e86296.png[/img]


4,异常的类别:
(1)中断(interrupt):来自处理器外部的I/O设备的信号的结果。

[img]http://dl.iteye.com/upload/attachment/251047/6f071df2-2297-3dd8-b317-d2556c3e2ca1.png[/img]

(2)陷阱(trap):有意的异常。最重要的用途:提供用户程序和内核的接口:系统调用。

[img]http://dl.iteye.com/upload/attachment/251049/d2672428-ceb1-3217-898a-26feafe2c174.png[/img]

(3)故障(fault):由错误情况引起,可能被故障处理程序修正。经典示例:缺页异常。

[img]http://dl.iteye.com/upload/attachment/251051/3c5047bd-625e-31d4-b34d-80fe4e106b20.png[/img]

(4)终止(abort):不可恢复的致命的错误。

[img]http://dl.iteye.com/upload/attachment/251053/dee83c6b-4d24-3da5-8b43-11b9a10cd446.png[/img]

5,进程的经典定义:一个执行的程序的实例。每个程序都运行在某个进程的上下文。
上下文是由程序正确运行所需的状态组成的。
这个状态包括:存放在存储器中的代码和数据,栈,通用目的寄存器的内容,程序计数器,环境变量,打开文件描述符的集合。
进程提供给应用程序的关键抽象:
一个独立的逻辑控制流。
一个私有的地址空间。

6,寄存器的一个模式位:
用户模式:限制一个应用可以执行的指令和可以访问的地址空间范围。
内核模式:可以执行指令集合中的任何指令,访问系统中任何存储器位置。

7,中断也可能引发上下文切换:如周期性定时器中断的机制。(1到10毫秒)
每次定时器中断,内核就判定当前进程已经运行了足够长的时间了,并切换到一个新的进程。

8,子进程得到与父进程用户级虚拟地址空间相同的一份拷贝,包括文本,数据和bss段,堆以及用户栈。
还获得父进程任何打开的文件描述符的拷贝。子进程可以读写父进程打开的任何文件。
注:父进程和子进程是独立的进程,它们每个都有自己的私有地址空间。
9,关于fork的使用:
程序1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <sys/wait.h>

void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}

pid_t Fork(void)
{
pid_t pid;

if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}

int main()
{
pid_t pid;
int x = 1;

pid = Fork(); //line:ecf:forkreturn
if (pid == 0)
{ /* Child */
printf("child : x=%d\n", ++x); //line:ecf:childprint
exit(0);
}
/* Parent */
printf("parent: x=%d\n", --x); //line:ecf:parentprint
exit(0);
}

输出结果:
parent: x=0
child : x=2

修改下main:

int main()
{
pid_t pid;
int x = 1;

pid = Fork(); //line:ecf:forkreturn
if (pid == 0)
printf("child : x=%d\n", ++x); //line:ecf:childprint
/* Parent */
printf("parent: x=%d\n", --x); //line:ecf:parentprint
exit(0);
}

输出结果:
child : x=2
parent: x=1
parent: x=0

(2)

int main()
{
Fork();
printf("hello.\n");
exit(0);
}


[img]http://dl.iteye.com/upload/attachment/251061/39249132-503a-393f-ac7b-e7919267c239.png[/img]

输出结果:
hello.
hello

int main()
{
Fork();
Fork();
Fork();
printf("hello.\n");
exit(0);
}


[img]http://dl.iteye.com/upload/attachment/251063/fab52d1f-cd4d-3785-97bb-c51291300cc8.png[/img]

输出结果:
hello.
hello.
hello.
hello.
hello.
hello.
hello.
hello.

10,终止了还未被回收的进程称为僵死进程。
waitpid错误条件:
调用进程没有子进程,返回-1,设置errno为ECHILD
waitpid被一个信号中断,返回-1,设置errno为EINTR

11,使用waitpid函数回首僵死子进程。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/wait.h>
#define N 4
void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}

pid_t Fork(void)
{
pid_t pid;

if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}

int main()
{
int status,i;
pid_t pid;
for (i = 0; i < N; i++)
if((pid=Fork()) == 0)
exit(100+i);
//-1,挂起调用进程的执行 ,直到等待集合中的一个子进程终止。
while((pid=waitpid(-1, &status, 0)) > 0)
{
if(WIFEXITED(status))
printf("child %d terminated normally with exit status=%d\n",
pid,WEXITSTATUS(status));
else
printf("child %d teminated abnormally\n", pid);
}
if(errno != ECHILD)
unix_error("waitpid error");
exit(0);
}

按照僵死进程创建的顺序回收。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/wait.h>
#define N 4
void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}

pid_t Fork(void)
{
pid_t pid;

if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}

int main()
{
int status,i;
pid_t pid[N],retpid;
for (i = 0; i < N; i++)
if((pid[i]=Fork()) == 0)
exit(100+i);
//-1,挂起调用进程的执行 ,直到等待集合中的一个子进程终止。
i=0;
while((retpid=waitpid(pid[i++], &status, 0)) > 0)
{
if(WIFEXITED(status))
printf("child %d terminated normally with exit status=%d\n",
retpid,WEXITSTATUS(status));
else
printf("child %d teminated abnormally\n", retpid);
}
if(errno != ECHILD)
unix_error("waitpid error");
exit(0);
}

12,sleep:将一个进程挂起一段时间。
pause:让函数休眠,直到该进程收到一个信号。
execve(char* filename, char* argv[], char* envp[]):在当前进程的上下文中加载并运行一个新进程。

[img]http://dl.iteye.com/upload/attachment/251055/88977ee5-0b8c-36ae-9496-54124bed6dc2.png[/img]

[img]http://dl.iteye.com/upload/attachment/251057/53f6201e-758a-3c2a-b5f0-bf944dee132e.png[/img]

它会覆盖当前进程的地址空间,但并没有创建一个新进程。有相同的PID,并继承了调用execve函数时打开的所有文件描述符。

[img]http://dl.iteye.com/upload/attachment/251059/3b2a0177-66f5-38b0-8c01-e4b9508555e5.png[/img]


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[], char *envp[])
{
int i;
printf("Command line arguments:\n");
for(i=0;argv[i]!=NULL;i++)
printf("argv[%2d]: %s\n", i, argv[i]);
printf("\n");
printf("Environment viriables: \n");
for(i=0;envp[i]!=NULL;i++)
printf("envp[%2d]: %s\n", i, envp[i]);
exit(0);
}

13,发送信号的原因:
(1)内核检测到一个系统事件。
(2)调用kill函数,显式要求内核发送一个信号给目的进程。
14,内核为每个进程在pending位向量维护者待处理信号集合。
而在blocked位向量维护着被阻塞的信号集合。

15,常见信号:
SIGINT:来自键盘的中断(ctrl+c)
SIGTSTP:来自键盘的暂停信号(ctrl+z)
SIGSTOP:不来自终端的暂停信号。
SIGCHLD:一个子进程暂停或终止。
SIGKILL:杀死进程。
SIGALRM:来自alarm函数的定时器信号
实例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/wait.h>

void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}

pid_t Fork(void)
{
pid_t pid;

if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}

/* $begin kill */
void Kill(pid_t pid, int signum)
{
int rc;

if ((rc = kill(pid, signum)) < 0)
unix_error("Kill error");
}
/* $end kill */

void Pause()
{
(void)pause();
return;
}


int main()
{
pid_t pid;
if((pid=Fork()) == 0)
{
Pause();//挂起,直到收到一个信号
printf("Never here.\n");
exit(0);
}
Kill(pid, SIGKILL); //传递信号给子进程。
exit(0);
}

16,unsigned int alarm(unsigned int secs):安排内核在secs秒内发送一个SIGALRM信号给调用进程。
返回:前一次闹钟剩余的秒数,对它的调用将会取消待处理的闹钟。
以前没有设定闹钟,返回0。

实例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}

typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler)
{
struct sigaction action, old_action;

action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* block sigs of type being handled */
action.sa_flags = SA_RESTART; /* restart syscalls if possible */

if (sigaction(signum, &action, &old_action) < 0)
unix_error("Signal error");
return (old_action.sa_handler);
}

unsigned int Alarm(unsigned int seconds)
{
return alarm(seconds);
}

void handler(int sig)
{
static int beeps = 0;
printf("BEEP\n");
if(++beeps < 5)
Alarm(1);
else
{
printf("BOOM!\n");
exit(0);
}
}

int main()
{
Signal(SIGALRM, handler);
Alarm(1);
while(1)
;
exit(0);
}

17,
typedef void handler_t(int);
handler_t *signal(int signum, handler_t *handler)
三种方法处理信号signum:
如果handler是SIG_IGN,那么忽略类型为signum的信号。
如果handler是SIG_DFL,默认行为。
信号处理程序。

18,非本地跳转。
int setjmp(jmp_buf env);//只调用一次,但返回很多次。
一次是第一次调用setjmp,栈的上下文保存在缓冲区env。
longjmp从env缓冲区中恢复栈的内容,从最近初始化env的setjmp调用返回。

实例代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <setjmp.h>

jmp_buf buf;

int error1 = 0;
int error2 = 1;

void bar(void)
{
if(error2)
longjmp(buf, 2);
}

void foo(void)
{
if(error1)
longjmp(buf, 1);
bar();
}


int main()
{
int rc;
rc = setjmp(buf);
if (rc == 0)
foo();
else if (rc == 1)
printf("Detected an error1 condition in foo\n");
else if (rc == 2)
printf("Detected an error2 condition in foo\n");
else
printf("Unknown error condition in foo\n");
exit(0);
}

非本地跳转的另一个重要应用:使一个信号处理程序转移到一个特殊的代码位置。
而不是返回到被信号到达中断了的指令的位置。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

sigjmp_buf buf;

void unix_error(char *msg) /* unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}
unsigned int Sleep(unsigned int secs)
{
unsigned int rc;

if ((rc = sleep(secs)) < 0)
unix_error("Sleep error");
return rc;
}

typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler)
{
struct sigaction action, old_action;

action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* block sigs of type being handled */
action.sa_flags = SA_RESTART; /* restart syscalls if possible */

if (sigaction(signum, &action, &old_action) < 0)
unix_error("Signal error");
return (old_action.sa_handler);
}

void handler(int sig)
{
//这里的巧妙使用
siglongjmp(buf, 1);
}

int main()
{
Signal(SIGINT, handler);
if(!sigsetjmp(buf, 1))
printf("starting\n");
else
printf("restarting\n");
while(1)
{
Sleep(1);
printf("processing...\n");
}
exit(0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值