【操作系统实验三】系统调用

1.修改”/home/shiyanlou/oslab/linux-0.11/include”目录下的”unistd.h”文件,对新增系统调用增加相应的宏定义:

实验楼

实验楼

2.修改”/home/shiyanlou/oslab/linux-0.11/kernel”目录下的”system_call.s”文件,扩大系统调用函数的数量:

实验楼

3.修改”/home/shiyanlou/oslab/linux-0.11/include/liunx”目录下的”sys.h”文件,使0x80号中断能调用相应的系统函数:

图片描述

4.”/home/shiyanlou/oslab/linux-0.11/kernel”目录,创建”who.c”文件

who.c
-----------------------------
#define __LIBRARY__
#include <unistd.h>
#include <errno.h>
#include <asm/segment.h>
char usnm[64]={0};
int sys_iam(const char* name)
{
    int result=0; int cnt;
    while(get_fs_byte(name+result) != '\0' && result<64) result++;
    if(result>23) return -EINVAL;
    else
    {
        for(cnt=0;cnt<=result;cnt++){usnm[cnt]=get_fs_byte(name+cnt);}    
        return result;    

    }

}


int sys_whoami(char* name, unsigned int size)
{
    int result=0; int cnt;
    while(usnm[result] != '\0' && result<64) result++;
    if(result>size) return -1;
    else
    {
        for(cnt=0;cnt<=result;cnt++){put_fs_byte(usnm[cnt],(name+cnt));}    
        return result;
    }

}

-----------------------------------------

5.修改”Makefile”文件:

打开”/home/shiyanlou/oslab/linux-0.11/kernel”目录,然后用”gedit”软件打开”Makefile”,将文件第29行修改为:

signal.o mktime.o who.o

在原第50行和第51行之间增加如下代码:

who.s who.o:who.c ../include/unistd.h ../include/errno.h \

../include/asm/segment.h

6.挂载虚拟机硬盘(在oslab目录下执行命令$ ./mount-hdc),打开目录”/home/shiyanlou/oslab/hdc/usr/include”,添加头文件”usname.h”:

usname.h
---------------
#define __LIBRARY__
#include <unistd.h>
#include <errno.h>
#include <asm/segment.h>
_syscall1(int,iam,const char*,name)
_syscall2(int,whoami,const char*,name,unsigned int,size)
-------------

7.将文件:

“/home/shiyanlou/oslab/linux-0.11/include/unistd.h”

复制到以下路径:

“/home/shiyanlou/oslab/hdc/usr/include”;

将文件:

“/home/shiyanlou/oslab/linux-0.11/include/linux/sys.h”

复制到以下路径:

“/home/shiyanlou/oslab/hdc/usr/include/linux”;

8.在”/home/shiyanlou/oslab/hdc/usr/root”目录下编写程序”iam.c”和”whoami.c”:

iam.c
----------
#include <usname.h>
int main(int argc,char* argv[])
{
    if(argc>1) {if(iam(argv[1])<0) return -1;}
        else return -1;
        return 0;
}
whoami.c
---------------
#include<usname.h>
#include<stdio.h>
int main(void)
{
    char str[128];  
    if(whoami(str, 24)<0) return -1;
    else printf("%s\n", str);
    return 0;
}

9.从”/home/teacher”拷贝文件”testlab2.c”和”testlab.sh”至以下路径:

“/home/shiyanlou/oslab/hdc/usr/root”

10.取消虚拟机硬盘挂载,运行虚拟机:

(1)键入以下三个命令:

gcc –o testlab2 testlab2.c

gcc -o iam iam.c -Wall

gcc -o whoami whoami.c -Wall

(2)键入以下两个命令:

./iam aa

./whoami

之后键入:

./testlab2

(3) 键入以下两个命令:

chmod +x testlab2.sh

./testlab2.sh

实验结果截图:

实验楼

实验楼

历史命令

95 tar zxvf hit-oslab-linux-20110823.tar.gz

96 cd oslab/

97 cd linux-0.11/

98 ls

99 cd include/

100 ls

101 gedit unistd.h

102 cd ../kernel/

103 gedit unistd.h

104 ls

105 gedit system_call.s

106 cd ../include/

107 ls

108 cd linux/

109 ls

110 gedit sys.h

111 cd ../..

112 cd kernel/

113 touch who.c

114 gedit who.c

115 ls

116 gedit Makefile

117 make all

118 ls

119 make

120 ls

121 cd /oslab

122 cd /

123 cd /home

124 ls

125 cd shiyanlou/

126 ls

127 cd oslab/

128 ls

129 sudo ./mount-hdc

130 cd hdc

131 ls

132 cd usr

133 cd include

134 ls

135 touch usname.h

136 gedit usname.h

137 ls

138 cp /home/shiyanlou/oslab/linux-0.11/include/unistd.h /home/shiyanlou/oslab/hdc/usr/include

139 cp /home/shiyanlou/oslab/oslab/linux-0.11/include/unistd.h /home/shiyanlou/oslab/oslab/hdc/usr/include

140 cp /home/shiyanlou/oslab/oslab/linux-0.11/include/linux/sys.h /home/shiyanlou/oslab/oslab/hdc/usr/include/linux

141 cd ../root

142 ls

143 touch iam.c

144 gedit iam.c

145 touch whoam.c

146 gedit whoam.c

147 ls /home/teacher/

148 cp /home/teacher/testlab2.c .

149 ls

150 cp /home/teacher/testlab2.sh .

151 ls

152 cp whoam.c whoami.c

153 ls

154 rm whoam.c

155 ls -l

156 cat whoami.c

157 rm whoam.c~

158 ls -l

159 cd /home//shiyanlou/

160 cd oslab/

161 ls

162 sudo umount hdc

163 ./run

164 cd linux-0.11/

165 make all

166 ls

167 cd ..

168 ./run

169 ls

170 sudo ./mount-hdc

171 cd hdc

172 cd usr

173 ls

174 cd ../include/

175 ls

176 gedit usname.h

177 gedit unistd.h

178 cd ../root

179 gedit whoami.c

180 cd ../../..

181 ls

182 sudo umount hdc

183 ./run

184 ./mount-hdc

185 cd hdc/usr/root

186 cd hdc

187 cd usr

188 ls

189 cd../

190 cd ../

191 ls

192 ./mount-hdc

193 sudo ./mount-hdc

194 cd hdc/usr/root

195 gedit whoami.c

196 cd ../ ../

197 cd ../

198 cd..

199 cd ..

200 sudo umount hdc

201 ./run

202 cd oslab/

203 cd linux-0.11/

204 cd include/

205 ls

206 gedit unistd.h

207 gedit linux/sys.h

208 cd ../kernel/

209 gedit sys_call.h

210 ls

211 gedit sys_call.s

212 gedit system_call.s

213 gedit who.c

214 gedit Makefile

215 ls

216 cd ..

217 make clean

218 ls kernel/

219 make all

220 ls kernel/

221 ls

222 cd ..

223 sudo ./mount-hdc

224 cd hdc

225 cd usr/

226 ls ./include/

227 gedit ./include/

228 gedit ./include/unistd.h

229 ls ./include/linux/

230 gedit ./include/linux/sys.h

231 gedit ./root/iam.c

232 gedit ./root/whoami.c

233 cd ../../

234 sudo umount hdc

235 ./r

236 ./run

237 cd linux-0.11/

238 gedit ./include/sys.h

239 gedit ./include/linux/sys.h

240 gedit ./include/unistd.h

241 ls

242 make clean

243 ls

244 make all

245 cd ..

246 ./run

247 history

发表评论

电子邮件地址不会被公开。 必填项已用*标注