洛陽(yáng)師范學(xué)院UNIX實(shí)驗(yàn)報(bào)告-1_第1頁(yè)
洛陽(yáng)師范學(xué)院UNIX實(shí)驗(yàn)報(bào)告-1_第2頁(yè)
洛陽(yáng)師范學(xué)院UNIX實(shí)驗(yàn)報(bào)告-1_第3頁(yè)
洛陽(yáng)師范學(xué)院UNIX實(shí)驗(yàn)報(bào)告-1_第4頁(yè)
洛陽(yáng)師范學(xué)院UNIX實(shí)驗(yàn)報(bào)告-1_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、測(cè)試過(guò)程:(實(shí)驗(yàn)中出現(xiàn)的問(wèn)題、錯(cuò)誤、解決方法)基本實(shí)現(xiàn)了函數(shù)的功能,在測(cè)試過(guò)程中會(huì)出現(xiàn)無(wú)法解決的問(wèn)題,通過(guò)請(qǐng)教老 師的講解最終達(dá)到了老師的基本要求實(shí)驗(yàn)總結(jié):通過(guò)此次實(shí)驗(yàn)掌握類unix下程序開(kāi)發(fā)的流程;熟悉程序開(kāi)發(fā)有關(guān)的工具軟件;掌握ansi c的編程規(guī)范;掌握文件系統(tǒng)訪問(wèn)的編程方法、數(shù)據(jù)結(jié)構(gòu)、函數(shù);簽名2012 年 2 月 20 日 評(píng)語(yǔ)與成績(jī):教師簽名:年 月日洛陽(yáng)師范學(xué)院信息技術(shù)學(xué)院軟件實(shí)驗(yàn)報(bào)告專業(yè):軟件工程課程:unix高級(jí)編程實(shí)驗(yàn)名稱學(xué)號(hào):姓名:班級(jí):編程環(huán)境實(shí)驗(yàn)類型驗(yàn)證型實(shí)驗(yàn)時(shí)間2012-2-22實(shí)驗(yàn)環(huán)境裝有l(wèi)inux操作系統(tǒng)的虛擬機(jī)環(huán)境實(shí)驗(yàn)?zāi)康呐c要求:掌握類unix下程序開(kāi)發(fā)的流程

2、;掌握ansi c的編程規(guī)范;掌握文件系統(tǒng)訪問(wèn)的編程方法.數(shù)據(jù)結(jié)構(gòu).函數(shù);理解不同系統(tǒng)平臺(tái)下編程的特點(diǎn).區(qū)別。實(shí)驗(yàn)內(nèi)容:編程實(shí)現(xiàn)例程1-5 使用vi編輯程序分別使用gcc和cc編譯生成可執(zhí)行程序分析、驗(yàn)證程序的功能 列出程序中包含的頭文件,分別指出頭文件包含程序中那些函數(shù)的原型 列出程序中用到了那些原始系統(tǒng)數(shù)據(jù)類型 分析該程序在調(diào)用execlp函數(shù)而執(zhí)行子進(jìn)程時(shí),為什么不能傳遞參數(shù)?如果實(shí)現(xiàn)該功能,需要如何修改程序(提出方案)編程實(shí)現(xiàn)指定目錄的遞歸訪問(wèn) 分析、調(diào)試?yán)?-1總結(jié)程序中用到哪些與目錄操作相關(guān)的數(shù)據(jù)結(jié)構(gòu) 如果在例程基礎(chǔ)上增加“遞歸列出一個(gè)目錄中的所有文件”,需要哪些數(shù) 據(jù)結(jié)構(gòu)、如何

3、修改程序的邏輯結(jié)構(gòu)?通過(guò)搜索、查閱資料,提出上述問(wèn)題的解決方案;編程實(shí)現(xiàn)該方案;實(shí)驗(yàn)步驟:(算法描述、源程序、操作步驟和方法)習(xí)題4.5的頭文件有#include<sys/types.h>基本系統(tǒng)數(shù)據(jù)類型#include<sys/wait.h> 進(jìn)程控制#include<stdio.h>定義輸入輸出函數(shù)#include<string.h>字符串處理函數(shù)#include<unistd.h> 符號(hào)常量#include<errno.h> 定義錯(cuò)誤碼#include<stdarg.h>不定參數(shù)列表支持vsprintf(

