#include "param.h"
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"
#include "syscall.h"
#include "traps.h"
#include "memlayout.h"
char buf[8192];
char name[3];
char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
int stdout = 1;
// does chdir() call iput(p->cwd) in a transaction?
void
iputtest(void)
{
printf(stdout, "iput test\n");
if(mkdir("iputdir") < 0){
printf(stdout, "mkdir failed\n");
exit();
}
if(chdir("iputdir") < 0){
printf(stdout, "chdir iputdir failed\n");
exit();
}
if(unlink("../iputdir") < 0){
printf(stdout, "unlink ../iputdir failed\n");
exit();
}
if(chdir("/") < 0){
printf(stdout, "chdir / failed\n");
exit();
}
printf(stdout, "iput test ok\n");
}
// does exit() call iput(p->cwd) in a transaction?
void
exitiputtest(void)
{
int pid;
printf(stdout, "exitiput test\n");
pid = fork();
if(pid < 0){
printf(stdout, "fork failed\n");
exit();
}
if(pid == 0){
if(mkdir("iputdir") < 0){
printf(stdout, "mkdir failed\n");
exit();
}
if(chdir("iputdir") < 0){
printf(stdout, "child chdir failed\n");
exit();
}
if(unlink("../iputdir") < 0){
printf(stdout, "unlink ../iputdir failed\n");
exit();
}
exit();
}
wait();
printf(stdout, "exitiput test ok\n");
}
// does the error path in open() for attempt to write a
// directory call iput() in a transaction?
// needs a hacked kernel that pauses just after the namei()
// call in sys_open():
// if((ip = namei(path)) == 0)
// return -1;
// {
// int i;
// for(i = 0; i < 10000; i++)
// yield();
// }
void
openiputtest(void)
{
int pid;
printf(stdout, "openiput test\n");
if(mkdir("oidir") < 0){
printf(stdout, "mkdir oidir failed\n");
exit();
}
pid = fork();
if(pid < 0){
printf(stdout, "fork failed\n");
exit();
}
if(pid == 0){
int fd = open("oidir", O_RDWR);
if(fd >= 0){
printf(stdout, "open directory for write succeeded\n");
exit();
}
exit();
}
sleep(1);
if(unlink("oidir") != 0){
printf(stdout, "unlink failed\n");
exit();
}
wait();
printf(stdout, "openiput test ok\n");
}
// 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 ok\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);
exit();
}
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
- 资源: 2
最新资源
- 2_base.apk.1
- MOUDBUS通讯程序模板数据变化后写优先
- labview编写的数据回放软件,支持多曲线回放,支持曲线缩放,支持曲线打标签,支持曲线勾选可见不可见,支持点击曲线加粗显示,支
- 电源环路的计算、环路补偿、环路仿真 一直以来,环路的计算和补偿都是开关电源领域的“难点”,很多做开关电源研发的工程师要么对环路一
- 本课程基于Abaqus,应用两种加载方式一-FluidCavity与Pressure分别介绍了气动驱动软体机器人仿真分析流程
- 基于FPGA的自适应滤波器FIR IIR滤波器LMS NLMS RLS算法 FxLMS 分数阶 本设计是在FPGA开发板上实现
- 无人船 无人艇路径跟踪控制 Matlab Simulink效果 基于Foseen船舶模型,LOS制导子系统+反步控制器制作
- comsol锂离子电池组充放电循环强制液冷散热仿真 模型为SolidWorks导入,可以提供原模型 电池模型:一维电化学(p
- 增材制造模拟,增材制造仿真 ansys workbench ,cmt, waam ,slm,电弧增材制造焊接,温度场应力场仿真
- 鸿蒙基础 (1).json
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