#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
#include "traps.h"
char buf[2048];
char name[3];
char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
int stdout = 1;
// simple file system tests
void
opentest(void)
{
int fd;
printf(stdout, "open test\n");
fd = open("echo", 0);
if(fd < 0){
printf(stdout, "open echo failed!\n");
exit();
}
close(fd);
fd = open("doesnotexist", 0);
if(fd >= 0){
printf(stdout, "open doesnotexist succeeded!\n");
exit();
}
printf(stdout, "open test ok\n");
}
void
writetest(void)
{
int fd;
int i;
printf(stdout, "small file test\n");
fd = open("small", O_CREATE|O_RDWR);
if(fd >= 0){
printf(stdout, "creat small succeeded; ok\n");
} else {
printf(stdout, "error: creat small failed!\n");
exit();
}
for(i = 0; i < 100; i++){
if(write(fd, "aaaaaaaaaa", 10) != 10){
printf(stdout, "error: write aa %d new file failed\n", i);
exit();
}
if(write(fd, "bbbbbbbbbb", 10) != 10){
printf(stdout, "error: write bb %d new file failed\n", i);
exit();
}
}
printf(stdout, "writes ok\n");
close(fd);
fd = open("small", O_RDONLY);
if(fd >= 0){
printf(stdout, "open small succeeded ok\n");
} else {
printf(stdout, "error: open small failed!\n");
exit();
}
i = read(fd, buf, 2000);
if(i == 2000){
printf(stdout, "read succeeded ok\n");
} else {
printf(stdout, "read failed\n");
exit();
}
close(fd);
if(unlink("small") < 0){
printf(stdout, "unlink small failed\n");
exit();
}
printf(stdout, "small file test ok\n");
}
void
writetest1(void)
{
int i, fd, n;
printf(stdout, "big files test\n");
fd = open("big", O_CREATE|O_RDWR);
if(fd < 0){
printf(stdout, "error: creat big failed!\n");
exit();
}
for(i = 0; i < MAXFILE; i++){
((int*)buf)[0] = i;
if(write(fd, buf, 512) != 512){
printf(stdout, "error: write big file failed\n", i);
exit();
}
}
close(fd);
fd = open("big", O_RDONLY);
if(fd < 0){
printf(stdout, "error: open big failed!\n");
exit();
}
n = 0;
for(;;){
i = read(fd, buf, 512);
if(i == 0){
if(n == MAXFILE - 1){
printf(stdout, "read only %d blocks from big", n);
exit();
}
break;
} else if(i != 512){
printf(stdout, "read failed %d\n", i);
exit();
}
if(((int*)buf)[0] != n){
printf(stdout, "read content of block %d is %d\n",
n, ((int*)buf)[0]);
exit();
}
n++;
}
close(fd);
if(unlink("big") < 0){
printf(stdout, "unlink big failed\n");
exit();
}
printf(stdout, "big files ok\n");
}
void
createtest(void)
{
int i, fd;
printf(stdout, "many creates, followed by unlink test\n");
name[0] = 'a';
name[2] = '\0';
for(i = 0; i < 52; i++){
name[1] = '0' + i;
fd = open(name, O_CREATE|O_RDWR);
close(fd);
}
name[0] = 'a';
name[2] = '\0';
for(i = 0; i < 52; i++){
name[1] = '0' + i;
unlink(name);
}
printf(stdout, "many creates, followed by unlink; ok\n");
}
void dirtest(void)
{
printf(stdout, "mkdir test\n");
if(mkdir("dir0") < 0){
printf(stdout, "mkdir failed\n");
exit();
}
if(chdir("dir0") < 0){
printf(stdout, "chdir dir0 failed\n");
exit();
}
if(chdir("..") < 0){
printf(stdout, "chdir .. failed\n");
exit();
}
if(unlink("dir0") < 0){
printf(stdout, "unlink dir0 failed\n");
exit();
}
printf(stdout, "mkdir test\n");
}
void
exectest(void)
{
printf(stdout, "exec test\n");
if(exec("echo", echoargv) < 0){
printf(stdout, "exec echo failed\n");
exit();
}
}
// simple fork and pipe read/write
void
pipe1(void)
{
int fds[2], pid;
int seq, i, n, cc, total;
if(pipe(fds) != 0){
printf(1, "pipe() failed\n");
exit();
}
pid = fork();
seq = 0;
if(pid == 0){
close(fds[0]);
for(n = 0; n < 5; n++){
for(i = 0; i < 1033; i++)
buf[i] = seq++;
if(write(fds[1], buf, 1033) != 1033){
printf(1, "pipe1 oops 1\n");
exit();
}
}
exit();
} else if(pid > 0){
close(fds[1]);
total = 0;
cc = 1;
while((n = read(fds[0], buf, cc)) > 0){
for(i = 0; i < n; i++){
if((buf[i] & 0xff) != (seq++ & 0xff)){
printf(1, "pipe1 oops 2\n");
return;
}
}
total += n;
cc = cc * 2;
if(cc > sizeof(buf))
cc = sizeof(buf);
}
if(total != 5 * 1033)
printf(1, "pipe1 oops 3 total %d\n", total);
close(fds[0]);
wait();
} else {
printf(1, "fork() failed\n");
exit();
}
printf(1, "pipe1 ok\n");
}
// meant to be run w/ at most two CPUs
void
preempt(void)
{
int pid1, pid2, pid3;
int pfds[2];
printf(1, "preempt: ");
pid1 = fork();
if(pid1 == 0)
for(;;)
;
pid2 = fork();
if(pid2 == 0)
for(;;)
;
pipe(pfds);
pid3 = fork();
if(pid3 == 0){
close(pfds[0]);
if(write(pfds[1], "x", 1) != 1)
printf(1, "preempt write error");
close(pfds[1]);
for(;;)
;
}
close(pfds[1]);
if(read(pfds[0], buf, sizeof(buf)) != 1){
printf(1, "preempt read error");
return;
}
close(pfds[0]);
printf(1, "kill... ");
kill(pid1);
kill(pid2);
kill(pid3);
printf(1, "wait... ");
wait();
wait();
wait();
printf(1, "preempt ok\n");
}
// try to find any races between exit and wait
void
exitwait(void)
{
int i, pid;
for(i = 0; i < 100; i++){
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
return;
}
if(pid){
if(wait() != pid){
printf(1, "wait wrong pid\n");
return;
}
} else {
exit();
}
}
printf(1, "exitwait ok\n");
}
void
mem(void)
{
void *m1, *m2;
int pid, ppid;
printf(1, "mem test\n");
ppid = getpid();
if((pid = fork()) == 0){
m1 = 0;
while((m2 = malloc(10001)) != 0){
*(char**)m2 = m1;
m1 = m2;
}
while(m1){
m2 = *(char**)m1;
free(m1);
m1 = m2;
}
m1 = malloc(1024*20);
if(m1 == 0){
printf(1, "couldn't allocate mem?!!\n");
kill(ppid);
exit();
}
free(m1);
printf(1, "mem ok\n");
exit();
} else {
wait();
}
}
// More file system tests
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
void
sharedfd(void)
{
int fd, pid, i, n, nc, np;
char buf[10];
unlink("sharedfd");
fd = open("sharedfd", O_CREATE|O_RDWR);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for writing");
return;
}
pid = fork();
memset(buf, pid==0?'c':'p', sizeof(buf));
for(i = 0; i < 1000; i++){
if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
printf(1, "fstests: write sharedfd failed\n");
break;
}
}
if(pid == 0)
exit();
else
wait();
close(fd);
fd = open("sharedfd", 0);
if(fd < 0){
printf(1, "fstests: cannot open sharedfd for reading\n");
return;
}
nc = np = 0;
while((n = read(fd, buf, sizeof(buf))) > 0){
for(i = 0; i < sizeof(buf); i++){
if(buf[i] == 'c')
nc++;
if(buf[i] == 'p')
np++;
}
}
close(fd);
unlink("sharedfd");
if(nc == 10000 && np == 10000)
printf(1, "sharedfd ok\n");
else
printf(1, "sharedfd oops %d %d\n", nc, np);
}
// two processes write two different files at the same
// time, to test block allocation.
void
twofiles(void)
{
int fd, pid, i, j, n, total;
char *fname;
printf(1, "twofiles test\n");
unlink("f1");
unlink("f2");
pid = fork();
if(pid < 0){
printf(1, "fork failed\n");
return;
}
fname = pid ? "f1" : "f2";
fd = open(fname, O_CREATE | O_RDWR);
if(fd < 0){
printf(1, "create failed\n");
exit();
}
memset(buf, pid?'p':'c', 512);
for(i = 0; i < 12; i++){
if((n = write(fd, buf, 500)) != 500){
printf(1, "write failed %d\n", n);
exit();
没有合适的资源?快使用搜索试试~ 我知道了~
Xv6源码包 xv6-rev5.tar.gz
5星 · 超过95%的资源 需积分: 42 270 下载量 32 浏览量
2011-10-22
13:46:49
上传
评论 2
收藏 78KB GZ 举报
温馨提示
共85个文件
c:44个
h:19个
s:8个
Xv6源码包 xv6-rev5.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
xv6-rev5.tar.gz (85个子文件)
xv6
elf.h 752B
rm.c 322B
pipe.c 2KB
sh.c 8KB
main.c 3KB
timer.c 938B
cat.c 513B
mp.h 2KB
bio.c 3KB
runoff.list 574B
param.h 674B
bootmain.c 2KB
traps.h 2KB
lapic.c 5KB
forktest.c 766B
picirq.c 2KB
trap.c 2KB
mkdir.c 327B
file.h 736B
.gdbinit.tmpl 682B
sysfile.c 7KB
sysproc.c 1KB
dot-bochsrc 33KB
file.c 2KB
kbd.h 3KB
swtch.S 461B
exec.c 2KB
defs.h 5KB
fs.h 1KB
spinlock.h 317B
fcntl.h 96B
sign.pl 363B
runoff1 2KB
wc.c 821B
string.c 1KB
init.c 649B
usertests.c 28KB
umalloc.c 2KB
kill.c 232B
user.h 828B
x86.h 3KB
echo.c 198B
ln.c 264B
fs.c 13KB
bootasm.S 3KB
spinlock.c 2KB
syscall.c 3KB
grep.c 2KB
stat.h 300B
proc.c 9KB
vectors.pl 989B
data.S 796B
syscall.h 485B
stressfs.c 747B
pr.pl 581B
ide.c 3KB
proc.h 3KB
runoff 5KB
buf.h 340B
kbd.c 925B
bootother.S 2KB
toc.hdr 290B
README 2KB
trapasm.S 662B
runoff.spec 1B
kalloc.c 1KB
vm.c 9KB
printf.c 1KB
mkfs.c 6KB
console.c 5KB
mmu.h 8KB
ioapic.c 2KB
usys.S 461B
mp.c 3KB
gdbutil 6KB
Makefile 6KB
types.h 110B
asm.h 936B
ulib.c 1KB
toc.ftr 512B
multiboot.S 2KB
zombie.c 215B
uart.c 1KB
initcode.S 422B
ls.c 2KB
共 85 条
- 1
资源评论
- vicky1233212014-10-29不错,很实用
- Yu-rmyfs2014-11-07对我的实验很有帮助
- olivewy2014-12-25谢谢楼主源码!
- matchchen19802015-04-10比耶鲁的rev4版本多一些。对应的文档也更易读。
- baidu_302081952016-03-01很详细!实用,谢谢分享!
zysyyz
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功