4、)由<stdarg.h> <stdio.h>printf(),fgets(),fputs(),fprintf(),fflush(),sprintf()需要用到頭文件 <stdio.h> 函數(shù)execlp()需要用到<unistd.h>頭文件以結(jié)尾的數(shù)據(jù)類型被稱為原始系統(tǒng)數(shù)據(jù)類型。它們通常在頭文件<sys/types.h>中定義(頭文件<unistd.h>應(yīng)已包括該頭文件)。1.5的程序用到的原始數(shù)據(jù)類型pidj#include<sys/types.h>#include<sys/wait h>#inc

5、lude<stdio h>#include<stdlibh>#include<string h>#include<unistd h> nclude<errno.h>#include<stdarg.h>define maxline 4096static void err.doit(int,const char *,va_list);char *pname=null;void err_ret(const char fmt,)va.list ap;va.start(ap,fmt); err_doit(l,fmt,ap); va_

6、end(ap);return;void err.sys(const char *fmt.)va.list ap; va_start(apffmt); err_doit(l,fmt,ap); va_end(ap);abort();exit(l);static void err_doit(int errnoflag,const char *fmt,va.list ap)int errno.save;char bufmaxline; errno_save=errno; vsprintf(buf,fmt,ap); if(errnoflag)sprintf (buf+strlen(buf) f n:%s

7、,f, strerror(errno.save); strcat (buf f f,nn );fflush(stdout);fputsfbufpstderr);fflush(null);return;int main(viod)charbufmaxline;pid_t pid;intstatus;printfc'%');while (f get s (buf ,n1axline, st din) !=null)bufstrlen(buf)-1=0;if(pid=fork()<0) err_sys(hfork error11);else if(pid=o) execlp(b

8、uffbuf,(char *)0); err_ret(mcouldnrt execute:%sm,buf); exit(127);if(pid=waitpid(pid,&status,0)<0) err_sys( mwaitpid error'1);printf(m%“);exit(o);在主程序中調(diào)用的 execlp函數(shù)格式為:execlp(buf9buf,(char *)0);因?yàn)閭鬟f的參數(shù)有誤,以至于函數(shù)無(wú)法正常運(yùn)行。rootlocalhostrootlocalhost rootlocalhostmengyawei# vi l_5.c mengyawei# gcc

9、l_5.c -o 1_5 mengyawei# is1_5l_5.crootlocalhostrootlocalhost rootlocalhostmengyawei# mengyawei# mengyawei#path=spath: chmod a+x 1_5 1_5rootlocalhost mengyawei# 1_5%l_5.ccouldn1t execute:1_5.c:permission denied%i習(xí)題1.1#include<sys/typesh> #include<direnth> include<stdio.h> #include&l

10、t;stdlib.h> #includecstring h> #include<unistdh> #include<errno.h>#include<stdarg.h>井define maxline 4096static void err_doit(int.const char *,va.list); char *pname=null;void err_sys(const char *fmt f.)va.list ap; va_start(ap,fmt); err.doit(1,fmt,ap); va_end(ap);exit(l);void e

11、rr_quit(const char 片fmt)va_list ap;va_start(ap,fmt);err.doit(0,fmt,ap);va_end(ap);exit(l);static void err_doit(int errnoflag,const char *fmt,va.list ap)int errno.save;char bufmaxline;errno_save=errno;vsprintf(buf,fmt,ap);iif(errnoflag)sprintf(buf+strlen(buf),h%sh,strerror(errno.save); strcat(buff mn

12、°);fflush(stdout);fputs(buf,stderr); fflush(null);return;int main(int argc.char *argv)dir *dp;struct dirent *dirp;required'*);if(argc!=2)err_quit(na single argument (the directory name) is if(dp=opendir(argv1)=null)errsysccan1t open %sh,argvl);while(dirp=readdir(dp)!=null)printf (u%snh fdir

13、p->d_name); closedir(dp);exit(o);rootlocalhost mengyawei# 1_1 mengyawei1. ci2. c3. c4. crootlocalhost mengyawei# is mengyaweil.c 2.c3.c4.c由題意“遞歸列出一個(gè)目錄中的所有文件”知:應(yīng)在while(dirp=readdir(dp)!=null)printf(“srt,dirp>dame);可寫一子程序,對(duì)傳遞過(guò)來(lái)的參數(shù)(文件名)的屬性進(jìn)行判斷,并輸出文件名,若參數(shù)的屬性為目錄,則該函數(shù)調(diào)用自身(將傳遞的參數(shù)修改為本次輸出的文件名)while循環(huán)體中

14、即為調(diào)用的子函數(shù)名稱,在子函數(shù)體readdir結(jié)構(gòu)體finclude <unistd.h>«include <1inux/direnth>finclude <linux/unistdh>the dirent structure is declared as follows:struct direntlong d_ino;/ inode number */off_t d_off;/* offset to this dirent /unsigned short d_reclen:/ length of this d_name */char cluane

15、 name_max+1;/ file name (null-terminated) */stat結(jié)構(gòu)體#include <sys/types.h> include <sys/stat.h> finclude cunistdh>struct stat dev_tst.dev;/*device */ino.tst.ino;/*inode */rode_tstrode;/*protection */nlink_tst.nlink;/*number of hard links /uid_tst.uid;/*user id of owner */fiicltst_gid;/

16、*group id of owner /dev_tst_rdev;/device type (if inode device) /off.tst.size;/*total size, in bytes */blksize_tst_blksize;/*blocksize for filesyster i/o /blkcnt.tst.blocks;/number of blocks allocated /tiue.tst_atine;/*time of last access */tine_tst_ntine;/*time of last modification */time.tst.ctire

17、;/time of last change */;只能查一級(jí)目錄,因?yàn)檎{(diào)用子函數(shù)checkdir()傳遞的第一個(gè)文件是:文件“。”:本級(jí)目錄文件。子函數(shù)查到文件,判斷該文件為目錄文件,同時(shí)將該文件的地址遞歸傳遞給checkdiro從而進(jìn)入死循環(huán),無(wú)法跳出,最后導(dǎo)致系統(tǒng)溢出。while(dirp=readdir(dp)!=null)checkdir(dirp);closedir(dp);exit(o);void checkdir(struct dirent *dir)struct dirent *dirp;struct stat buf;dirp=dir;printf(m%snm f dirp-

18、>d_name);/if(lstat(dirp->d_name,&buf)<0)/printf (f, error !nh);/if(s_isdir(buf.st.mode)/checkdir(dirp);0void checkdir(struct dirent *dir)struct dirent *dirp;struct stat buf;dirpdir;printf("%sn",dirp->d_name);if(lstat(dirp->d_name,&buf)>=0)if(s_isdir(bufst.mode)ope

19、n| checkdir(dirp);采用 if(s_isdir(buf.st_mode)判斷是否為文件羅或j若不是則checkdir(參數(shù));該算法也陷入死循環(huán),無(wú)法跳出。老師的建議是在if(sjsdir(buf.st_mode)open (文件);read(文件);close(文件);使用讀文件內(nèi)容的方法,讀取本級(jí)目錄文件中的所有內(nèi)容,這樣也省去了遍歷所有目錄文件的頻繁遞歸,自己覺(jué)得這樣就不能夠說(shuō)是遞歸算法了,只能是調(diào)用子函數(shù),閱讀本級(jí)目錄文件的內(nèi)容。?include<sys/types #include<dirent.h> #include<stdio<h&g

20、t; #include<stdlibh> #include<stringh> #include<unistd h> #include<errnoh> #include<stdarg h> #include<sys/stat.h> #define maxline 4096 static void err.doit(int ,const char va_list); char apname=null;void err_sys(const char *fmt,.) va_list ap;vastartcapifmt); err.

21、doit.ap); va_end(ap);exit(l); void err.quit(const char afmt,.-)tva_likt ap;va_startcap,fmt); err.doit co.fmt, ap); va_end(ap);exit(l);static void err.doit(int errnoflag,const char *fmtfva_list ap) int errno.save; char bufmaxline; errno.save=errno; vsprintf(buf,fmtap); if (errnof ltig) sprintf(buf+strlen(buf).m:%sm.strerror(errno.save); strcat(buf.hnh);fflush(stdout); fputs(buf< stderr

溫馨提示

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

評(píng)論

0/150

提交評(píng)論