天津大学操作系统原理实验
天津大学计算机科学与技术专业,大三上学期,操作系统原理实验报告,文件管理、主存管理等。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-09-17 20:06 lab1
文件 10827 2017-10-19 22:15 lab1lab1.c
目录 0 2018-09-17 20:06 lab2
文件 1569 2017-10-31 21:46 lab2lab2.c
文件 1652440 2018-09-17 20:11 主存管理实验报告.doc
文件 2084546 2018-09-17 20:11 文件管理实验报告.doc
#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; // < or >
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
“);
exit(-1);
case ‘ ‘:
ecmd = (struct execcmd*)cmd;
if(ecmd->argv[0] == 0)
{
exit(0);
}
//fprintf(stderr “exec not implemented
“);
// 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
“ 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
“ __LINE__);
exit(0);
}
//有权限访问文件,在/bin/搜索可执行文件成功
execv(ecmd->argv[0] ecmd->argv);
}
//既无权限,执行文件又不存在,结束当前进程
fprintf(stderr “Can‘t find file: %s %d
“ ecmd->argv[0] __LINE__);
break;
*/
case ‘>‘:
case ‘<‘:
rcmd = (struct redircmd*)cmd;
//fprintf(stderr “redir not implemented
“);
// Your code here ...
//‘<‘为0,‘>’为1
close(rcmd->fd);
//open()函数,对产生的新文件赋
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-09-17 20:06 lab1
文件 10827 2017-10-19 22:15 lab1lab1.c
目录 0 2018-09-17 20:06 lab2
文件 1569 2017-10-31 21:46 lab2lab2.c
文件 1652440 2018-09-17 20:11 主存管理实验报告.doc
文件 2084546 2018-09-17 20:11 文件管理实验报告.doc
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)