Skip to content

Commit 9ea62bd

Browse files
committed
getChildren syscall and getChildren test added to project.
1 parent 8c8ac25 commit 9ea62bd

11 files changed

+102
-7
lines changed

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"*.tcc": "c",
4+
"cstdio": "c"
5+
}
6+
}

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ UPROGS=\
182182
_wc\
183183
_zombie\
184184
_getParentIDtest\
185+
_getChildrentest\
185186

186187
fs.img: mkfs README $(UPROGS)
187188
./mkfs fs.img README $(UPROGS)
@@ -253,6 +254,7 @@ EXTRA=\
253254
ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\
254255
printf.c umalloc.c\
255256
getParentIDtest.c\
257+
getChildrentest.c\
256258
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
257259
.gdbinit.tmpl gdbutil\
258260

defs.h

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ int wait(void);
121121
void wakeup(void*);
122122
void yield(void);
123123
int getparentid(void);
124+
int getChildren(char* ptr);
124125

125126
// swtch.S
126127
void swtch(struct context**, struct context*);

getChildrentest.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# include "types.h"
2+
# include "stat.h"
3+
# include "user.h"
4+
5+
6+
void printChildren(char* children);
7+
8+
int main(void)
9+
{
10+
int p = 0;
11+
char* ptr = (char*)malloc(64 * sizeof(int));
12+
13+
p = fork();
14+
15+
for (int i = 0; i < 7; i++)
16+
{
17+
if(p != 0)
18+
{
19+
p = fork();
20+
}
21+
}
22+
23+
if(p != 0){
24+
getChildren(ptr);
25+
printChildren(ptr);
26+
}
27+
28+
for (int i = 0; i < 8; i++)
29+
{
30+
wait();
31+
}
32+
33+
exit();
34+
35+
return 0;
36+
}
37+
38+
void
39+
printChildren(char* children)
40+
{
41+
int i = 0;
42+
43+
printf(1, "This is process %d and children are : ", getpid());
44+
45+
while (children[i] != 0)
46+
{
47+
printf(1, "%d/ ", children[i]);
48+
i++;
49+
}
50+
printf(1, "\n");
51+
}

getParentIDtest.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ int main()
77
int p;
88

99
printf(1, "--------------Start Testing--------------\n");
10-
printf(1, "This is process %d and the parent id is %d\n", getpid(), getparentid());
10+
printf(1, "This is process %d and the parent id is %d.\n", getpid(), getparentid());
1111
p = fork();
1212

1313
if(p == 0)
1414
{
1515
printf(1, "----------------Step One----------------\n");
16-
printf(1, "This is process %d and the parent id is %d\n", getpid(), getparentid());
16+
printf(1, "This is process %d and the parent id is %d.\n", getpid(), getparentid());
1717

1818
p = fork();
1919

2020
if(p == 0)
2121
{
2222
printf(1, "----------------Step two----------------\n");
23-
printf(1, "This is process %d and the parent id is %d\n", getpid(), getparentid());
23+
printf(1, "This is process %d and the parent id is %d.\n", getpid(), getparentid());
2424

2525
p = fork();
2626
if(p == 0)
2727
{
28-
printf(1, "----------------Step three----------------\n");
29-
printf(1, "This is process %d and the parent id is %d\n", getpid(), getparentid());
28+
printf(1, "---------------Step three---------------\n");
29+
printf(1, "This is process %d and the parent id is %d.\n", getpid(), getparentid());
3030
}
3131

3232
wait();
3333
}
3434
wait();
3535
}
3636
wait();
37-
37+
exit();
3838
return 0;
3939
}

proc.c

+18
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,22 @@ getparentid(void)
539539
struct proc *curproc = myproc();
540540
int chlid_pid = curproc->parent->pid;
541541
return chlid_pid;
542+
}
543+
544+
int
545+
getChildren(char * ptr)
546+
{
547+
struct proc *curproc = myproc();
548+
struct proc *p;
549+
int i = 0;
550+
551+
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
552+
{
553+
if(p->parent == curproc)
554+
{
555+
ptr[i] = p->pid;
556+
i++;
557+
}
558+
}
559+
return 1;
542560
}

syscall.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extern int sys_wait(void);
104104
extern int sys_write(void);
105105
extern int sys_uptime(void);
106106
extern int sys_getparentid(void);
107+
extern int sys_getChildren(void);
107108

108109
static int (*syscalls[])(void) = {
109110
[SYS_fork] sys_fork,
@@ -127,7 +128,8 @@ static int (*syscalls[])(void) = {
127128
[SYS_link] sys_link,
128129
[SYS_mkdir] sys_mkdir,
129130
[SYS_close] sys_close,
130-
[SYS_getparentid] sys_getparentid
131+
[SYS_getparentid] sys_getparentid,
132+
[SYS_getChildren] sys_getChildren
131133
};
132134

133135
void

syscall.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
#define SYS_mkdir 20
2222
#define SYS_close 21
2323
#define SYS_getparentid 22
24+
#define SYS_getChildren 23

sysproc.c

+12
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,16 @@ int
9494
sys_getparentid(void)
9595
{
9696
return getparentid();
97+
}
98+
99+
int
100+
sys_getChildren(void)
101+
{
102+
char * ptr;
103+
104+
if(argptr(0, &ptr, 4) < 0)
105+
return -1;
106+
107+
getChildren(ptr);
108+
return 1;
97109
}

user.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ char* sbrk(int);
2424
int sleep(int);
2525
int uptime(void);
2626
int getparentid(void);
27+
int getChildren(char* ptr);
2728

2829
// ulib.c
2930
int stat(const char*, struct stat*);

usys.S

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ SYSCALL(sbrk)
3030
SYSCALL(sleep)
3131
SYSCALL(uptime)
3232
SYSCALL(getparentid)
33+
SYSCALL(getChildren)

0 commit comments

Comments
 (0)