《操作系统课程实验报告1.docx》由会员分享,可在线阅读,更多相关《操作系统课程实验报告1.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、中央民族大学操作系统实验报告姓名:黄越伶学号:21011656年级:2021级院系:信息工程学院专业:软件工程2023年4月6日U 21:45teacher)teacher-VirtualBox: /桌面 teacher teacher-VtrtualBox:$ chmod a+x hello.shteacherteacher-VtrtualBox:$ ./hello.shb.cdxs.chello.exelab.ctc print.cb.odxs.exehello.shla.c挂载共享文件夹c.hecpl.cHelloWorld.cla.exeIc.h.gchecpl.exeHelloWor
2、ld.oMakefiledx.cgnome-termtnal.desktop ke.cpress.cdx.exehello.cke.exepress.exeIs命令显示完毕 /home/teache/桌面 八Welocme!teacher,the time is: 21:44:27the cal of the monthcEdtt FileeCompute rExitInput your choice: e Show a file Enter file name: b the file no exister-VtrtualBox:teachergteacher-VtrtualBox:b.c b
3、.o c.h c.h.gch dx.c dx.exedxs.c dxs.exeecpl.c ecpl.exe$ ./hello.sh hello.exe hello.sh HelioWorld.c HelloWorld.ognome-terminal.desktop ke.chello.c/home/teacher/桌面ke.exeIs命令显示完毕lab.ctc print.cla.c挂砒共享文件夹la.exeMakefilepress.cpress.exepwd命令显不完毕Welocme!teacher,the time is: 21:46:27the cal of the month Ed
4、it FileCompute ExitInput your choice: r Add: n=7 the end!Qteacherteacher-VirtualBox:/桌面Input your choice:Add:n=7the end! tcachcrQteachcr-VtrtualBox:b.c b.o c.h c.h.gch dx.c dx.exe$ ./hello.sh dxs.chello.exeHvc exehello.shecpi.cHelloWorld.cecpi.exeHelloWold。gnorne- terminal .desktop ke c hello.c人人 D_
5、-exeIs命令显不元毕lab.c lac la.exe Makefile press.c press.exetc print.c挂薮共享文件夹/home 八 eacher/桌面pwd命令显示完毕Welocme!teacher,the time is: 21:46:42the cal of the month Edit File Compute Exit Input your choice:Bye4、编写Makefile文件(1)简单的makefile文件,并执行(建立多个c ,生成一个链接文件) a)建立下面三个源程序 HelloWorld.c 源程序:#include #include “
6、b.c”#include c.h int main(void) (printf (HHelloWorld!yangxingnH);bb();cc();return 0;)b.c源程序如下:bb()(printf(Hasasasasasnn);c.h源程序如下: cc()printf(nccccccccccccccccccccnn);)b)创建Makefile文件(仅拷贝下面代码内容到Makefile文件中):CC=gccEXEC= HelloWorldOBJS= HelloWorld.oall: $(EXEC)$(EXEC): $(OBJS)$(CC) $(LDFLAGS) -o $ $(OB
7、JS)clean:-rm -f $(EXEC) *.elf *.gdb *.o c)在linux终端,输入make命令,此命令将执行Makefile文件.make将生成HelloWorld可执行程序./桌面$ makegcc -c -o HelloWorld.o HelloWorld.cIn file included from HelloWorld.c:2:0:b.c:l:l: warning: return type defaults to tnt -Wtnplicit-tnt bb() AIn file included from Hell.oHorld.c:3:0:c.h:l:l: w
8、arning: return type defaults to tnt cc() Agcc -o HelloWorld HelloWorId.o_teacherQteacher- VirtualBox:*/桌面$ . /HelloWorIdd) ./HelloWorld(运行可执行程序)实验结果:teacherQteacher-VtrtualBox:/桌面$ ./HelloWorId HelloWorldyangxing asasasasascccccccccccccccccccc.teacherteacherVtrtualBox:/臬面$ 5、实现定时启动为当前用户创建cron服务(1) 键
9、入crontab -e编辑crontab服务文件,选择第二项编辑器teacherQteacher-VirtualBox/桌面$ crontab -e no crontab for teacher - using an empty oneSelect an editor. To change later, run Select-editor1 1 /bin/ed2. /bln/nano/home/demo/a.txt(表示 10:30 执行此命令)35 10 * * * ./a.exe/home/demo/b.txt用ctrl+X保存文件,并退出# daemons notion of time
10、and timezones #:Output of the crontab jobs (including errors) is sent throuah# # # # # email to the user the crontab file belongs to (unUss ?e5?rected).祟5TM工犬烧工,, backup of au your user accounts# For # m h */2 30 10 35 10* * 1 ta -zcf /var/backups/home.tgz /hone/don mon dow commandnore information s
11、ee the manual pages of crontab(5) and cron(8)/btn/sh/home/admtn/ deleteFile.sh ls/home/demo/atxt/aexe/home/demo/b.txtAx求助离开H Write Out3读档AWbl剪切文字S对齐Uncut Textffl拼写检查ACA游标位置 跳行(2)查看该用户下的crontab服务是否创建成功,用crontab -1命令teacherteacheVirtualBox: 桌面teacheteacherVlrtualBox: -/桌面$ crontab l# Edit this file to
12、 introduce tasks to be run by cron.n# Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task# To define the time you can provide concrete values for# minute (m), hour (h), day of month (don), month (m
13、on),# and day of week (dow) or use tn these fields (for ,any,),# Notice that tasks will be started based on the cron1s system# daemon1s notion of time and timezones. # Output of the crontab jobs (including errors) is sent through:email to the user the crontab file belongs to (unless redirected).# Fo
14、r example, you can run a backup of all your user accounts# at 5a.m every week with:# e 5 1 tar -zcf /var/backups/home.tgz /home/j For more information see the manual pages of crontab(5) and cron(8)# m h dom non dow command# /2 * * * * /btn/sh/home/admtn/ deleteFlle.sh30 10 * * * ls/home/demo/a.txt四_
15、弛一 /aexe/home/demo/b.txt(3)启动crontab服务一般启动服务用/sbin/service crond start若是根用户的cron服务可以用sudo service crond start这里还是要注意下不同版本linux系统启动的服务的命令也不同,像虚拟机里只需用sudo service cron restart即可,若是在根用户下直接键入service cron start就能启动服务service cron start七“小e1:已己1:1/153180乂:/桌面$ sudo service cron restartsudo teacher 的密码:teac
16、herQteacher-VirtualBox:*/$ sudo service cron restart teacherteacher-VtrtuaflLBox:/M$ ps -ax|grep cron4721 ?Ss6:00 /usr/sbin/4728 pts/4S+0:00 grep color=auto c ror(4)查看服务是否已经运行用ps -ax | grep cronteacherteacher-VtrtualBox:/MS$ ps -ax|grep cron4721 ?Ss0:00 /usr/sbin/cron4728 pts/4S+0:00 grep -color=aut
17、o cron四、结果分析1、编写shell脚本文件分析运行结果:1):1secho ”Is命令显示完毕”/将会显示目录下的所有文件2):pwdecho pwd命令显示完毕”回打印出当前终端所在的目录3):read choice输入自己的选择c| |e| |r| |q4):类似c语言中的swicth语句C:选择c会执行以下语句:c|C) echo nenter month:nread monthecho nenter yearnread yearcal $month $year set n+=l 输入一个可读月份,然后再输入一个可读年份,最后在用cal命令打印输入的年份和月份,然后 n=n+lo
18、E:选择e会执行以下语句:e|E) echo ”Show a fileo Enter file name: read fileif -f $file ;thenmore $fileelseecho the file no exist”exit 1 fi 需要输入一个文件名如果文件存在就执行more $flle语句,不存在就打印the file no exist然后结束 进程。R:选择r会执行以下语句:r|R) n=l(n=n+6) echo Add: n=$n 打印结果为:Add: n-7Q:选择q会执行以下语句:qlQ) echo Bye1 exit 1;打印结果为:Bye2、编写Makef
19、ile文件运行结果分析CC=gccEXEC= HelioWorldOBJS= HelloWorld.oall: $(EXEC)$(EXEC): $(OBJS)定义一个常量CC来表示gcc命令定义一个常量EXEC来表示HelioWorld文件 定义一个常量OBJS来表示HelloWorld.o文件选择目标文件HelioWorld可执行目标文件HelioWorld依赖于HelloWorld.o$(CC) $(LDFLAGS) -o $ $(OBJS) 表示 gcc pwd -o HelioWorld HelloWorld.oclean:-rm -f $(EXEC) *.elf *.gdb *.o3
20、.编译c程序打印语句为:Hello World!五、实验心得经过上机实验后我完成了实验目的,掌握了 Linux的基本命令,学会在终端运行c语言程序、 shell脚本程序,同时还学习了 Makefile的一些的一些基础编写规则以及学会如何实现定时启动,收 获了知识和实验经验。实验一Linux的初步认识一、实验目的1、熟练使用linux常用命令2、编译C程序3、编写shell脚本文件4、编写Makefile文件5、实现定时启动二、实验原理linux常用命令c语言编程语法,shell脚本语法,Makefile编写语法规则:(l)LDFLAGS是告诉链接器从哪里寻找库文件(2).makefile的命令
21、行以tab键开始makefile的目标必须放在第一个命令行,如果有多个目标,可以使用:all:target 1 target? target3(4).VPATH可以设置源文件搜索路径(5)通配符;三、实验程序及实验结果1、熟练使用linux常用命令:PS-查看系统的进程tty-查看当前使用的终端df-查看系统的文件系统的空间使用情况du-查看具体某个文件和目录的磁盘空间的占用情况free-查看使用的内存资源的情况一些其他常用的Linux命令:cal-显示日历clear-清屏cd-改变当前目录命令chmod-变更档案属性rmdir-删除子目录命令rm-删除文件命令cp-文件复制命令find-查找
22、文件groups查看当前登录用户的组内成员 whoami查看当前登录用户名date-显示系统的日期和时间 pass wd-更改密码的指令 pwd-显示目前所在目录的指令 mkdir-建立子目录 touch-建立一个文件mv-文件改名命令1s一列出指定目录的内容mucmuc: */lab-lTIME00:00:0000:00:00CMD bashPSPID TTY2562 pts/O2707 pts/0Mem:Swap:total 1024028 998396used 391112 159400free86252838996shared buff/cache9572546664available
23、487268Filesystem udevIK-blocks494228Used Available Use% Mounted on04942280% /devtmpf s10240411401012642% /run/dev/sdal7173040614715664014491% /tmpf s51201205120120% /dev/shmtmpf s5120451161% /run/locktmpf s1024001401022601% /run/user/1000Operation not permittedmucmuc:-*/lab-l$ tty/dev/pts/0mucmuc:*/
24、lab-l$ dfdf: /run/user/lO00/doc:mucmuc:-/lab-l$ du4.mucmuc:-/lab-l$ freefoundmucmuc:*/lab-l$ cal bash: cal: command not mucmuc:-/lab-l$ datemucmuc:*/lab-l$ cal bash: cal: command not found muc(amuc :*/lab-l$ dateTue 04 Apr 2023 08:04:05 AM EDTmucmuc:*/lab-1$ passwd Changing password for muc.Current
25、password: 1mucmuc:-/lab-l$ pwd /home/muc/lab-1mucmuc:-/lab-l$ chmod chmod: missing operand Try 1chmod -help1 for more information. mucmuc:-/lab-l$ rmdir rmdir: missing operandTry 1 rmdir -help1 for more information. muc(amuc :*/lab-l$ rm rm: missing operandTry 1 rm -help1 for more information.|mucmu
26、c: -/lab-l$ cp cp: missing file operand Try 1 cp -help1 for more information. mucmuc:-/lab-l$ find mucmuc:-/lab-l$ Ismucmuc:-/lab-l$ mv mv: missing file operandTry 1mv -help1 for more information. muc(amuc :-*/lab-l$进入根目录 进入目录cd / cd * cd /usr/src/Is | wc wc a.txtIs Igrep a ps kill -9 pid 号 more a.t
27、xtaimucmuc:*/lab-l$ mv mv: missing file operand Try *mv -help* for more information. jnucmuc:-/lab-l$ mkdir mkdir: missing operand Try 1mkdir -help* for more information. smucmuc:*/lab-l$ touch touch: missing file operand Try touch -help* for more information. mucmuc:*/lab-l$ groups muc cdrom floppy
28、 audio dip video plugdev netdev bluetooth Ipadmin scanner mucmuc:*/lab-l$ whoami muc mucmuc:*/lab-l$Is显示目录统计指定文件中的字节数、字数、行数,并将统计结果显示输出查看进程pid号功能 杀死进程重定向写入amore amore b.txtaIs |head -2head-10 *.txt显示文件前10行tail -2 a.txthistory显示执行命令的历史./a.out (c执行程序),b (无more b(无显示文件内容./a.out &w 执行这项指令可得知目前登入系统的用户有那些人
29、,以及他们正在执行的程序。su 切换用户的工具pstree显示系统进程树wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。wc -c filename:显示一个文件的字节数wc -m filename:显示一个文件的字符数wc -1 filename :显示一个文件的行数wc -L filename:显示一个文件中的最长行的长度wc -w filename:显示一个文件的字数mucmuc:*/lab-l$ cd *bash: cd: : No such file or directorymucmuc:-/lab-l$ cd /usr/src/mu
30、cmuc:/usr/src$mucmuc:-/lab-l$ cd / mucmuc:/$ .muc(amucIs | wc2265mucmuc:-/lab-l$ wc a.txt1 7 39 a.txtmucmuc:-/lab-l$ la|grep a bash: la: command not found mucmuc:-/lab-1$ Is|grep a a.txt mucmuc:-/lab-l$ psPID TTYTIMECMD3598 pts/000:00:00bash3655 pts/000:00:00psmuc(amuc :-*/lab-l$ s|muc(amuc:-/lab-l$
31、Is | wc65mucQmuc:-/lab-l$ wc a.txt1 7 39 a.txt mucmuc:-/lab-l$ la|grep a bash: la: command not found muc(amuc:*/lab-l$ Is | grep a a. txt muc(amuc: -/lab -1$ psPID TTYTIMECMD3598 pts/O00:00:00bash3655 pts/O00:00:00psmucmuc:*/lab-l$ kill -9 pidbash: kill: pid: arguments must be process or job IDs muc
32、(amuc:-/lab-l$ kill -9 pid haobash: kill: pid: arguments must be process or job IDs bash: kill: hao: arguments must be process or job IDs mucmuc:*/lab-l$ more a.txta mucmuc:*/lab-l$ more a nihaoyanihao this is my text of lab.mucmuc:-/lab-l$ more b.txta more: cannot open b.txt: No such file or direct
33、ory muc(amuc :*/lab-l$ Is | head -2 a a. txtmuc(amuc :-/lab-l$ | muc(amuc: -/lab-l$ head -10 a. txt nihaoyanihao this is my text of lab. muc(amuc:-/lab-l$ tail -2 a.txt nihaoyanihao this is my text of lab. muc(amuc:*/lab-l$ history1 ssh2 ifconfig3 yum4 apt-get install build-essential 5 apt-get insta
34、ll yum6 sudo -s7 apt-get install yum8 sudo root9 sudo -s10 su -s11 sudo -s12 cd /etc/13 Is -I14 vi sudoersmucmuc:*/lab-l$ ./a.out &JI 3790mucmuc:-*/lab-l$ bash: ./a.out: No such file or directoryload average:LOGIN 07:41 ./a.out0.13, 0.12, 0.17 IDLE JCPU PCPU WHA 1:12m 0.01s 0.01s /u?w 08:43:19 up 1:
35、12,1 user,?USERTTYFROMmuctty2tty21+ Exit 127 mucmuc:-/lab-l$ su Password:rootmuc:/home/muc/lab-1# | Dcbian32 (备 1 江在.一视图 网nun ico * G Dja a .向+mucmuc: */lab-lmucmuc:*/lab-l$ pstree systemd(ModemManager-2*ModemManagerNetworkManager2*NetworkManager-accounts-daemonanacronavahi-daemon2*accounts-daemonav
36、ahi-daemoncolord2*colordcroncups-browsed2*cups-browsedcupsddbusdbus-daemonfwupd4*fwupdgdm3|gdm-session-worL2*gdm3gdm-wayland-sesignome-session-b3*gnom+ 12*gdm-wayland-ses2*gdm-session-worgnome-keyring-d3*(gnome-keyring-dpackagekitdphttpsL3*packagekitdpolkitd2*polkitdrsyslogd3*rsyslogdrtkit-daemonssh
37、d switcheroo-cont2*rtkit-daemon2*switcheroo-cont2.编译c程序代码:1 #include2 int main()3 printf(Hello,worid!);4 return 0;5 )6|运行结果:mucmuc :-*/lab-l$ mucmuc:/labl$ muc(9muc :-/lab-l$ mucmuc:-/Lab-l$vi hello.cvi hello.cgcc hello.c -o hello.exe ./hello.exeHello,world!mucmuc:-/lab-l$3、编写shell脚本文件#scriptname:sh
38、ell_one#writer:xxxx#select what you do#!/bin/bashIsecho Is命令显示完毕”pwdecho pwd命令显示完毕”echo nWelocme!$LOGNAME,the time is: $(date +%T) echo ,necho nthe cal of the monthcecho “Edit Fileenecho ”Computernecho Exitq #choice为定义的变量名echo Input your choice: read choicecase $choice inc|C) echo enter month:1read
39、monthecho enter year”read year cal $month $year set n+=l e|E) echo “Show a file。Enter file name: read fileif -f$file;thenmore $fileelseecho nthe file no existexit 1fir|R) n=l(n二n+6)echo Add: n=$nq|Q) echo “Bye”exit 1;*) echo illegal option1echo n$choice is not of the choices!esac echo nthe end!”(2)存
40、盘后,在命令行终端设置权限:chmod a+x hello.sh$ chmod a+x hello.shb.c b.o c.h c.h.gch dx.c dx.exeeacncr-v dxs.c dxs.exe ecpl.c ecpl.exea iBox:$ ./hello.sh hello.exe hello.sh HelioWorld.c HelloWorld.ognome-terminal.desktop ke.chello.c/home/teacher/桌面ke.exeIs命令显示完毕lab.c la.c la.exe Makefile press.c press.exertnt .c共享文件夹pwd命令显不完毕Welocme!teacher,the time is: 21:44:27the cal of the month Edit File Compute Exit Input your choice:(3)执行:./hello.sh每次运行时,输入e,c,q三个字母之一,去完成相关任务