Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
43572072
提交
43572072
8月 12, 2006
创建
作者:
rtm
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix getblk to actually lock the block
no more cons_put system calls usertests tests two processes writing files
上级
1f544842
显示空白字符变更
内嵌
并排
正在显示
11 个修改的文件
包含
158 行增加
和
85 行删除
+158
-85
bio.c
bio.c
+18
-11
buf.h
buf.h
+2
-0
defs.h
defs.h
+1
-1
init.c
init.c
+2
-2
sh.c
sh.c
+2
-3
syscall.c
syscall.c
+0
-53
syscall.h
syscall.h
+0
-3
ulib.c
ulib.c
+11
-0
user.h
user.h
+3
-0
usertests.c
usertests.c
+119
-9
usys.S
usys.S
+0
-3
没有找到文件。
bio.c
浏览文件 @
43572072
...
@@ -17,24 +17,31 @@ binit(void)
...
@@ -17,24 +17,31 @@ binit(void)
}
}
struct
buf
*
struct
buf
*
getblk
()
getblk
(
uint
dev
,
uint
sector
)
{
{
int
i
;
struct
buf
*
b
;
acquire
(
&
buf_table_lock
);
acquire
(
&
buf_table_lock
);
// XXX need to lock the block even if not caching, to
// avoid read modify write problems.
while
(
1
){
while
(
1
){
for
(
i
=
0
;
i
<
NBUF
;
i
++
){
for
(
b
=
buf
;
b
<
buf
+
NBUF
;
b
++
)
if
((
buf
[
i
].
flags
&
B_BUSY
)
==
0
){
if
((
b
->
flags
&
B_BUSY
)
&&
b
->
dev
==
dev
&&
b
->
sector
)
buf
[
i
].
flags
|=
B_BUSY
;
break
;
if
(
b
<
buf
+
NBUF
){
sleep
(
buf
,
&
buf_table_lock
);
}
else
{
for
(
b
=
buf
;
b
<
buf
+
NBUF
;
b
++
){
if
((
b
->
flags
&
B_BUSY
)
==
0
){
b
->
flags
|=
B_BUSY
;
b
->
dev
=
dev
;
b
->
sector
=
sector
;
release
(
&
buf_table_lock
);
release
(
&
buf_table_lock
);
return
buf
+
i
;
return
b
;
}
}
}
}
sleep
(
buf
,
&
buf_table_lock
);
panic
(
"getblk: no buffers"
);
}
}
}
}
}
...
@@ -45,7 +52,7 @@ bread(uint dev, uint sector)
...
@@ -45,7 +52,7 @@ bread(uint dev, uint sector)
struct
buf
*
b
;
struct
buf
*
b
;
extern
struct
spinlock
ide_lock
;
extern
struct
spinlock
ide_lock
;
b
=
getblk
();
b
=
getblk
(
dev
,
sector
);
acquire
(
&
ide_lock
);
acquire
(
&
ide_lock
);
c
=
ide_start_rw
(
dev
&
0xff
,
sector
,
b
->
data
,
1
,
1
);
c
=
ide_start_rw
(
dev
&
0xff
,
sector
,
b
->
data
,
1
,
1
);
...
...
buf.h
浏览文件 @
43572072
struct
buf
{
struct
buf
{
int
flags
;
int
flags
;
uint
dev
;
uint
sector
;
uchar
data
[
512
];
uchar
data
[
512
];
};
};
#define B_BUSY 0x1
#define B_BUSY 0x1
defs.h
浏览文件 @
43572072
...
@@ -104,7 +104,7 @@ int ide_finish(void *);
...
@@ -104,7 +104,7 @@ int ide_finish(void *);
// bio.c
// bio.c
void
binit
(
void
);
void
binit
(
void
);
struct
buf
;
struct
buf
;
struct
buf
*
getblk
(
void
);
struct
buf
*
getblk
(
uint
dev
,
uint
sector
);
struct
buf
*
bread
(
uint
,
uint
);
struct
buf
*
bread
(
uint
,
uint
);
void
bwrite
(
uint
,
struct
buf
*
,
uint
);
void
bwrite
(
uint
,
struct
buf
*
,
uint
);
void
brelse
(
struct
buf
*
);
void
brelse
(
struct
buf
*
);
...
...
init.c
浏览文件 @
43572072
...
@@ -17,10 +17,10 @@ main(void)
...
@@ -17,10 +17,10 @@ main(void)
open
(
"console"
,
1
);
open
(
"console"
,
1
);
open
(
"console"
,
1
);
open
(
"console"
,
1
);
write
(
1
,
"init...
\n
"
,
8
);
puts
(
"init...
\n
"
);
while
(
1
){
while
(
1
){
write
(
1
,
"running sh...
\n
"
,
14
);
puts
(
"running sh...
\n
"
);
pid
=
fork
();
pid
=
fork
();
if
(
pid
==
0
){
if
(
pid
==
0
){
exec
(
"sh"
,
sh_args
);
exec
(
"sh"
,
sh_args
);
...
...
sh.c
浏览文件 @
43572072
...
@@ -12,7 +12,7 @@ main(void)
...
@@ -12,7 +12,7 @@ main(void)
int
pid
;
int
pid
;
while
(
1
){
while
(
1
){
write
(
1
,
"$ "
,
2
);
puts
(
"$ "
);
gets
(
buf
,
sizeof
(
buf
));
gets
(
buf
,
sizeof
(
buf
));
if
(
buf
[
0
]
==
'\0'
)
if
(
buf
[
0
]
==
'\0'
)
continue
;
continue
;
...
@@ -21,8 +21,7 @@ main(void)
...
@@ -21,8 +21,7 @@ main(void)
args
[
0
]
=
buf
;
args
[
0
]
=
buf
;
args
[
1
]
=
0
;
args
[
1
]
=
0
;
exec
(
buf
,
args
);
exec
(
buf
,
args
);
write
(
1
,
buf
,
strlen
(
buf
));
printf
(
1
,
"%s: not found
\n
"
,
buf
);
write
(
1
,
": not found
\n
"
,
12
);
exit
();
exit
();
}
}
if
(
pid
>
0
)
if
(
pid
>
0
)
...
...
syscall.c
浏览文件 @
43572072
...
@@ -215,38 +215,6 @@ sys_kill(void)
...
@@ -215,38 +215,6 @@ sys_kill(void)
}
}
int
int
sys_cons_putc
(
void
)
{
int
c
;
char
buf
[
2
];
if
(
fetcharg
(
0
,
&
c
)
<
0
)
return
-
1
;
buf
[
0
]
=
c
;
buf
[
1
]
=
0
;
cprintf
(
"%s"
,
buf
);
return
0
;
}
int
sys_cons_puts
(
void
)
{
char
buf
[
256
];
int
i
;
uint
addr
;
struct
proc
*
cp
=
curproc
[
cpu
()];
if
(
fetcharg
(
0
,
&
addr
)
<
0
)
return
-
1
;
for
(
i
=
0
;
i
<
sizeof
buf
-
1
&&
fetchbyte
(
cp
,
addr
+
i
,
&
buf
[
i
])
>=
0
;
i
++
)
if
(
buf
[
i
]
==
0
)
break
;
buf
[
i
]
=
0
;
cprintf
(
"%s"
,
buf
);
return
0
;
}
int
sys_open
(
void
)
sys_open
(
void
)
{
{
struct
proc
*
cp
=
curproc
[
cpu
()];
struct
proc
*
cp
=
curproc
[
cpu
()];
...
@@ -525,18 +493,6 @@ sys_block(void)
...
@@ -525,18 +493,6 @@ sys_block(void)
return
0
;
return
0
;
}
}
int
sys_panic
(
void
)
{
struct
proc
*
p
=
curproc
[
cpu
()];
uint
addr
;
if
(
fetcharg
(
0
,
&
addr
)
<
0
)
return
-
1
;
panic
(
p
->
mem
+
addr
);
return
0
;
}
void
void
syscall
(
void
)
syscall
(
void
)
{
{
...
@@ -554,9 +510,6 @@ syscall(void)
...
@@ -554,9 +510,6 @@ syscall(void)
case
SYS_wait
:
case
SYS_wait
:
ret
=
sys_wait
();
ret
=
sys_wait
();
break
;
break
;
case
SYS_cons_putc
:
ret
=
sys_cons_putc
();
break
;
case
SYS_pipe
:
case
SYS_pipe
:
ret
=
sys_pipe
();
ret
=
sys_pipe
();
break
;
break
;
...
@@ -575,12 +528,6 @@ syscall(void)
...
@@ -575,12 +528,6 @@ syscall(void)
case
SYS_kill
:
case
SYS_kill
:
ret
=
sys_kill
();
ret
=
sys_kill
();
break
;
break
;
case
SYS_panic
:
ret
=
sys_panic
();
break
;
case
SYS_cons_puts
:
ret
=
sys_cons_puts
();
break
;
case
SYS_exec
:
case
SYS_exec
:
ret
=
sys_exec
();
ret
=
sys_exec
();
break
;
break
;
...
...
syscall.h
浏览文件 @
43572072
#define SYS_fork 1
#define SYS_fork 1
#define SYS_exit 2
#define SYS_exit 2
#define SYS_wait 3
#define SYS_wait 3
#define SYS_cons_putc 4
#define SYS_pipe 5
#define SYS_pipe 5
#define SYS_write 6
#define SYS_write 6
#define SYS_read 7
#define SYS_read 7
#define SYS_close 8
#define SYS_close 8
#define SYS_block 9
#define SYS_block 9
#define SYS_kill 10
#define SYS_kill 10
#define SYS_panic 11
#define SYS_cons_puts 12
#define SYS_exec 13
#define SYS_exec 13
#define SYS_open 14
#define SYS_open 14
#define SYS_mknod 15
#define SYS_mknod 15
...
...
ulib.c
浏览文件 @
43572072
...
@@ -26,6 +26,17 @@ strlen(char *s)
...
@@ -26,6 +26,17 @@ strlen(char *s)
return
n
;
return
n
;
}
}
void
*
memset
(
void
*
dst
,
int
c
,
unsigned
int
n
)
{
char
*
d
=
(
char
*
)
dst
;
while
(
n
--
>
0
)
*
d
++
=
c
;
return
dst
;
}
char
*
char
*
gets
(
char
*
buf
,
int
max
)
gets
(
char
*
buf
,
int
max
)
{
{
...
...
user.h
浏览文件 @
43572072
...
@@ -14,9 +14,12 @@ int exec(char *, char **);
...
@@ -14,9 +14,12 @@ int exec(char *, char **);
int
open
(
char
*
,
int
);
int
open
(
char
*
,
int
);
int
mknod
(
char
*
,
short
,
short
,
short
);
int
mknod
(
char
*
,
short
,
short
,
short
);
int
unlink
(
char
*
);
int
unlink
(
char
*
);
struct
stat
;
int
fstat
(
int
fd
,
struct
stat
*
stat
);
int
fstat
(
int
fd
,
struct
stat
*
stat
);
int
puts
(
char
*
);
int
puts
(
char
*
);
char
*
strcpy
(
char
*
,
char
*
);
char
*
strcpy
(
char
*
,
char
*
);
void
printf
(
int
fd
,
char
*
fmt
,
...);
void
printf
(
int
fd
,
char
*
fmt
,
...);
char
*
gets
(
char
*
,
int
max
);
char
*
gets
(
char
*
,
int
max
);
unsigned
int
strlen
(
char
*
);
unsigned
int
strlen
(
char
*
);
void
*
memset
(
void
*
dst
,
int
c
,
unsigned
int
n
);
usertests.c
浏览文件 @
43572072
#include "user.h"
#include "user.h"
#include "fcntl.h"
char
buf
[
2048
];
char
buf
[
2048
];
...
@@ -18,7 +19,7 @@ pipe1(void)
...
@@ -18,7 +19,7 @@ pipe1(void)
for
(
i
=
0
;
i
<
1033
;
i
++
)
for
(
i
=
0
;
i
<
1033
;
i
++
)
buf
[
i
]
=
seq
++
;
buf
[
i
]
=
seq
++
;
if
(
write
(
fds
[
1
],
buf
,
1033
)
!=
1033
){
if
(
write
(
fds
[
1
],
buf
,
1033
)
!=
1033
){
p
anic
(
"pipe1 oops 1
\n
"
);
p
rintf
(
1
,
"pipe1 oops 1
\n
"
);
exit
();
exit
();
}
}
}
}
...
@@ -30,7 +31,7 @@ pipe1(void)
...
@@ -30,7 +31,7 @@ pipe1(void)
while
((
n
=
read
(
fds
[
0
],
buf
,
cc
))
>
0
){
while
((
n
=
read
(
fds
[
0
],
buf
,
cc
))
>
0
){
for
(
i
=
0
;
i
<
n
;
i
++
){
for
(
i
=
0
;
i
<
n
;
i
++
){
if
((
buf
[
i
]
&
0xff
)
!=
(
seq
++
&
0xff
)){
if
((
buf
[
i
]
&
0xff
)
!=
(
seq
++
&
0xff
)){
p
anic
(
"pipe1 oops 2
\n
"
);
p
rintf
(
1
,
"pipe1 oops 2
\n
"
);
return
;
return
;
}
}
}
}
...
@@ -40,7 +41,7 @@ pipe1(void)
...
@@ -40,7 +41,7 @@ pipe1(void)
cc
=
sizeof
(
buf
);
cc
=
sizeof
(
buf
);
}
}
if
(
total
!=
5
*
1033
)
if
(
total
!=
5
*
1033
)
p
anic
(
"pipe1 oops 3
\n
"
);
p
rintf
(
1
,
"pipe1 oops 3
\n
"
);
close
(
fds
[
0
]);
close
(
fds
[
0
]);
wait
();
wait
();
}
}
...
@@ -69,7 +70,7 @@ preempt(void)
...
@@ -69,7 +70,7 @@ preempt(void)
if
(
pid3
==
0
){
if
(
pid3
==
0
){
close
(
pfds
[
0
]);
close
(
pfds
[
0
]);
if
(
write
(
pfds
[
1
],
"x"
,
1
)
!=
1
)
if
(
write
(
pfds
[
1
],
"x"
,
1
)
!=
1
)
p
anic
(
"preempt write error"
);
p
rintf
(
1
,
"preempt write error"
);
close
(
pfds
[
1
]);
close
(
pfds
[
1
]);
for
(;;)
for
(;;)
;
;
...
@@ -77,7 +78,7 @@ preempt(void)
...
@@ -77,7 +78,7 @@ preempt(void)
close
(
pfds
[
1
]);
close
(
pfds
[
1
]);
if
(
read
(
pfds
[
0
],
buf
,
sizeof
(
buf
))
!=
1
){
if
(
read
(
pfds
[
0
],
buf
,
sizeof
(
buf
))
!=
1
){
p
anic
(
"preempt read error"
);
p
rintf
(
1
,
"preempt read error"
);
return
;
return
;
}
}
close
(
pfds
[
0
]);
close
(
pfds
[
0
]);
...
@@ -99,12 +100,12 @@ exitwait(void)
...
@@ -99,12 +100,12 @@ exitwait(void)
for
(
i
=
0
;
i
<
100
;
i
++
){
for
(
i
=
0
;
i
<
100
;
i
++
){
pid
=
fork
();
pid
=
fork
();
if
(
pid
<
0
){
if
(
pid
<
0
){
p
anic
(
"fork failed
\n
"
);
p
rintf
(
1
,
"fork failed
\n
"
);
return
;
return
;
}
}
if
(
pid
){
if
(
pid
){
if
(
wait
()
!=
pid
){
if
(
wait
()
!=
pid
){
p
anic
(
"wait wrong pid
\n
"
);
p
rintf
(
1
,
"wait wrong pid
\n
"
);
return
;
return
;
}
}
}
else
{
}
else
{
...
@@ -114,15 +115,124 @@ exitwait(void)
...
@@ -114,15 +115,124 @@ exitwait(void)
puts
(
"exitwait ok
\n
"
);
puts
(
"exitwait ok
\n
"
);
}
}
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
void
sharedfd
()
{
int
fd
,
pid
,
i
,
n
,
nc
,
np
;
char
buf
[
10
];
unlink
(
"sharedfd"
);
fd
=
open
(
"sharedfd"
,
O_CREATE
|
O_RDWR
);
if
(
fd
<
0
){
printf
(
1
,
"usertests: cannot open sharedfd for writing"
);
return
;
}
pid
=
fork
();
memset
(
buf
,
pid
==
0
?
'c'
:
'p'
,
sizeof
(
buf
));
for
(
i
=
0
;
i
<
100
;
i
++
){
if
(
write
(
fd
,
buf
,
sizeof
(
buf
))
!=
sizeof
(
buf
)){
printf
(
1
,
"usertests: write sharedfd failed
\n
"
);
break
;
}
}
if
(
pid
==
0
)
exit
();
else
wait
();
close
(
fd
);
fd
=
open
(
"sharedfd"
,
0
);
if
(
fd
<
0
){
printf
(
1
,
"usertests: 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
);
if
(
nc
==
1000
&&
np
==
1000
)
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
()
{
int
fd
,
pid
,
i
,
j
,
n
,
total
;
char
*
fname
;
unlink
(
"f1"
);
unlink
(
"f2"
);
pid
=
fork
();
if
(
pid
<
0
){
puts
(
"fork failed
\n
"
);
return
;
}
fname
=
pid
?
"f1"
:
"f2"
;
fd
=
open
(
fname
,
O_CREATE
|
O_RDWR
);
if
(
fd
<
0
){
puts
(
"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
();
}
}
close
(
fd
);
if
(
pid
)
wait
();
else
exit
();
for
(
i
=
0
;
i
<
2
;
i
++
){
fd
=
open
(
i
?
"f1"
:
"f2"
,
0
);
total
=
0
;
while
((
n
=
read
(
fd
,
buf
,
sizeof
(
buf
)))
>
0
){
for
(
j
=
0
;
j
<
n
;
j
++
){
if
(
buf
[
j
]
!=
(
i
?
'p'
:
'c'
)){
puts
(
"wrong char
\n
"
);
exit
();
}
}
total
+=
n
;
}
close
(
fd
);
if
(
total
!=
12
*
500
){
printf
(
1
,
"wrong length %d
\n
"
,
total
);
exit
();
}
}
puts
(
"twofiles ok
\n
"
);
}
int
int
main
(
int
argc
,
char
*
argv
[])
main
(
int
argc
,
char
*
argv
[])
{
{
puts
(
"usertests starting
\n
"
);
puts
(
"usertests starting
\n
"
);
twofiles
();
sharedfd
();
pipe1
();
pipe1
();
preempt
();
preempt
();
exitwait
();
exitwait
();
p
anic
(
"usertests succeeded
"
);
p
uts
(
"usertests finished
\n
"
);
return
0
;
exit
()
;
}
}
usys.S
浏览文件 @
43572072
...
@@ -11,15 +11,12 @@
...
@@ -11,15 +11,12 @@
STUB(fork)
STUB(fork)
STUB(exit)
STUB(exit)
STUB(wait)
STUB(wait)
STUB(cons_putc)
STUB(pipe)
STUB(pipe)
STUB(read)
STUB(read)
STUB(write)
STUB(write)
STUB(close)
STUB(close)
STUB(block)
STUB(block)
STUB(kill)
STUB(kill)
STUB(panic)
STUB(cons_puts)
STUB(exec)
STUB(exec)
STUB(open)
STUB(open)
STUB(mknod)
STUB(mknod)
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论