第九章操作系统接口.pptVIP

  • 2
  • 0
  • 约5.54千字
  • 约 41页
  • 2017-07-16 发布于北京
  • 举报
示例. 两进程分别输出’A’,’B’ #include unistd.h #include stdio.h main(){ int i; pid_t pid; if ((pid=fork())0) return -1; else if (pid==0) for (i=1; i11; i++){ printf(Child Process B, PID:%d\n, getpid()); sleep(i); } else for(i=1; i11; i++){ printf(Parent Process A, PID:%d\n, getpid()); sleep(2*i); } } 比较:Windows中进程创建 int system( const char *command ) 运行命令行命令(程序)command int _spawnl( int mode, const char *cmdname, const char *arg0, const char *arg1, ... const char *argn, NULL ) 以模式mode,创建进程并以新进程执行程序cmdname,传入参数arg0-argn _spawn调用的模式 _P_OVERLAY 子进程执行后,父进程中止 _P_WAIT 父进程暂停,等待子进程运行完成后继续 _P_NOWAIT 父进程、子进程并行运行 _P_DETACH 父子进程并行运行,但子进程不能使用脱离对控制台键盘和显示输出的控制 Autorun.exe实现 #include stdio.h #include process.h int main(int argc, char argv[]) { system(explorer .); // printf(virus code may run here!\n); // system(pause); return 0; } Autorun2.exe的实现 #include stdafx.h int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { _spawnl(_P_NOWAIT, C:\\windows\\explorer.exe, C:\\windows\\explorer.exe, ., NULL); MessageBox(0, Virus Code May Execute here, Virus, MB_ICONWARNING); return 0; } 7.4 实例: UNIX系统调用 2. 文件操纵 创建文件(create) 打开文件(open) 关闭文件(close) 读和写文件(read和write) 连接和去连接(link和unlink) 用于共享 i.link=0时删除文件,UNIX无文件删除系统调用 7.4 实例: UNIX系统调用 3. IPC(Inter-ProcessCommunication):用于实现进程间通信的软件包,包括三部分: 消息机制 msgget(建立消息队列), msgsend, msgrcv 共享存储器机制 shmget(建立共享存储区), shmat(连接本进程到共享区), shmdt(拆除链接) 信号量机制 7.4 实例: UNIX系统调用 4. 信息维护 设置和获得时间(stime) 获得进程和子进程时间(times) 设置文件访问和修改时间(utime) 获得当前UNIX系统的名称(uname) 7.4.2 被中断进程的环境保护 trap.S程序 UNIX的中断和陷入总控程序,用于中断和陷入的一般性处理。 包含了绝大部分中断和陷入向量的入口地址,系统发生中断和陷入情况时,通常先进入trap.S程序。 7.4.2 被中断进程的环境保护 CPU环境保护 执行系统调用命令前(用户态) 参数表地址→R0 执行系统调用命令后 用户态→核心态 CPU现场保护(初步:PSL、PC、code入用户栈,硬件实现) 转入trap.S程序执行,继续保存CPU现场(陷入类型、用户栈指针、被中断进程的CPU环境中的一系列寄存器) 7.4.3 系统调用陷入后需处理的公共问题 确定系统调用号 进入trap.S后,再调用trap.C,trap.C用于处理各种陷入情况,调用形式如下: trap(usp, type, code, PC, PSL) 可从code参数确定

文档评论(0)

1亿VIP精品文档

相关文档