期末考核題目_第1頁
期末考核題目_第2頁
期末考核題目_第3頁
期末考核題目_第4頁
期末考核題目_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、期末考核內容每人按照老師的要求,順序或隨即從下面shell編程部分和環(huán)境編程部分分別選擇一題,然后在課堂上進行編程、演示運行結果,并回答老師至少三個問題。如不能回答,則不通過考核。通過考核后,應根據(jù)兩個題目的題目分析、編程思想、相關知識、流程圖、算法設計(如有)、源代碼及注釋,完成實踐課程報告,并打印后提交。老師根據(jù)平時考勤、演示結果、 回答問題情況、實驗報告情況打分。定于第九周周四上午進行考核。請各位同學做好準備。Shell編程部分:1、設計一個程序cuts,它由標準輸入讀取數(shù)據(jù),獲取有第一個參數(shù) n和第二個參數(shù) m所限定范圍的數(shù)據(jù),n和m都是整數(shù),即從輸入的字符串中抽取第n個字符至第m個字

2、符之間的所有字符(包括這兩個字符)并顯示出來。$ cuts 11 14This is a test of cuts program(回車)test (顯示結果)P1454.18 #!/bi n/bash"TEXTread -p ”請輸入一段內容:read -p "請輸入兩個整數(shù),中間用空格分開:"n um1 num2 echo $TEXT | cut -b $n um1-$ num2 2>/dev/ null2、利用for循環(huán)將當前目錄下的.c文件移到指定的目錄下,并按文件大小排序,顯示移動 后指定目錄的內容。P144 4.12 #!/bi n/bashfo

3、r file in 'Is -1 /root/a | gre p ".*.c"'mv /root/a/$file /root/bls -IS /root/b3、編寫一個shell腳本,求斐波那契奇數(shù)列的前10項及總和。P144 4.14 #!/bi n/bash i=1 j=0 for ( k=0; k<10; k+ )doecho -n "$i "let m=ilet i+=jlet j=m do ne echo ""4、編寫一個shell腳本,程序運行時,要求輸入兩個數(shù)字。然后判斷數(shù)字的大小,如果兩個數(shù)字的和

4、大于100,就輸出“x+y>100”;如果兩個數(shù)字的和小于等于 100并且大于10, 就輸出"x+y>10” ;如果x>y,則求出x除以y的余數(shù)并輸出“ x mod y = ?(把計算的值代 替問號)”,否則求出y除以x的余數(shù)并輸出“ y mod x = ?” .P127 例 4.135、編寫一個shell腳本,并利用函數(shù)實現(xiàn)數(shù)列求和運算。即主程序接受兩個數(shù)字,分別作 為數(shù)列頭和數(shù)列尾,如果第一個數(shù)字不小于第二個數(shù)字則輸出“wrong number ”,否則將這兩個數(shù)字傳遞給函數(shù);函數(shù)把這兩個數(shù)字中間的數(shù)字加起來求和,并輸出。女口:當輸入3 6時,函數(shù)計算 3+4+

5、5+6的值并輸出。#!/bi n/sh fun()sum=0for i in 'seq $1 $2'dosum='ex pr $i + $sum'doneecho $sum echo "PI ease input 2 nu mber:" read a b expr $a + 0 1>/dev/ null 2>&1 if $? -ne 0 ;the necho "wrong nu mber"exit 1;fi expr $b + 0 1>/dev/ null 2>&1 if $? -n

6、e 0 I1;thenecho "wrong nu mber"exit 1;fi if $a -ge $b ;thenecho "wro ng nu mber"exit 1;fi fun $a $b編寫特洛伊木馬 shell腳本。以文本方式登陸界面,接收用戶輸入的名字和密碼。一旦 用戶在這種偽裝界面登陸,就獲取用戶名和密碼,并保存在指定的文件中。接著睡眠幾 秒,然后顯示錄入錯誤的信息,使用戶以為輸入有誤。最后在調用真正的登陸程序,允 許用戶正常登陸。P107 例 4.67、8、+行數(shù),編寫shell腳本,創(chuàng)建一個 5個元素的數(shù)組,輸出各個元素的值 編寫一

