中断、定时和系统调用

上传人:ldj****22 文档编号:51574603 上传时间:2018-08-15 格式:PPT 页数:55 大小:1,021.50KB
返回 下载 相关 举报
中断、定时和系统调用_第1页
第1页 / 共55页
中断、定时和系统调用_第2页
第2页 / 共55页
中断、定时和系统调用_第3页
第3页 / 共55页
中断、定时和系统调用_第4页
第4页 / 共55页
中断、定时和系统调用_第5页
第5页 / 共55页
点击查看更多>>
资源描述

《中断、定时和系统调用》由会员分享,可在线阅读,更多相关《中断、定时和系统调用(55页珍藏版)》请在金锄头文库上搜索。

1、中断、定时和系统调用中断和异常中断是一种触发信号,一个CPU接收到这样一种信号后会改变它 执行代码的流程,一般是从一个固定的地址执行一段预先设定好 的程序。 中断的种类 同步中断:异常,软中断 程序运行过程中产生 页错误,被0除,中断指令(int),. 异步中断:中断,硬中断 由设备产生 键盘中断,时钟中断,. 中断信号常被用来泛指上述各种中断。中断信号是系统的重要和 稀缺的资源。 每个中断和异常都有一个编号,0-255,称为中断向量。中断信号处理 中断信号可以由进程或外部事件产生 中断信号的处理只能由内核完成 中断当前的程序,切换到中断处理代码 中断处理代码不属于进程 同步中断与当前进程相关

2、 异步中断与当前进程无关 中断的结果与进程相关 中断处理条件: 中断响应时间越快越好 中断处理分成top half和bottom half。 中断嵌套 中断处理过程中对临界区的处理中断 中断 可屏蔽中断 不可屏蔽中断 中断控制器 可编程中断控制器:PIC 单处理器环境,8259A 先进可编程中断控制器:APIC 多处理器环境中断请求和中断控制器响应 中断请求(IRQ) 对中断请求的响应 中断控制器 产生中断的设备8259A8259A80x86015异常 80x86处理器约有20个异常 常见异常 Divide error:被0除异常 Invalid opcode:无效操作码异常 Page fau

3、lt:页失效异常 每个异常都有相应的异常处理函数(handler) divide_error( ), invalid_op( ), page_fault( ), .中断门描述符 内核管理中断使用的数据结构 一个中断门描述符由8个字节组成 描述符中保存有关中断的各种信息 中断向量 中断处理函数地址 各种权限参数 i386共有3种类型的中断门描述符 Task Gate Descriptor:任务门描述符 Interrupt Gate Descriptor:中断门描述符 Trap Gate Descriptor:自陷门描述符不同类型的中断门描述符中断门描述符表 内核维护的一个由256个表项的表,每个

4、 表项为一个中断门描述符。 一个中断门描述符由8个字节组成,整个 表共用内存空间2048字节。 中断门描述符表保存在内存中,其起始 地址保存在一个专用的寄存器中。 操作系统在使能中断之前要初始化中断 门描述附表。中断门描述符表结构0132331282550号异常:Divide error,被0除1号异常:Debug,调试0号中断:IRQ01号中断:IRQ1中断指令:int 80Linux对中断的使用 中断指令int int指令可以产生任意向量的中断 Linux对中断的划分 中断门:interrupt gate,用于硬件中断信号产生的中 断,只能在内核模式下使用。 系统门:system gate

5、,用于实现系统调用,可由用 户模式下的代码产生。 int3, into, bound, int 80. 自陷门:trap gate,用于其他异常的中断,只能在 内核模式下使用。初始化中断门描述符表 初级初始化:setup_idt() 缺省的中断处理函数:ignore_int() 打印“Unknown interrupt“ 用ignore_int()填充中断门描述符表中所有表 项的中断处理函数 ignore_int函数应该永远不会被调用,除非系统硬 件或内核出现问题。/* This is the default interrupt “handler“ :-) */ int_msg: .asciz

6、 “Unknown interruptn“ ALIGN ignore_int: cld pushl%eax pushl %ecx pushl %edx pushl %es pushl %ds movl $(_KERNEL_DS),%eax movl %eax,%ds movl %eax,%es pushl $int_msg call SYMBOL_NAME(printk) popl %eax popl %ds popl %es popl %edx popl %ecx popl %eax iretarch/i386/kernel/head.S: ignore_int/* setup_idt* s

7、ets up a idt with 256 entries pointing to* ignore_int, interrupt gates. It doesnt actually load* idt - that can be done only after paging has been enabled* and the kernel moved to PAGE_OFFSET. Interrupts* are enabled elsewhere, when we can be relatively* sure everything is ok.*/ setup_idt: lea ignor

