資源簡介
天津大學計算機科學與技術專業,大三上學期,操作系統原理實驗報告,文件管理、主存管理等。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
//?Simplifed?xv6?shell.
#define?MAXARGS?10
//?All?commands?have?at?least?a?type.?Have?looked?at?the?type?the?code
//?typically?casts?the?*cmd?to?some?specific?cmd?type.
struct?cmd?{
??int?type;??????????//??‘?‘?(exec)?|?(pipe)?‘<‘?or?‘>‘?for?redirection
};
struct?execcmd?{
??int?type;??????????????//?‘?‘
??char?*argv[MAXARGS];???//?arguments?to?the?command?to?be?exec-ed
};
struct?redircmd?{
??int?type;??????????//?
??struct?cmd?*cmd;???//?the?command?to?be?run?(e.g.?an?execcmd)
??char?*file;????????//?the?input/output?file
??int?mode;??????????//?the?mode?to?open?the?file?with
??int?fd;????????????//?the?file?descriptor?number?to?use?for?the?file
};
struct?pipecmd?{
??int?type;??????????//?|
??struct?cmd?*left;??//?left?side?of?pipe
??struct?cmd?*right;?//?right?side?of?pipe
};
int?fork1(void);??//?Fork?but?exits?on?failure.
struct?cmd?*parsecmd(char*);
//運行cmd,Never?returns.
void?runcmd(struct?cmd?*cmd)
{
??int?p[2]?r;
??struct?execcmd?*ecmd;
??struct?pipecmd?*pcmd;
??struct?redircmd?*rcmd;
??if(cmd?==?0)
??{
??????exit(0);
??}
??switch(cmd->type)
??{
??default:
????fprintf(stderr?“unknown?runcmd\n“);
????exit(-1);
??case?‘?‘:
????ecmd?=?(struct?execcmd*)cmd;
????if(ecmd->argv[0]?==?0)
????{
????????exit(0);
????}
????//fprintf(stderr?“exec?not?implemented\n“);
????//?Your?code?here?...
????//在當前目錄下搜索執行文件失敗
????if(execv(ecmd->argv[0]?ecmd->argv)?==?-1)
????{
????????//更換為/bin/目錄
????????char?cmdPath[30]?=?“/bin/“;
????????//連接字符串/bin/與之前的目錄
????????strcat(cmdPath?ecmd?->?argv[0]);
????????//在/bin/目錄下搜索執行文件失敗
????????if(execv(cmdPath?ecmd->argv)?==?-1)
????????{
????????????//更換為/usr/bin/目錄
????????????char?cmdPath2[30]?=?“/usr/bin/“;
????????????//連接字符串/usr/bin/與最初的目錄
????????????strcat(cmdPath2?ecmd?->?argv[0]);
????????????//更換為/usr/bin/目錄搜索執行文件失敗
????????????if(execv(cmdPath2?ecmd->argv)?==?-1)
????????????{
????????????????//執行文件不存在,結束當前進程
????????????????fprintf(stderr?“Can‘t?found?file:?%s%d\n“?ecmd?->?argv[0]?__LINE__);
????????????????exit(0);
????????????}
????????}
????}
????break;
????
????/*
????//如果有權限訪問文件且文件存在
????if(access(ecmd->argv[0]F_OK)?==?0)
????{
????????//找到可執行文件
????????execv(ecmd->argv[0]?ecmd->argv);
????}
????else
????{
????????//有權限訪問文件,將當前的工作目錄改變成/bin/目錄搜索失敗
????????if(chdir(“/bin/“)?0)
????????{
????????????//文件不存在,結束當前進程
????????????printf(“Can‘t?change?directory?%d\n“?__LINE__);
????????????exit(0);
????????}
????????//有權限訪問文件,在/bin/搜索可執行文件成功
????????execv(ecmd->argv[0]?ecmd->argv);
????}
????//既無權限,執行文件又不存在,結束當前進程
????fprintf(stderr?“Can‘t?find?file:?%s?%d\n“?ecmd->argv[0]?__LINE__);
????
????break;
????*/
??case?‘>‘:
??case?‘<‘:
????rcmd?=?(struct?redircmd*)cmd;
????//fprintf(stderr?“redir?not?implemented\n“);
????//?Your?code?here?...
????//‘<‘為0,‘>’為1
????close(rcmd->fd);
?????//open()函數,對產生的新文件賦
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-09-17?20:06??lab1\
?????文件???????10827??2017-10-19?22:15??lab1\lab1.c
?????目錄???????????0??2018-09-17?20:06??lab2\
?????文件????????1569??2017-10-31?21:46??lab2\lab2.c
?????文件?????1652440??2018-09-17?20:11??主存管理實驗報告.doc
?????文件?????2084546??2018-09-17?20:11??文件管理實驗報告.doc
評論
共有 條評論