7、個腳本,用于統(tǒng)計所指定文件的行數(shù)總數(shù)。輸出結果為各個文件的名字 以及總行數(shù)。如:當輸入./腳本名file1 file2,腳本計算file1和file2兩個文件的總行數(shù)#!/bi n/bash#statistics the raws of the files you input usage()echo "usage:p lease input >= two file names"totalli ne=0 |if $# -lt 2 ; thenusage fiwhile $# -ne 0 dolin e='cat $1 |wc -I'echo "

8、$1:$li ne"totalli ne=$ $totalli ne+$li ne shiftdone Iecho ""echo "totalli ne: $totalli ne"編寫一個shell腳本,根據(jù)鍵盤輸入的值,給出對應的提示;輸入A -Z ,輸出UppercaseLetter;輸入a-z,輸出 Lowercase Letter;輸入0-9,輸出 Digit;輸入其他,輸出 Punctuation, whites pace, or other#!/bi n/bash export LC_ALL=C read a while $a do

9、case $a inA-Z) echo "Upp ercase Letter"a-z) echo "Lowercase Letter"0-9) echo "Digit"*) echo "Pun ctuati on, whites pace, or other"esacread a do ne10、編寫shell腳本,提示用戶輸入用戶名,并判斷此用戶名是否存在與系統(tǒng)用戶中P123 例 4.11Linux環(huán)境編程部分:1、編寫一個程序,把一個文件的內容復制到另一個文件上,即實現(xiàn)簡單的copy功能,要求:只用open()

10、/read()/write()/close()系統(tǒng)調用,程序的第一個參數(shù)為源文件,第二個參 數(shù)為目的文件。P225 7.3#in elude <stdio.h> #in elude <stdlib.h> #in elude <uni std.h> #in elude <sys/t yp es.h> #in clude <sys/stat.h> #in clude <fcn tl.h>#define BUF SIZE 1024*8 int mai n()int fds, fdd;char bufBUF_SIZE;size_t