8、e_int,%edx movl $(_KERNEL_CS int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);$ man setitimerwhich ITIMER_REAL:以系统实时钟为计时标准,时钟到时后发出SIGALRM信号 ITIMER_VIRTUAL:以进程运行时间为计时标准,时钟到时后发出SIGVTALRM信号 ITIMER_PROF:以进程运行时间和内核以进程身份运行时间之和为计时标准,到时后发出SIGPROF信号struct itimerval struct tim

9、eval it_interval;/* next value */ struct timeval it_value;/* current value */ ;struct timeval long tv_sec;/* seconds */ long tv_usec;/* microseconds */ ;#include #include #include /* A global variable used to control which string is printed */ int flag = 0;/* The signal handler which runs when the s

10、ignal is received */ void sighand(int signum) if(flag) flag = 0; else flag = 1; return; int main(void) struct itimerval itimer;/* Assume the itimer expires every 2.7 sec */ itimer.it_interval.tv_usec = 700000;/* 0.7 sec */ itimer.it_interval.tv_sec = 2;/* 2 sec */mytimer.c/* The itimer starts after

11、2 sec */ itimer.it_value.tv_usec = 0; itimer.it_value.tv_sec = 2;/* Register the signal handler */ signal(SIGALRM, /* Start the itimer */ setitimer(ITIMER_REAL, /* This is the main routine */ while(1) if(flag) printf(“AAAAn“); else printf(“BBBBn“); int i; for(i=100000000; i0; i-) ;/* A busy loop for

12、 delay */ return 0; mytimer.c (续)$ gcc -o mytimer mytimer.c $ ./mytimer BBBB BBBB AAAA AAAA AAAA AAAA BBBB BBBB BBBB AAAA AAAA C编译和运行注意:当setitimer使用ITIMER_REAL模式调用时,它与系统调用alarm()和库 函数sleep()可能使用相同的动态定时器实现机制,并且都会发出SIGALRM信 号。因此不要混合使用上述几种定时函数。系统调用 系统调用是进程向内核请求资源的唯一手段 系统调用的各种过程是内核的一部分 应用程序接口(API)与系统调用

13、系统调用是系统以进程身份执行的内核过程 系统调用的实现基于特定的中断指令。中断指 令将进程从用户模式切换到内核模式。 i386:int 0x80系统调用的调用过程系统调用的实现 sys_call_table 保存系统调用入口地址的数组,共有NR_syscalls(通 常为256)个数组元素。 第n个数组元素对应调用号为n的系统调用。 初始化中断门描述符表,使用0x80号中断 set_system_gate(0x80, 每个系统调用都有2个部分 派遣函数或系统调用句柄 系统调用服务程序系统调用中的参数传递 system_call()是所有系统调用的入口函数,它 根据调用号从sys_call_ta

14、ble中调用相应的系统 调用服务程序。 系统调用使用寄存器进行参数传递 一般C函数使用堆栈进行参数传递 因为寄存器的大小和数目都是有限的,因此系统调 用的参数不能超过寄存器大小并且数目不能超过6 个。但可以使用其它技术传递更多参数。 最终的系统调用服务程序由C函数完成,因此 调用参数仍然会被放到堆栈中。系统调用相关源代码析读.data ENTRY(sys_call_table).long SYMBOL_NAME(sys_ni_syscall) /* 0, old “setup()“ system call*/.long SYMBOL_NAME(sys_exit).long SYMBOL_NAM

15、E(sys_fork).long SYMBOL_NAME(sys_read).long SYMBOL_NAME(sys_write).long SYMBOL_NAME(sys_open)/* 5 */.long SYMBOL_NAME(sys_ni_syscall) /* 255 sys_epoll_ctl */.long SYMBOL_NAME(sys_ni_syscall) /* sys_epoll_wait */.long SYMBOL_NAME(sys_ni_syscall) /* sys_remap_file_pages */.long SYMBOL_NAME(sys_set_tid

16、_address)arch/i386/kernel/entry.S: sys_call_table/* Return to user mode is not as complex as all this looks,* but we want the default path for a system call return to* go as quickly as possible which is why some of this is* less clear than it otherwise should be.*/ENTRY(system_call)pushl %eax# save orig_

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号