已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一 一 实验题目 实验题目 熟悉 Linux shell 命令 熟悉 fork exec 的使用 实验 1 2 当堂完成 报告编写实验 1 2 二 二 实验目的 实验目的 通过对相关 linux GNU c 语法和命令的熟悉 可进一步了解 linux 操作系统的功能 结构 了 解 linux 的进程创建方法 父子进程的关系 进程的回收 进程的不同状态等 三三 实验设备及环境 实验设备及环境 1 硬件设备 PC 机一台 2 软件环境 安装 Linux 操作系统 并安装相关的程序开发环境 如 C C tsh bsh 等编 程语言环境 四四 实验内容及要求 实验内容及要求 1 编写一个 shell 命令提示符 1 包含以下内部命令 i cd change the current default directory to If the argument is not present report the current directory If the directory does not exist an appropriate error should be reported This command should also change the PWD environment variable ii clr clear the screen iii dir list the contents of directory iv environ list all the environment strings v echo display on the display followed by a new line multiple spaces tabs may be reduced to a single space vi help display the user manual using the more filter vii pause pause operation of the shell until Enter is pressed viii quit quit the shell ix The shell environment should contain shell myshell where myshell is the full path for the shell executable not a hardwired path back to your directory but the one from which it was executed 2 在环境变量里添加 parent myshell 3 支持批处理 如 myshell batchfile 4 支持 i o redirection 如 programname arg1 arg2 outputfile 5 支持后台运行 如 dir 3 3 主要模块 实验主要模块 实验 1 2 共用 共用 int main int argc char argv 六 代码六 代码 实验一代码实验一代码 主函数 include include include include include include include myio h ifndef FALSE define FALSE 0 endif ifndef TRUE define TRUE 1 endif extern char environ int main int argc char argv int oldstdout oldstdin oldstdout dup 1 oldstdin dup 0 char input 300 input string char cmdline char cmd 20 char rmpath 100 char execmd 20 char cwdstr 100 paraments valid command char cmdbuf 300 all the command getcwd rmpath 100 strcat rmpath readme printf s n rmpath strcpy cwdstr shell if putenv strdup cwdstr 0 if OK non zero on error printf add of shell environment variable failed n else while 1 if getcwd cwdstr sizeof cwdstr read current working directory printf myshell s cwdstr output as prompt else printf getcwd ERROR n int usexec TRUE cmdbuf 0 0 int i 0 while input i getchar n i input the string input i 0 if input 0 0 continue char p cmdline myio input int j 1 cmd 0 strtok cmdline while cmd j strtok NULL j 上面是得到用户的输入 并分离命令和参数 下面是对命令进行解释 if strcmp cmd 0 quit exit 0 input quit exit the program else if strcmp cmd 0 dir dir if j 1 strcpy cmd 2 cmd 1 cmd 0 ls cmd 1 al usexec TRUE else if strcmp cmd 0 clr clr strcpy cmd 0 clear usexec TRUE else if strcmp cmd 0 help help cmd 0 more cmd 1 readme cmd 2 NULL usexec TRUE else if strcmp cmd 0 cd cd if j 1 strcpy cmd 0 pwd usexec TRUE else if j 2 printf ERROR too many paraments n else if chdir cmd 1 printf change directory failed n else strcpy cwdstr PWD else printf retrieving cwd n usexec FALSE else if strcmp cmd 0 environment environment char env environ while env printf s n env usexec FALSE if cmd 0 0 0 if usexec 1 execute cmd fflush stdout dup2 oldstdout 1 dup2 oldstdin 0 h 文件 char myio char input 实现重定向 char cmdline char middle char pathin char pathout char p int i 0 j 0 while input i 0 if input i j i cmdline strtok input get the cmdline printf cmdline is s n cmdline p strtok NULL if p printf the path is s n myio p get the path while p p pathout p FILE fd if j 1 fd fopen pathout w 输出重定向 if j 2 fd fopen pathout a if j 2 printf redirection erro to much n dup2 fileno fd 1 cmdline strtok cmdline p strtok NULL arrivaltime priority PRIORITYMIN delete job if excess resource or memory request if p printers RTJOB PRINTERMAX p scanners RTJOB SCANNERMAX p modems RTJOB MODEMMAX p cd drives RTJOB CDDRIVEMAX p memoryusage RTJOB MEMORY fprintf stderr One process deleted excess resource memory request n free p p NULL else p mab memAlloc rtmab RTJOB MEMORY realtimeQ enqPcb realtimeQ p else if p priority printers PRINTERS p scanners SCANNERS p modems MODEMS p cd drives CD DRIVES p memoryusage MEMORY RTJOB MEMORY fprintf stderr One process deleted excess resource memory request n free p p NULL else userjobQ enqPcb userjobQ p else fprintf stderr Unknown process priority level must be between 0 and 3 n exit 1 Unload pending process es from userjobQ allocate memory and resources and dispatch to appropriate priorityQ void unload userjobQ void Pcb p Mab m unload if memory and resources are available while userjobQ dequeue process from userjobQ p status READY mark as ready to run allocate memory to process if p memoryusage m size m allocated p mab memAlloc m p memoryusage allocate if just fit else p mab memSplit m p memoryusage split otherwise allocate I O resources to process rsrcAlloc p enqueue on appropriate priority feedback queue switch p priority case 1 priority1Q enqPcb priority1Q p break case 2 priority2Q enqPcb priority2Q p break case 3 priority3Q enqPcb priority3Q p break default fprintf stderr Unknown process priority level must be between 1 and 3 n exit 1 Main program int main int argc char argv FILE dispatch list Pcb p curr throw error if not two arguments supplied if argc 2 fprintf stderr Usage hostd n return 1 open job dispatch list for reading if dispatch list freopen argv 1 r stdin fprintf stderr Dispatch job file s not found n argv 1 return 1 initialise dispatcher queues inputQ NULL realtimeQ NULL userjobQ NULL priority1Q NULL priority2Q NULL priority3Q NULL initialise memory structures if memChk mab MEMORY split into two blocks one for real time process and the other to be shared among user jobs mab memAlloc mab MEMORY mab memSplit mab RTJOB MEMORY rtmab mab mab mab next real time memory and user job memory should not be linked rtmab next NULL mab prev NULL initialise resource structures printers PRINTERS scanners SCANNERS modems MODEMS cd drives CD DRIVES fill inputQ from dispatch list file fill inputQ dispatch list timer 0 start dispatcher timer curr NULL no running process while there is anything in the queues or currently running process while inputQ realtimeQ userjobQ priority1Q priority2Q priority3Q curr unload pending process es from inputQ unload inputQ unload pending process es from userjobQ unload userjobQ if a process is currently running if curr decrement process remaining time curr remainingcputime QUANTUM if time s up if curr remainingcputime priority PRIORITYMIN curr mab allocated 0 else memFree curr mab user process rsrcFree curr free curr free up process structure memory curr NULL now there is no running process if curr is user process and other processes are waiting in any of the queues else if curr priority PRIORITYMIN reduce priority and enqueue to appropriate priorityQ if curr priority priority 1 switch curr priority case 2 priority2Q enqPcb priority2Q curr break case 3 priority3Q enqPcb priority3Q curr break default break curr NULL now there is no running process if no process currently running and real time queue and feedback queues are not all empty if curr else if priority1Q p deqPcb else if priority2Q p deqPcb else p deqPcb restart process if suspended else start it if p status SUSPENDED p restartPcb p else p startPcb p curr p set as currently running process sleep QUANTUM sleep for QUANTUM second timer QUANTUM increment dispatcher timer free memory at end memFree mab memFree rtmab mab NULL rtmab NULL return 0 2 mab c2 mab c 代码代码 mab c Contiguous memory blocks include mab h Function declarations Allocate memory block Mab memAlloc Mab m int size if m if m malloc sizeof Mab m offset 0 m size size m allocated size m next NULL m prev NULL else fprintf stderr Cannot allocate memory n else m allocated size return m return allocated block Check if memory is available Mab memChk Mab m int size search each partition while m get partition that can fit size if size size m allocated return m else m m next return NULL no partition is available Free memory block Mab memFree Mab m if already freed if m return NULL if either side of m initialised else if m prev m next m allocated 0 m memMerge m merge with adjacent free block s if both sides uninitialised free as normal else free m m NULL return m Merge adjacent free memory blocks Mab memMerge Mab m Mab ml mr temp ml m prev mr m next merge left if ml m ml m size temp size m next temp next if m next m next prev m free temp temp NULL merge right if mr m size temp size m next temp next if m next m next prev m free temp temp NULL return m Split a memory block into two adjacent partitions The first partition is allocated with size and the second left unallocated Mab memSplit Mab m int size Mab mr second partition is the remaining free space if mr malloc sizeof Mab mr offset m offset size mr size m size size mr allocated 0 set new partition to inbetween first partition and next block mr next m next if mr next mr next prev mr mr prev m m next mr else fprintf stderr Error while splitting memory n first partition will be fully allocated and point to second partition m size size m allocated size return m 3 pcb c3 pcb c 代码代码 pcb c Process control block include pcb h Function declarations Pcb createnullPcb void Pcb p if p malloc sizeof Pcb p status UNINITIALISED p arrivaltime 0 p priority 0 p remainingcputime 0 p memoryusage 0 p printers 0 p scanners 0 p modems 0 p cd drives 0 p args 0 process p args 1 NULL p mab NULL p next NULL return p else fprintf stderr Cannot create PCB n return NULL PcbPtr deqPcb PcbPtr headofQ dequeue process at front of queue returns pointer of process Pcb deqPcb Pcb head Pcb p if head p head head head next p next NULL return p else fprintf stderr Nothing to dequeue n return NULL PcbPtr enqPcb PcbPtr headofQ PcbPtr process queue process or join queues at end of queue returns head of queue Pcb enqPcb Pcb head Pcb p Pcb curr first job goes to head if head head p the rest goes to tail else curr head while curr next curr curr next curr next p return head Initialise process from a string of parameters return new process Pcb initialisePcb Pcb p char buff p status INITIALISED p arrivaltime atoi strtok buff DELIMITERS p priority atoi strtok NULL DELIMITERS p remainingcputime atoi strtok NULL DELIMITERS p memoryusage atoi strtok NULL DELIMITERS p printers atoi strtok NULL DELIMITERS p scanners atoi strtok NULL DELIMITERS p modems atoi strtok NULL DELIMITERS p cd drives atoi strtok NULL DELIMITERS return p print process attributes on iostream returns PcbPtr of process void printPcbInfo Pcb p fprintf stdout pid d memoffset d priority d time d memory d printers d scanners d modems d drives d n int p pid p mab offset p priority p remainingcputime p memoryusage p printers p scanners p modems p cd drives PcbPtr terminatePcb PcbPtr process restart a process returns PcbPtr of process NULL if terminate failed Pcb restartPcb Pcb p if kill p pid SIGCONT fprintf stderr Restart of d failed int p pid return NULL p status RUNNING return p PcbPtr startPcb PcbPtr process start a process returns PcbPtr of process NULL if start restart failed Pcb startPcb Pcb p if new process if p pid fork 0 p pid getpid p status RUNNING printPcbInfo p print info execvp p args 0 p args if parent process else kill p pid SIGCONT p status RUNNING return p PcbPtr suspendPcb PcbPtr process suspend a process returns PcbPtr of process NULL if suspend failed Pcb suspendPcb Pcb p int status if kill p pid SIGTSTP fprintf stderr Suspend of d failed int p pid waitpid p pid p status SUSPENDED return p PcbPtr terminatePcb PcbPtr process terminate a process returns PcbPtr of process NULL if terminate failed Pcb terminatePcb Pcb p int status if kill p pid SIGINT fprintf stderr Terminate of d failed int p pid waitpid p pid p status TERMINATED return p 4 rsrc c 代码代码 rsrc c Resource allocation include rsrc h Allocate resource to process void rsrcAlloc Pcb p printers p printers scanners p scanners modems p modems cd drives p cd drives Check if resource available for process int rsrcChk Pcb p if printers p printers else return 1 Free resource from process void rsrcFree Pcb p printers p printers scanners p scanners modems p modems cd drives p cd drives 5 sigtrap c OS Exercises project 2 sigtrap report system signals applied to process usage sigtrap n n is time for process to exist default 20 seconds program ticks away reporting process id and tick count every second the program traps and reports the following signals SIGINT SIGQUIT SIGHUP SIGTERM SIGABRT SIGCONT SIGTSTP program can not trap SIGSTOP or SIGKILL to help identify specific processes the program uses the process id to select one of 32 colour combinations for the display to an ASCC terminal output is to stdout set in define reset to BLACK and NORMAL and flushed after every printf include include include include include include include include include include include ifndef TRUE define TRUE 1 endif ifndef FALSE define FALSE 0 endif static void SignalHandler int void PrintUsage char for error exit strip path from filename define DEFAULT TIME 20 define DEFAULT OP stdout define DEFAULT NAME sigtrap define BLACK 033 30m foreground colours define RED 033 31m define GREEN 033 32m define YELLOW 033 33m define BLUE 033 34m define MAGENTA 033 35m define CYAN 033 36m define WHITE 033 37m define ON BLACK 033 40m background colours define ON RED 033 41m define ON GREEN 033 42m define ON YELLOW 033 43m define ON BLUE 033 44m define ON MAGENTA 033 45m define ON CYAN 033 46m define ON WHITE 033 47m define NORMAL 033 0m not bold underline flashing char colours BLACK ON WHITE CYAN ON RED GREEN ON MAGENTA BLUE ON YELLOW BLACK ON CYAN WHITE ON RED BLUE ON GREEN YELLOW ON MAGENTA BLACK ON GREEN YELLOW ON RED BLUE ON CYAN MAGENTA ON WHITE BLACK ON YELLOW GREEN ON RED BLUE ON WHITE CYAN ON MAGENTA WHITE ON BLACK RED ON CYAN MAGENTA ON GREEN YELLOW ON BLUE CYAN ON BLACK RED ON WHITE GREEN ON BLUE MAGENTA ON YELLOW GREEN ON BLACK RED ON YELLOW CYAN ON BLUE WHITE ON MAGENTA YELLOW ON BLACK RED ON GREEN WHITE ON BLUE MAGENTA ON CYAN define N COLOUR 32 char colour choice of colour for this process static int signal SIGINT FALSE flags set by signal handler static int signal SIGQUIT FALSE all response done in main process static int signal SIGHUP FALSE rather than in interrupt routine static int signal SIGTERM FALSE static int signal SIGABRT FALSE static int signal SIGCONT FALSE static int signal SIGTSTP FALSE int main int argc char argv FILE output DEFAULT OP pid t pid getpid get process id int i cycle rc long clktck sysconf SC CLK TCK struct tms t clock t starttick stoptick sigset t mask colour colours pid N COLOUR select colour for this process if argc 2 argc 2 fprintf output s 7d START BLACK NORMAL n colour int pid fflush output signal SIGINT SignalHandler hook up signal handler signal SIGQUIT SignalHandler signal SIGHUP SignalHandler signal SIGTERM SignalHandler signal SIGABRT SignalHandler signal SIGCONT SignalHandler do this intrinsically after return from SIGTSTP due to Darwin BSD inconsistent SIGCONT behaviour signal SIGTSTP SignalHandler rc setpriority PRIO PROCESS 0 20 be nice lower priority by 20 cycle argc 2 DEFAULT TIME atoi argv 1 get tick count if cycle 0 cycle 1 for i 0 i clktck 2 fprintf output s 7d tick d BLACK NORMAL n colour int pid i if signal SIGINT fprintf output s 7d SIGINT BLACK NORMAL n colour int pid exit 0 if signal SIGQUIT fprintf output s 7d SIGQUIT BLACK NORMAL n colour int pid exit 0 if signal SIGHUP fprintf output s 7d SIGHUP BLACK NORMAL n colour int pid exit 0 if signal SIGTSTP signal SIGTSTP FALSE fprintf output s 7d SIGTSTP BLACK NORMAL n colour int pid fflush output sigemptyset unblock SIGSTP if necessary BSD OS X sigaddset sigprocmask SIG UNBLOCK signal SIGTSTP SIG DFL reset trap to default raise SIGTSTP now suspend ourselves signal SIGTSTP SignalHandler reset trap on return from suspension signal SIGCONT TRUE set flag here rather than trap signal if signal SIGABRT fprintf output s 7d SIGABRT BLACK NORMAL n colour int pid fflush output signal SIGABRT SIG DFL raise S
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年地面维修电工变电运行工模拟试题(含答案)
- 拖拉机驾驶员岗前技术知识考核试卷含答案
- 2026年生产文员模拟题及答案详解
- 中小电机笼型绕组制造工安全文明考核试卷含答案
- 2026年生活饮用水监测培训模拟题及答案详解
- 2026年高级车工理论模拟试题(含答案)
- 2026年手写心理模拟题及答案详解
- 2026年农村家庭模拟题及答案详解
- 无方向信标、指点标机务员安全专项竞赛考核试卷含答案
- 企业绿色电力消费与采购中国市场年度报告:2025-2026年进展与“十五五”展望
- 无人机装调检修工艺流程文件
- 班级安全委员培训会议课件
- (正式版)DB61∕T 5053-2023 《湿陷性黄土地区建筑边坡治理技术规程》
- GB/T 4026-2025人机界面标志标识的基本和安全规则设备端子、导体终端和导体的标识
- 校园保安法制教育专题培训
- 会务服务培训课件
- 桡骨远端骨折护理课件
- 启蒙认知冷暖课件
- 单位工作餐订餐协议
- 公立医院低值耗材管理的财会监督改进研究
- 肿瘤免疫治疗与护理
评论
0/150
提交评论