Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
8d618cab
提交
8d618cab
8月 28, 2014
创建
作者:
Robert Morris
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
git+ssh://amsterdam.csail.mit.edu/home/am0/6.828/xv6
上级
12eeefc7
2b2c1971
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
215 行增加
和
163 行删除
+215
-163
bio.c
bio.c
+2
-0
defs.h
defs.h
+2
-2
exec.c
exec.c
+4
-4
file.c
file.c
+4
-4
log.c
log.c
+84
-33
mkfs.c
mkfs.c
+1
-1
param.h
param.h
+3
-2
proc.c
proc.c
+2
-2
sysfile.c
sysfile.c
+25
-25
usertests.c
usertests.c
+88
-90
没有找到文件。
bio.c
浏览文件 @
8d618cab
...
@@ -80,6 +80,8 @@ bget(uint dev, uint sector)
...
@@ -80,6 +80,8 @@ bget(uint dev, uint sector)
}
}
// Not cached; recycle some non-busy and clean buffer.
// Not cached; recycle some non-busy and clean buffer.
// "clean" because B_DIRTY and !B_BUSY means log.c
// hasn't yet committed the changes to the buffer.
for
(
b
=
bcache
.
head
.
prev
;
b
!=
&
bcache
.
head
;
b
=
b
->
prev
){
for
(
b
=
bcache
.
head
.
prev
;
b
!=
&
bcache
.
head
;
b
=
b
->
prev
){
if
((
b
->
flags
&
B_BUSY
)
==
0
&&
(
b
->
flags
&
B_DIRTY
)
==
0
){
if
((
b
->
flags
&
B_BUSY
)
==
0
&&
(
b
->
flags
&
B_DIRTY
)
==
0
){
b
->
dev
=
dev
;
b
->
dev
=
dev
;
...
...
defs.h
浏览文件 @
8d618cab
...
@@ -81,8 +81,8 @@ void microdelay(int);
...
@@ -81,8 +81,8 @@ void microdelay(int);
// log.c
// log.c
void
initlog
(
void
);
void
initlog
(
void
);
void
log_write
(
struct
buf
*
);
void
log_write
(
struct
buf
*
);
void
begin_
trans
();
void
begin_
op
();
void
commit_trans
();
void
end_op
();
// mp.c
// mp.c
extern
int
ismp
;
extern
int
ismp
;
...
...
exec.c
浏览文件 @
8d618cab
...
@@ -18,9 +18,9 @@ exec(char *path, char **argv)
...
@@ -18,9 +18,9 @@ exec(char *path, char **argv)
struct
proghdr
ph
;
struct
proghdr
ph
;
pde_t
*
pgdir
,
*
oldpgdir
;
pde_t
*
pgdir
,
*
oldpgdir
;
begin_
trans
();
begin_
op
();
if
((
ip
=
namei
(
path
))
==
0
){
if
((
ip
=
namei
(
path
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
ilock
(
ip
);
ilock
(
ip
);
...
@@ -50,7 +50,7 @@ exec(char *path, char **argv)
...
@@ -50,7 +50,7 @@ exec(char *path, char **argv)
goto
bad
;
goto
bad
;
}
}
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
ip
=
0
;
ip
=
0
;
// Allocate two pages at the next page boundary.
// Allocate two pages at the next page boundary.
...
@@ -101,7 +101,7 @@ exec(char *path, char **argv)
...
@@ -101,7 +101,7 @@ exec(char *path, char **argv)
freevm
(
pgdir
);
freevm
(
pgdir
);
if
(
ip
){
if
(
ip
){
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
}
}
return
-
1
;
return
-
1
;
}
}
file.c
浏览文件 @
8d618cab
...
@@ -72,9 +72,9 @@ fileclose(struct file *f)
...
@@ -72,9 +72,9 @@ fileclose(struct file *f)
if
(
ff
.
type
==
FD_PIPE
)
if
(
ff
.
type
==
FD_PIPE
)
pipeclose
(
ff
.
pipe
,
ff
.
writable
);
pipeclose
(
ff
.
pipe
,
ff
.
writable
);
else
if
(
ff
.
type
==
FD_INODE
){
else
if
(
ff
.
type
==
FD_INODE
){
begin_
trans
();
begin_
op
();
iput
(
ff
.
ip
);
iput
(
ff
.
ip
);
commit_trans
();
end_op
();
}
}
}
}
...
@@ -136,12 +136,12 @@ filewrite(struct file *f, char *addr, int n)
...
@@ -136,12 +136,12 @@ filewrite(struct file *f, char *addr, int n)
if
(
n1
>
max
)
if
(
n1
>
max
)
n1
=
max
;
n1
=
max
;
begin_
trans
();
begin_
op
();
ilock
(
f
->
ip
);
ilock
(
f
->
ip
);
if
((
r
=
writei
(
f
->
ip
,
addr
+
i
,
f
->
off
,
n1
))
>
0
)
if
((
r
=
writei
(
f
->
ip
,
addr
+
i
,
f
->
off
,
n1
))
>
0
)
f
->
off
+=
r
;
f
->
off
+=
r
;
iunlock
(
f
->
ip
);
iunlock
(
f
->
ip
);
commit_trans
();
end_op
();
if
(
r
<
0
)
if
(
r
<
0
)
break
;
break
;
...
...
log.c
浏览文件 @
8d618cab
...
@@ -5,18 +5,19 @@
...
@@ -5,18 +5,19 @@
#include "fs.h"
#include "fs.h"
#include "buf.h"
#include "buf.h"
// Simple logging. Each file system system call
// Simple logging that allows concurrent FS system calls.
// should be surrounded with begin_trans() and commit_trans() calls.
//
//
// The log holds at most one transaction at a time. Commit forces
// A log transaction contains the updates of multiple FS system
// the log (with commit record) to disk, then installs the affected
// calls. The logging system only commits when there are
// blocks to disk, then erases the log. begin_trans() ensures that
// no FS system calls active. Thus there is never
// only one system call can be in a transaction; others must wait.
// any reasoning required about whether a commit might
//
// write an uncommitted system call's updates to disk.
// Allowing only one transaction at a time means that the file
//
// system code doesn't have to worry about the possibility of
// A system call should call begin_op()/end_op() to mark
// one transaction reading a block that another one has modified,
// its start and end. Usually begin_op() just increments
// for example an i-node block.
// the count of in-progress FS system calls and returns.
// But if it thinks the log is close to running out, it
// sleeps until the last outstanding end_op() commits.
//
//
// The log is a physical re-do log containing disk blocks.
// The log is a physical re-do log containing disk blocks.
// The on-disk log format:
// The on-disk log format:
...
@@ -38,13 +39,15 @@ struct log {
...
@@ -38,13 +39,15 @@ struct log {
struct
spinlock
lock
;
struct
spinlock
lock
;
int
start
;
int
start
;
int
size
;
int
size
;
int
busy
;
// a transaction is active
int
outstanding
;
// how many FS sys calls are executing.
int
committing
;
// in commit(), please wait.
int
dev
;
int
dev
;
struct
logheader
lh
;
struct
logheader
lh
;
};
};
struct
log
log
;
struct
log
log
;
static
void
recover_from_log
(
void
);
static
void
recover_from_log
(
void
);
static
void
commit
();
void
void
initlog
(
void
)
initlog
(
void
)
...
@@ -117,36 +120,88 @@ recover_from_log(void)
...
@@ -117,36 +120,88 @@ recover_from_log(void)
write_head
();
// clear the log
write_head
();
// clear the log
}
}
// called at the start of each FS system call.
void
void
begin_
trans
(
void
)
begin_
op
(
void
)
{
{
acquire
(
&
log
.
lock
);
acquire
(
&
log
.
lock
);
while
(
log
.
busy
)
{
while
(
1
){
sleep
(
&
log
,
&
log
.
lock
);
if
(
log
.
committing
){
sleep
(
&
log
,
&
log
.
lock
);
}
else
if
(
log
.
lh
.
n
+
(
log
.
outstanding
+
1
)
*
MAXOPBLOCKS
>
LOGSIZE
){
// this op might exhaust log space; wait for commit.
sleep
(
&
log
,
&
log
.
lock
);
}
else
{
log
.
outstanding
+=
1
;
release
(
&
log
.
lock
);
break
;
}
}
}
log
.
busy
=
1
;
release
(
&
log
.
lock
);
}
}
// called at the end of each FS system call.
// commits if this was the last outstanding operation.
void
void
commit_trans
(
void
)
end_op
(
void
)
{
int
do_commit
=
0
;
acquire
(
&
log
.
lock
);
log
.
outstanding
-=
1
;
if
(
log
.
committing
)
panic
(
"log.committing"
);
if
(
log
.
outstanding
==
0
){
do_commit
=
1
;
log
.
committing
=
1
;
}
else
{
// begin_op() may be waiting for log space.
wakeup
(
&
log
);
}
release
(
&
log
.
lock
);
if
(
do_commit
){
// call commit w/o holding locks, since not allowed
// to sleep with locks.
commit
();
acquire
(
&
log
.
lock
);
log
.
committing
=
0
;
wakeup
(
&
log
);
release
(
&
log
.
lock
);
}
}
// Copy modified blocks from cache to log.
static
void
write_log
(
void
)
{
int
tail
;
for
(
tail
=
0
;
tail
<
log
.
lh
.
n
;
tail
++
)
{
struct
buf
*
to
=
bread
(
log
.
dev
,
log
.
start
+
tail
+
1
);
// log block
struct
buf
*
from
=
bread
(
log
.
dev
,
log
.
lh
.
sector
[
tail
]);
// cache block
memmove
(
to
->
data
,
from
->
data
,
BSIZE
);
bwrite
(
to
);
// write the log
brelse
(
from
);
brelse
(
to
);
}
}
static
void
commit
()
{
{
if
(
log
.
lh
.
n
>
0
)
{
if
(
log
.
lh
.
n
>
0
)
{
write_log
();
// Write modified blocks from cache to log
write_head
();
// Write header to disk -- the real commit
write_head
();
// Write header to disk -- the real commit
install_trans
();
// Now install writes to home locations
install_trans
();
// Now install writes to home locations
log
.
lh
.
n
=
0
;
log
.
lh
.
n
=
0
;
write_head
();
// Erase the transaction from the log
write_head
();
// Erase the transaction from the log
}
}
acquire
(
&
log
.
lock
);
log
.
busy
=
0
;
wakeup
(
&
log
);
release
(
&
log
.
lock
);
}
}
// Caller has modified b->data and is done with the buffer.
// Caller has modified b->data and is done with the buffer.
// Append the block to the log and record the block number,
// Record the block number and pin in the cache with B_DIRTY.
// but don't write the log header (which would commit the write).
// commit()/write_log() will do the disk write.
//
// log_write() replaces bwrite(); a typical use is:
// log_write() replaces bwrite(); a typical use is:
// bp = bread(...)
// bp = bread(...)
// modify bp->data[]
// modify bp->data[]
...
@@ -159,21 +214,17 @@ log_write(struct buf *b)
...
@@ -159,21 +214,17 @@ log_write(struct buf *b)
if
(
log
.
lh
.
n
>=
LOGSIZE
||
log
.
lh
.
n
>=
log
.
size
-
1
)
if
(
log
.
lh
.
n
>=
LOGSIZE
||
log
.
lh
.
n
>=
log
.
size
-
1
)
panic
(
"too big a transaction"
);
panic
(
"too big a transaction"
);
if
(
!
log
.
busy
)
if
(
log
.
outstanding
<
1
)
panic
(
"write outside of trans"
);
panic
(
"
log_
write outside of trans"
);
for
(
i
=
0
;
i
<
log
.
lh
.
n
;
i
++
)
{
for
(
i
=
0
;
i
<
log
.
lh
.
n
;
i
++
)
{
if
(
log
.
lh
.
sector
[
i
]
==
b
->
sector
)
// log absorbtion
?
if
(
log
.
lh
.
sector
[
i
]
==
b
->
sector
)
// log absorbtion
break
;
break
;
}
}
log
.
lh
.
sector
[
i
]
=
b
->
sector
;
log
.
lh
.
sector
[
i
]
=
b
->
sector
;
struct
buf
*
lbuf
=
bread
(
b
->
dev
,
log
.
start
+
i
+
1
);
memmove
(
lbuf
->
data
,
b
->
data
,
BSIZE
);
bwrite
(
lbuf
);
brelse
(
lbuf
);
if
(
i
==
log
.
lh
.
n
)
if
(
i
==
log
.
lh
.
n
)
log
.
lh
.
n
++
;
log
.
lh
.
n
++
;
b
->
flags
|=
B_DIRTY
;
//
XXX
prevent eviction
b
->
flags
|=
B_DIRTY
;
// prevent eviction
}
}
//PAGEBREAK!
//PAGEBREAK!
...
...
mkfs.c
浏览文件 @
8d618cab
...
@@ -13,7 +13,7 @@
...
@@ -13,7 +13,7 @@
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
int
nblocks
=
985
;
int
nblocks
=
(
995
-
LOGSIZE
)
;
int
nlog
=
LOGSIZE
;
int
nlog
=
LOGSIZE
;
int
ninodes
=
200
;
int
ninodes
=
200
;
int
size
=
1024
;
int
size
=
1024
;
...
...
param.h
浏览文件 @
8d618cab
...
@@ -3,10 +3,11 @@
...
@@ -3,10 +3,11 @@
#define NCPU 8 // maximum number of CPUs
#define NCPU 8 // maximum number of CPUs
#define NOFILE 16 // open files per process
#define NOFILE 16 // open files per process
#define NFILE 100 // open files per system
#define NFILE 100 // open files per system
#define NBUF 10 // size of disk block cache
#define NINODE 50 // maximum number of active i-nodes
#define NINODE 50 // maximum number of active i-nodes
#define NDEV 10 // maximum major device number
#define NDEV 10 // maximum major device number
#define ROOTDEV 1 // device number of file system root disk
#define ROOTDEV 1 // device number of file system root disk
#define MAXARG 32 // max exec arguments
#define MAXARG 32 // max exec arguments
#define LOGSIZE 10 // max data sectors in on-disk log
#define MAXOPBLOCKS 10 // max # of blocks any FS op writes
#define LOGSIZE (MAXOPBLOCKS*3) // max data sectors in on-disk log
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache (>= LOGSIZE)
proc.c
浏览文件 @
8d618cab
...
@@ -186,9 +186,9 @@ exit(void)
...
@@ -186,9 +186,9 @@ exit(void)
}
}
}
}
begin_
trans
();
begin_
op
();
iput
(
proc
->
cwd
);
iput
(
proc
->
cwd
);
commit_trans
();
end_op
();
proc
->
cwd
=
0
;
proc
->
cwd
=
0
;
acquire
(
&
ptable
.
lock
);
acquire
(
&
ptable
.
lock
);
...
...
sysfile.c
浏览文件 @
8d618cab
...
@@ -121,16 +121,16 @@ sys_link(void)
...
@@ -121,16 +121,16 @@ sys_link(void)
if
(
argstr
(
0
,
&
old
)
<
0
||
argstr
(
1
,
&
new
)
<
0
)
if
(
argstr
(
0
,
&
old
)
<
0
||
argstr
(
1
,
&
new
)
<
0
)
return
-
1
;
return
-
1
;
begin_
trans
();
begin_
op
();
if
((
ip
=
namei
(
old
))
==
0
){
if
((
ip
=
namei
(
old
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
ilock
(
ip
);
ilock
(
ip
);
if
(
ip
->
type
==
T_DIR
){
if
(
ip
->
type
==
T_DIR
){
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
...
@@ -148,7 +148,7 @@ sys_link(void)
...
@@ -148,7 +148,7 @@ sys_link(void)
iunlockput
(
dp
);
iunlockput
(
dp
);
iput
(
ip
);
iput
(
ip
);
commit_trans
();
end_op
();
return
0
;
return
0
;
...
@@ -157,7 +157,7 @@ bad:
...
@@ -157,7 +157,7 @@ bad:
ip
->
nlink
--
;
ip
->
nlink
--
;
iupdate
(
ip
);
iupdate
(
ip
);
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
...
@@ -189,9 +189,9 @@ sys_unlink(void)
...
@@ -189,9 +189,9 @@ sys_unlink(void)
if
(
argstr
(
0
,
&
path
)
<
0
)
if
(
argstr
(
0
,
&
path
)
<
0
)
return
-
1
;
return
-
1
;
begin_
trans
();
begin_
op
();
if
((
dp
=
nameiparent
(
path
,
name
))
==
0
){
if
((
dp
=
nameiparent
(
path
,
name
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
...
@@ -225,13 +225,13 @@ sys_unlink(void)
...
@@ -225,13 +225,13 @@ sys_unlink(void)
iupdate
(
ip
);
iupdate
(
ip
);
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
0
;
return
0
;
bad:
bad:
iunlockput
(
dp
);
iunlockput
(
dp
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
...
@@ -291,23 +291,23 @@ sys_open(void)
...
@@ -291,23 +291,23 @@ sys_open(void)
if
(
argstr
(
0
,
&
path
)
<
0
||
argint
(
1
,
&
omode
)
<
0
)
if
(
argstr
(
0
,
&
path
)
<
0
||
argint
(
1
,
&
omode
)
<
0
)
return
-
1
;
return
-
1
;
begin_
trans
();
begin_
op
();
if
(
omode
&
O_CREATE
){
if
(
omode
&
O_CREATE
){
ip
=
create
(
path
,
T_FILE
,
0
,
0
);
ip
=
create
(
path
,
T_FILE
,
0
,
0
);
if
(
ip
==
0
){
if
(
ip
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
}
else
{
}
else
{
if
((
ip
=
namei
(
path
))
==
0
){
if
((
ip
=
namei
(
path
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
ilock
(
ip
);
ilock
(
ip
);
if
(
ip
->
type
==
T_DIR
&&
omode
!=
O_RDONLY
){
if
(
ip
->
type
==
T_DIR
&&
omode
!=
O_RDONLY
){
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
}
}
...
@@ -316,11 +316,11 @@ sys_open(void)
...
@@ -316,11 +316,11 @@ sys_open(void)
if
(
f
)
if
(
f
)
fileclose
(
f
);
fileclose
(
f
);
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
iunlock
(
ip
);
iunlock
(
ip
);
commit_trans
();
end_op
();
f
->
type
=
FD_INODE
;
f
->
type
=
FD_INODE
;
f
->
ip
=
ip
;
f
->
ip
=
ip
;
...
@@ -336,13 +336,13 @@ sys_mkdir(void)
...
@@ -336,13 +336,13 @@ sys_mkdir(void)
char
*
path
;
char
*
path
;
struct
inode
*
ip
;
struct
inode
*
ip
;
begin_
trans
();
begin_
op
();
if
(
argstr
(
0
,
&
path
)
<
0
||
(
ip
=
create
(
path
,
T_DIR
,
0
,
0
))
==
0
){
if
(
argstr
(
0
,
&
path
)
<
0
||
(
ip
=
create
(
path
,
T_DIR
,
0
,
0
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
0
;
return
0
;
}
}
...
@@ -354,16 +354,16 @@ sys_mknod(void)
...
@@ -354,16 +354,16 @@ sys_mknod(void)
int
len
;
int
len
;
int
major
,
minor
;
int
major
,
minor
;
begin_
trans
();
begin_
op
();
if
((
len
=
argstr
(
0
,
&
path
))
<
0
||
if
((
len
=
argstr
(
0
,
&
path
))
<
0
||
argint
(
1
,
&
major
)
<
0
||
argint
(
1
,
&
major
)
<
0
||
argint
(
2
,
&
minor
)
<
0
||
argint
(
2
,
&
minor
)
<
0
||
(
ip
=
create
(
path
,
T_DEV
,
major
,
minor
))
==
0
){
(
ip
=
create
(
path
,
T_DEV
,
major
,
minor
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
0
;
return
0
;
}
}
...
@@ -373,20 +373,20 @@ sys_chdir(void)
...
@@ -373,20 +373,20 @@ sys_chdir(void)
char
*
path
;
char
*
path
;
struct
inode
*
ip
;
struct
inode
*
ip
;
begin_
trans
();
begin_
op
();
if
(
argstr
(
0
,
&
path
)
<
0
||
(
ip
=
namei
(
path
))
==
0
){
if
(
argstr
(
0
,
&
path
)
<
0
||
(
ip
=
namei
(
path
))
==
0
){
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
ilock
(
ip
);
ilock
(
ip
);
if
(
ip
->
type
!=
T_DIR
){
if
(
ip
->
type
!=
T_DIR
){
iunlockput
(
ip
);
iunlockput
(
ip
);
commit_trans
();
end_op
();
return
-
1
;
return
-
1
;
}
}
iunlock
(
ip
);
iunlock
(
ip
);
iput
(
proc
->
cwd
);
iput
(
proc
->
cwd
);
commit_trans
();
end_op
();
proc
->
cwd
=
ip
;
proc
->
cwd
=
ip
;
return
0
;
return
0
;
}
}
...
...
usertests.c
浏览文件 @
8d618cab
...
@@ -512,51 +512,56 @@ sharedfd(void)
...
@@ -512,51 +512,56 @@ sharedfd(void)
}
}
}
}
//
two processes write two
different files at the same
//
four processes write
different files at the same
// time, to test block allocation.
// time, to test block allocation.
void
void
two
files
(
void
)
four
files
(
void
)
{
{
int
fd
,
pid
,
i
,
j
,
n
,
total
;
int
fd
,
pid
,
i
,
j
,
n
,
total
,
pi
;
char
*
names
[]
=
{
"f0"
,
"f1"
,
"f2"
,
"f3"
};
char
*
fname
;
char
*
fname
;
printf
(
1
,
"
two
files test
\n
"
);
printf
(
1
,
"
four
files test
\n
"
);
unlink
(
"f1"
);
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
unlink
(
"f2"
);
fname
=
names
[
pi
];
unlink
(
fname
);
pid
=
fork
();
pid
=
fork
();
if
(
pid
<
0
){
if
(
pid
<
0
){
printf
(
1
,
"fork failed
\n
"
);
printf
(
1
,
"fork failed
\n
"
);
exit
();
exit
();
}
}
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
);
if
(
pid
==
0
){
for
(
i
=
0
;
i
<
12
;
i
++
){
fd
=
open
(
fname
,
O_CREATE
|
O_RDWR
);
if
((
n
=
write
(
fd
,
buf
,
500
))
!=
500
){
if
(
fd
<
0
){
printf
(
1
,
"write failed %d
\n
"
,
n
);
printf
(
1
,
"create failed
\n
"
);
exit
();
}
memset
(
buf
,
'0'
+
pi
,
512
);
for
(
i
=
0
;
i
<
12
;
i
++
){
if
((
n
=
write
(
fd
,
buf
,
500
))
!=
500
){
printf
(
1
,
"write failed %d
\n
"
,
n
);
exit
();
}
}
exit
();
exit
();
}
}
}
}
close
(
fd
);
if
(
pid
)
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
wait
();
wait
();
else
}
exit
();
for
(
i
=
0
;
i
<
2
;
i
++
){
for
(
i
=
0
;
i
<
2
;
i
++
){
fd
=
open
(
i
?
"f1"
:
"f2"
,
0
);
fname
=
names
[
i
];
fd
=
open
(
fname
,
0
);
total
=
0
;
total
=
0
;
while
((
n
=
read
(
fd
,
buf
,
sizeof
(
buf
)))
>
0
){
while
((
n
=
read
(
fd
,
buf
,
sizeof
(
buf
)))
>
0
){
for
(
j
=
0
;
j
<
n
;
j
++
){
for
(
j
=
0
;
j
<
n
;
j
++
){
if
(
buf
[
j
]
!=
(
i
?
'p'
:
'c'
)
){
if
(
buf
[
j
]
!=
'0'
+
i
){
printf
(
1
,
"wrong char
\n
"
);
printf
(
1
,
"wrong char
\n
"
);
exit
();
exit
();
}
}
...
@@ -568,87 +573,80 @@ twofiles(void)
...
@@ -568,87 +573,80 @@ twofiles(void)
printf
(
1
,
"wrong length %d
\n
"
,
total
);
printf
(
1
,
"wrong length %d
\n
"
,
total
);
exit
();
exit
();
}
}
unlink
(
fname
);
}
}
unlink
(
"f1"
);
printf
(
1
,
"fourfiles ok
\n
"
);
unlink
(
"f2"
);
printf
(
1
,
"twofiles ok
\n
"
);
}
}
//
two
processes create and delete different files in same directory
//
four
processes create and delete different files in same directory
void
void
createdelete
(
void
)
createdelete
(
void
)
{
{
enum
{
N
=
20
};
enum
{
N
=
20
};
int
pid
,
i
,
fd
;
int
pid
,
i
,
fd
,
pi
;
char
name
[
32
];
char
name
[
32
];
printf
(
1
,
"createdelete test
\n
"
);
printf
(
1
,
"createdelete test
\n
"
);
pid
=
fork
();
if
(
pid
<
0
){
printf
(
1
,
"fork failed
\n
"
);
exit
();
}
name
[
0
]
=
pid
?
'p'
:
'c'
;
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
name
[
2
]
=
'\0'
;
pid
=
fork
();
for
(
i
=
0
;
i
<
N
;
i
++
){
if
(
pid
<
0
){
name
[
1
]
=
'0'
+
i
;
printf
(
1
,
"fork failed
\n
"
);
fd
=
open
(
name
,
O_CREATE
|
O_RDWR
);
if
(
fd
<
0
){
printf
(
1
,
"create failed
\n
"
);
exit
();
exit
();
}
}
close
(
fd
);
if
(
i
>
0
&&
(
i
%
2
)
==
0
){
if
(
pid
==
0
){
name
[
1
]
=
'0'
+
(
i
/
2
);
name
[
0
]
=
'p'
+
pi
;
if
(
unlink
(
name
)
<
0
){
name
[
2
]
=
'\0'
;
printf
(
1
,
"unlink failed
\n
"
);
for
(
i
=
0
;
i
<
N
;
i
++
){
exit
();
name
[
1
]
=
'0'
+
i
;
fd
=
open
(
name
,
O_CREATE
|
O_RDWR
);
if
(
fd
<
0
){
printf
(
1
,
"create failed
\n
"
);
exit
();
}
close
(
fd
);
if
(
i
>
0
&&
(
i
%
2
)
==
0
){
name
[
1
]
=
'0'
+
(
i
/
2
);
if
(
unlink
(
name
)
<
0
){
printf
(
1
,
"unlink failed
\n
"
);
exit
();
}
}
}
}
exit
();
}
}
}
}
if
(
pid
==
0
)
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
exit
();
else
wait
();
wait
();
}
name
[
0
]
=
name
[
1
]
=
name
[
2
]
=
0
;
for
(
i
=
0
;
i
<
N
;
i
++
){
for
(
i
=
0
;
i
<
N
;
i
++
){
name
[
0
]
=
'p'
;
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
name
[
1
]
=
'0'
+
i
;
name
[
0
]
=
'p'
+
pi
;
fd
=
open
(
name
,
0
);
name
[
1
]
=
'0'
+
i
;
if
((
i
==
0
||
i
>=
N
/
2
)
&&
fd
<
0
){
fd
=
open
(
name
,
0
);
printf
(
1
,
"oops createdelete %s didn't exist
\n
"
,
name
);
if
((
i
==
0
||
i
>=
N
/
2
)
&&
fd
<
0
){
exit
();
printf
(
1
,
"oops createdelete %s didn't exist
\n
"
,
name
);
}
else
if
((
i
>=
1
&&
i
<
N
/
2
)
&&
fd
>=
0
){
exit
();
printf
(
1
,
"oops createdelete %s did exist
\n
"
,
name
);
}
else
if
((
i
>=
1
&&
i
<
N
/
2
)
&&
fd
>=
0
){
exit
();
printf
(
1
,
"oops createdelete %s did exist
\n
"
,
name
);
}
exit
();
if
(
fd
>=
0
)
}
close
(
fd
);
if
(
fd
>=
0
)
close
(
fd
);
name
[
0
]
=
'c'
;
name
[
1
]
=
'0'
+
i
;
fd
=
open
(
name
,
0
);
if
((
i
==
0
||
i
>=
N
/
2
)
&&
fd
<
0
){
printf
(
1
,
"oops createdelete %s didn't exist
\n
"
,
name
);
exit
();
}
else
if
((
i
>=
1
&&
i
<
N
/
2
)
&&
fd
>=
0
){
printf
(
1
,
"oops createdelete %s did exist
\n
"
,
name
);
exit
();
}
}
if
(
fd
>=
0
)
close
(
fd
);
}
}
for
(
i
=
0
;
i
<
N
;
i
++
){
for
(
i
=
0
;
i
<
N
;
i
++
){
name
[
0
]
=
'p'
;
for
(
pi
=
0
;
pi
<
4
;
pi
++
){
name
[
1
]
=
'0
'
+
i
;
name
[
0
]
=
'p
'
+
i
;
unlink
(
name
)
;
name
[
1
]
=
'0'
+
i
;
name
[
0
]
=
'c'
;
unlink
(
name
)
;
unlink
(
name
);
}
}
}
printf
(
1
,
"createdelete ok
\n
"
);
printf
(
1
,
"createdelete ok
\n
"
);
...
@@ -1716,6 +1714,12 @@ main(int argc, char *argv[])
...
@@ -1716,6 +1714,12 @@ main(int argc, char *argv[])
}
}
close
(
open
(
"usertests.ran"
,
O_CREATE
));
close
(
open
(
"usertests.ran"
,
O_CREATE
));
createdelete
();
linkunlink
();
concreate
();
fourfiles
();
sharedfd
();
bigargtest
();
bigargtest
();
bigwrite
();
bigwrite
();
bigargtest
();
bigargtest
();
...
@@ -1741,18 +1745,12 @@ main(int argc, char *argv[])
...
@@ -1741,18 +1745,12 @@ main(int argc, char *argv[])
fourteen
();
fourteen
();
bigfile
();
bigfile
();
subdir
();
subdir
();
concreate
();
linkunlink
();
linktest
();
linktest
();
unlinkread
();
unlinkread
();
createdelete
();
twofiles
();
sharedfd
();
dirfile
();
dirfile
();
iref
();
iref
();
forktest
();
forktest
();
bigdir
();
// slow
bigdir
();
// slow
exectest
();
exectest
();
exit
();
exit
();
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论