11、 hasread = 0;fds = open ("filea", O_RDONLY);fdd = open( "fileb", O_WRONLY, O_CREAT);if(fds && fdd)while(hasread = read(fds, buf, sizeof(buf) > 0)write(fdd, buf, hasread);close(fds);close(fdd);2、編寫一個程序它利用 fork ()創(chuàng)建一個子進程;父進程打開一個文件,父子進程都向文 件中寫入信息(利用 write),表明是在那個進程中;每個進程都

12、打印兩個進程的ID號。最后父進程執(zhí)行wait ()。P225 7.5 #in clude<stdio.h> #in clude< uni std.h> #in clude<malloc.h> #in clude<fc ntl.h> #in clude<stri ng.h> #in clude<stdlib.h> int main() pid_t pid;int fd;char *buf1=(char*)malloc(20);char *buf2=(char*)malloc(20);strc py(buf1,"th

13、isis child process:");strc py(buf2,"thisis parent process:");if(fd=o pen ("file",O_CREAT|O_RDWR)<0)printf("openfile error");if(p id=fork()=0) if(write(fd,buf1,strle n(buf1)<0)prin tf("writeerror!");exit(0);prin tf(" pid:%d",get pid();close

14、(fd);else if(write(fd,buf2,strlen(buf1)<0) printf("write error!");exit(0);close(fd);wait(NULL);printf("ppid:%d",getppid();return 0;3、編寫一個程序,他創(chuàng)建一個子進程。父進程向子進程發(fā)送一個信號,然后等待子進程終 止;子進程接受信號,輸出自己的狀態(tài)信息,最后終止自己。P225 7.9 #include<stdio.h> #include<unistd.h> #include<malloc.h

15、> #include<fcntl.h> #include<string.h> #include<stdlib.h> int main() pid_t pid;int fd;char *buf1=(char*)malloc(20);char *buf2=(char*)malloc(20);strcpy(buf1,"thisis child process:");strcpy(buf2,"thisis parent process:");if(fd=open("file",O_CREAT|O_RD

16、WR)<0)printf("open file error");if(pid=fork()=0)if(wnte(fd,buf1,strle n(buf1)<0)prin tf("writeerror!");exit(0);prin tf(" pid:%d",get pid();close(fd);else if(write(fd,buf2,strle n(buf1)<0)prin tf("writeerror!");exit(0);close(fd);wait(NULL);prin tf(&quo

17、t; pp id:%d",get pp id();return 0;寫管道的程序打開管道,main函數(shù)里的參數(shù)由用戶輸入要寫入的內容。讀管道創(chuàng)建管道,讀出了用戶寫入用戶寫入管道的內容。這兩個函數(shù)用的是非阻塞度寫管道打開open函數(shù)創(chuàng)建的文件,然后對此文件進行讀寫操作(將文件打開屬性改為可讀可寫,文件權限要做相應更改)。接著,寫入"Hello!l am writing to this file! ”,此時文件指 針位于文件尾部。 接著再使用Iseek函數(shù)將文件指針移動文件開始處,并讀出10個字節(jié)并將其打印出來#i nclude <stdio.h>#in clude

18、 <sys/t yp es.h>#in clude <sys/stat.h>#in clude <fcn tl.h>#in clude <uni std.h>#in elude <stri ng.h> int mai n() int fd = -1;fd = open ("zhidao_561804018.dat", O_CREAT | O_TRUNC | O_RDWR, 0666)if (fd < 0) p error(" open “);return -1;char buff64;strcpy(

19、buff, "Hello!l am writi ng to this file!"); int count = strle n( buff);if (write(fd, buff, count) < 0) p error("write");return -1;if (lseek(fd, 0, SEEK_SET) < 0) p error("lseek");return -1;if (read(fd, buff, 10) < 0) p error("read");return -1;buff10 =

20、 0x00;prin tf("%sn", buff);if (fd > 0) close(fd);fd = -1;return 0;10s 在/tmp/dameon.log 中寫入一句話6、首先建立一個守護進程,然后讓該守護進程每隔#in clude <stdio.h>#in elude <stdlib.h>#in clude <stri ng.h>#in clude <fcn tl.h>#in clude <sys/t yp es.h> #in clude <uni std.h> #i nclu

21、de <sys/wait.h> #defi ne MAXFILE 65535 int mai n(void)pid_t pc;int i,fd,len;char *buf="This is a Dameonn"len=strlen(buf);pc=fork();if(pc<0)printf("error forkn");exit(1);else if(pc>0)exit(0);setsid();chdir("/");umask(0);for(i=0;i<MAXFILE;i+)close(i);while(

22、1)if(fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600)<0)perror("open:");exit(1);write(fd,buf,len+1);close(fd);sleep(10);0600。7、 在 tmp 目錄下調用 open 函數(shù),以可讀可寫的方式創(chuàng)建 hello.c 文件。 open 函數(shù)帶有 3 個flag參數(shù):O_CREATE O_TRUNG O_WRONLY文件權限設置為& 首先創(chuàng)建一個子進程,然后讓其子進程暫停5s(使用sleep函數(shù))。接下來對原有

23、的父進程使用 waitpid 函數(shù),并使用參數(shù) WNOHANG 使該父進程不會阻塞。若有子進程退出, 則 waitpid 返回子進程號;若沒有子進程退出,則 waitpid 返回 0,并且父進程每隔 1s 循 環(huán)判斷一次11fx=:汀嚴創(chuàng)的子進程和12UCpc<a)嚴出鶴處理仃13I14jrlntf C "Error fork cT);1514cisp iffpt-a)廠子進S*/1?It廠子進程WffssV19prtntf C 'I NF the chnd prngres? . I ir g«tng 51eep! v'";20&le

24、epC5);71prinT("T an the child.T an 弓百tng tscttll n" j ;22嚴子盜程正常a岀暫23eKtt(o;24ZStist /父誑程士f2627廠«環(huán)測試子進程*ffiS出r28do2*30八tfl用haUpldCb且處進程不阻B/Itpr-tfdttplclpc pNULL3Z廠苦子as還出.則父進S暫#11*/33IfCpr0)八邀00說明子進程awjfitfiv34<prtntf I a-n the fat he r prggress .Th& chtld pirocess hits not exit

25、ed ' ):36UeepCil;5738 whlTt(pr=O);29握現(xiàn)子進程jfi出.打印出相ffitt況*#4elf(pr=pc)4142prlrbtF("r an the father progress11 get child exit>:曲” *p;4344else4544prlntfSone Error occured.n*);47 ,.ww訓iriiixi;:(' :T;T9、首先使用fork子進程中使用進程中調用kill向子進程發(fā)送信號,請使用創(chuàng)建一個子進程,接著為了保證子進程不在父進程調用kill之前退出,在raise函數(shù)向子進程發(fā)送 SIG

26、STOp言號,使子進程暫停。接下來,再在父SIGKIL10、/* ktll_fatse.cSCtt */ ULncAuduvMtdid'hK ttncludcstndl.h# LKjr<3tdHb. h> #lnclLniefys7types .h> Vl n elude vsy&/w4tt.h:> IntFiainC JQ91011IZ1314151617le2021ZZ242526Z72B19303132羽34353a373B39FiW_t pld:lit ret;/til-個子 »®*/ tf(pid-fi>rk()-;o

27、)嚴出猜竝 a 彗(>rtnt f ( "Fork prrortfiptd=C)嚴子進程*八崔子進程中«用rat«C)JR«ajltSTOPfg號,ffi于ass停睥prtntf ("I ari chtld progrcsCpld: i) .1 or “ItAng for my ilgnjL ' ,getptd(); ratse(si&$T0P):prtntf("I child pr«grtsiCpt: ).i 加 冀VU*d 鈔 progreti; 'getpid(.etppidf: ?Kit

28、(3J;el« "父逬程叮sieep(2>:先it乂進H休e. tt子atsmfi,旬igs»試一試打廠在交證程單收JI子進程岌岀的倍號,并ii用帽應的操ff* U(wd<.tpldtpld.FlULL,>JhOHAMC)=9)嚴若|>1時|向的子進程a有週出.卿1回乂進S革集執(zhí)疔下邊的«旬呻prntf=C'i an piren,t prcjrestfptd : ),I kU 一 " ,getpd( *ptcl;WflLtpld(pLd子進程退出,i阻金/首先創(chuàng)建一個共享內存區(qū),之后將其映射到本進程中,最后再解除

29、這種映射關系。/*創(chuàng)建共享內存*/int shmid; void *shmaddif (shmid = shmget( IPC_P RIVATE,BUFSZ,0666) < 0 ) p error(exit(-1);syetem("fail to shmget“ ” ipcs - m );”);/*映射共享內存*/if (shmadd = shmat(shmid,0,0) = NULL ) p error(exit(-1);syetem(“fail to shmat“ipcs - m'I);”);/*刪除共享內存*/if (shmdt(shmadd) < 0 )

30、”);p error(“ fail to shmdtexit(-1);syetem( “ ipcs - nr”);if (shmctl(shmid, I PC_RMID, NULL) < 0 )perror(exit(-1);“fail to shmcti”);ipcs - m );11、if(sigactio n(SIGINT,&act,NULL)/注冊信號SIGINT處理函數(shù)為syetem(首先把SIGQUIT SIGINT兩個信號加入信號集,然后將信號集設為阻塞狀態(tài),并在 該狀態(tài)下使程序暫停 5s.接下來再將信號集設置為非阻塞狀態(tài),再對這兩個信號分別操 作,其中SIGQUIT執(zhí)行默認操作,而 SIGINT執(zhí)行用戶自定義函數(shù)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論