操作系统实验PPT课件

上传人:资****亨 文档编号:130546915 上传时间:2020-04-28 格式:PPT 页数:31 大小:86.50KB
返回 下载 相关 举报
操作系统实验PPT课件_第1页
第1页 / 共31页
操作系统实验PPT课件_第2页
第2页 / 共31页
操作系统实验PPT课件_第3页
第3页 / 共31页
操作系统实验PPT课件_第4页
第4页 / 共31页
操作系统实验PPT课件_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《操作系统实验PPT课件》由会员分享,可在线阅读,更多相关《操作系统实验PPT课件(31页珍藏版)》请在金锄头文库上搜索。

1、 操作系统实验 实验一Linux登录与vi编辑器 实验目的 让学生熟悉Linux的环境 掌握Linux系统登录的方法 给学生使用vi创建与编辑文件的机会 vi是UNIX中最常用的编辑器 对vi编辑器的全面而清晰的理解 对于后续内容的学习至关重要 实验内容与要求 首先 登录Linux命令login的使用 其次 使用vimtutor e查看vi编辑器的相关帮助文档 让学生自行阅读使用方法 最后 通过第二步的阅读帮助 完成以下任务 1 创建一个文件 2 保存退出一个文件及不保存退出一个文件 3 在文本中使用不同的键进行光标的移动 4 在一个文件中加入 删除与修改文本 5 设定选项以自定义编辑环境 6

2、 调用命令行编辑功能 实验二Linux常用命令与Linux下C程序设计 实验目的 让学生熟悉Linux常用命令 对Linux命令有一个熟悉的了解 进一步熟悉vi编辑器的使用 掌握在linux下的编译命令gcc及C语言程序的运行 实验内容与要求Linux下常用命令的使用 1 Cp 拷贝文件命令将一个档案拷贝至另一档案 或将数个档案拷贝至另一目录 格式为cp options sourcedest 选项的说明如下 a尽可能将档案状态 权限等资料都照原状予以复制 f若目的地已经有相同档名的档案存在 则在复制前先予以删除再行复制 范例 a 将档案aaa复制 已存在 并命名为bbb cpaaabbb b

3、将所有的C语言程式拷贝至Finished子目录中 cp cFinished Cd 切换目录该命令的作用是切换当前目录至另一目录 格式为cd dirName 下面是一些小技巧 cd 进入用户主目录 cd 返回进入此目录之前所在的目录 cd 返回上级目录 若当前目录为 则执行完后还在 为上级目录的意思 cd 返回上两级目录 Mkdir 创建新文件夹命令所创建的文件夹 目录 不能与其父目录 即父文件夹 中的文件名重名 即同一个目录下不能有同名的 区分大小写 格式为 mkdirDirName实例 在 home cc 下面创建以 itorg 命名的文件夹 格式 mkdir home cc itorg 使

4、用vi编辑器编写一个C语言程序HelloWorld includeintmain void printf Helloworld Linuxprogramming n return0 gcc编译 将程序存为hello c 编译 gcc ohellohello c 实验三Linux进程的异步并发执行 实验目的1 了解进程与程序的区别 加深对进程概念的理解 2 掌握进程并发执行的原理 理解进程并发执行的特点 区分进程并发执行与串行执行 3 了解fork 系统调用的返回值 掌握用fork 创建进程的方法 4 熟悉wait exit等系统调用 实验内容与要求1 编写一C语言程序 实现在程序运行时通过系统

5、调用fork 创建两个子进程 使父 子三进程并发执行 父亲进程执行时屏幕显示 Iamfather 儿子进程执行时屏幕显示 Iamson 女儿进程执行时屏幕显示 Iamdaughter 2 多次连续反复运行这个程序 观察屏幕显示结果的顺序 直至出现不一样的情况为止 记下这种情况 试简单分析其原因 3 修改程序 在父 子进程中分别使用wait exit等系统调用 实现 其同步推进 多次反复运行改进后的程序 观察并记录运行结果 程序参考 仅供参考 include includevoidmain inti pid tpid fork if pid 0 子进程程序 for i 1 i 1000 i pr

6、intf Iamson n else 父进程程序 for i 1 i 1000 i printf Iamfather n fork 当程序执行到下面的语句 pid fork OS创建子进程 子进程和父进程的可执行程序是同一个程序 上下文和数据 绝大部分就父进程的拷贝 但它们是两个相互独立的进程 当前是父进程执行到fork调用即将返回 此时子进程不占有CPU 父进程继续执行 OS对fork的实现 使这个系统调用在父进程中返回刚刚创建的子进程的pid 一个正整数 所以下面的if语句pid 0的分支不会执行 所以输出iamfather 子进程在之后的某个时候得到调度 它的PCB的内容被换入 占用CP

7、U OS对fork的实现 使子进程中fork返回0 所以在这个子进程中pid 0 这个进程继续执行的过程中 if语句中pid 0是true 所以输出iamson 获得进程ID的函数getpid include includemain pid tpid fork if pid 0 printf iamson pid d n getpid elseprintf iamfather pid d n getpid wait和exit函数wait 等待子进程中断或结束 定义函数pid twait int status 返回值如果执行成功则返回子进程的ID 如果有错误发生则返回 1 进程一旦调用wait

8、就立即阻塞自己 直到有信号来到或子进程结束 如果有子进程退出 wait就会收集这个子进程的信息 并把它彻底销毁后返回 如果没有找到这样一个子进程 wait就会一直阻塞在这里 直到有一个出现为止 注意 如果在调用wait时子进程已经结束 则wait会立即返回子进程结束状态值 子进程的结束状态值会由参数status返回 而子进程的ID也会一快返回 如果不在意结束状态值 则参数status可以设成NULL include include include includeintmain intstatus i pid tpid fork if pid 0 printf Iamson pid d n ge

9、tpid exit 5 else sleep 1 printf Iamfather waitforson n pid wait 执行示例 Iamson pid 1501Iamfather waitforson son spid 1501 exitstatus 5 实验三遇到的问题 问题一pid tpid1 fork pid tpid2 fork if pid1 0 printf iamson else if pid2 0 printf iamdaught elseprintf iamfather pid tpid1 fork pid tpid2 fork pid1 儿子idPid2 女儿id

10、pid tpid1 fork pid tpid2 fork pid1 0pid2 孙子id 父进程 儿子进程 pid tpid1 fork pid tpid2 fork pid1 儿子进程pid2 0 女儿进程 pid tpid1 fork pid tpid2 fork pid1 0pid2 0 孙子进程 if pid1 0 printf iamson else if pid2 0 printf iamdaught elseprintf iamfather pid tpid1 pid tpid2 pid1 fork if pid1 0 printf iamson else pid2 fork

11、if pid2 0 printf iamdaught elseprintf iamfather pid tpid1 pid tpid2 inti for i 0 i 2 i pid1 fork if pid1 0 printf iamson else pid2 fork if pid2 0 printf iamdaught elseprintf iamfather 输出 IamdaughtIamfatherIamson pid tpid1 fork pid tpid2 fork 父进程 儿子进程 女儿进程 输出 fatherdaughtson fatherdaughtson fatherdau

12、ghtson fatherdaughtson 第二遍循环 问题三pid tpid fork if pid 0 for i 1 i 1000 i printf Iamson n exit 5 else for i 1 i 1000 i printf Iamfather waitforson n pid wait 输出 IamsonIamfather waitforson son spid 1015son spid 1son spid 1son spid 1son spid 1son spid 1 原因 第一遍循环时子进程已经退出 实验四通道通信 实验目的进一步熟悉linux的编程环境和vi编辑器

13、 熟悉linux环境下C语言程序的编译和运行 进一步掌握父子进程间的通信原理和fork 函数 熟悉并掌握管道通信的原理 以及linux环境下提供的pipe write 及read 函数 能够编写出管道通信的程序 并调试通过 实验内容pipe 函数 创建管道的方法 函数原型为intpipe intfiledes 2 write 函数 读取数据方法 函数原型为 intwrite intpipe fd char buffer size tlen read 函数 写入数据的方法 函数原型为 intread intpipe fd char buffer size tlen pipe 建立管道头文件 in

14、clude定义函数 intpipe intfiledes 2 函数说明 建立管道 并将文件描述符由数组filedes返回 filedes 0 为管道里的读取端 用read从管道中读数据filedes 1 则为管道的写入端 用write向管道中写数据 返回值 若成功则返回0 否则返回 1 include include defineINPUT0 defineOUTPUT1intmain intfiledes 2 定义子进程号 pid tpid charbuf 500 intreturned count 创建无名管道 pipe filedes 创建子进程 if pid fork 1 printf

15、Errorinfork n exit 1 执行子进程 if pid 0 printf inthespawned child process n 子进程向父进程写数据 关闭管道的读端 close filedes INPUT write filedes OUTPUT testdata strlen testdata exit 0 else 执行父进程 printf inthespawning parent process n 父进程从管道读取子进程写的数据 关闭管道的写端 close filedes OUTPUT returned count read filedes INPUT buf sizeof buf printf dbytesofdatareceivedfromspawnedprocess s n returned count buf root localhost gccpipe c opipe o root localhost pipe o显示结果 inthespawning parent process inthespawned child process 9bytesofdatareceivedfromspawnedprocess testdata 感谢亲观看此幻灯片 此课件部分内容来源于网络 如有侵权请及时联系我们删除 谢谢配合

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

当前位置:首页 > 高等教育 > 大学课件

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