I've added fork() to a7out after adding some comments to src/cmd/init.s.
There is a test program to demonstrate fork working.
This commit is contained in:
+38
-37
@@ -2,46 +2,46 @@
|
|||||||
|
|
||||||
-1
|
-1
|
||||||
sys intrp
|
sys intrp
|
||||||
jms init1
|
jms init1 " Fork the first child connected to ttyin/ttyout
|
||||||
jms init2
|
jms init2 " Fork the second child connected to keyboard/display
|
||||||
1:
|
1:
|
||||||
sys rmes
|
sys rmes " Wait for a child to exit
|
||||||
sad pid1
|
sad pid1
|
||||||
jmp 1f
|
jmp 1f " It was child 1, so jump to 1f and restart it
|
||||||
sad pid2
|
sad pid2
|
||||||
jms init2
|
jms init2 " It was child 2, so restart it
|
||||||
jmp 1
|
jmp 1 " and loop back. XXX: weird use of 1: not 1b. I don't like it!
|
||||||
1:
|
1:
|
||||||
jms init1
|
jms init1
|
||||||
jmp 1
|
jmp 1 " Weird use of 1: not 1b. I don't like it!
|
||||||
|
|
||||||
init1: 0
|
init1: 0
|
||||||
sys fork
|
sys fork " Fork a child process
|
||||||
jmp 1f
|
jmp 1f
|
||||||
sys open; ttyin; 0
|
sys open; ttyin; 0 " which opens the ttyin
|
||||||
sys open; ttyout; 1
|
sys open; ttyout; 1 " and ttyout files, and
|
||||||
jmp login
|
jmp login " waits for a user to log in
|
||||||
1:
|
1:
|
||||||
dac pid1
|
dac pid1 " Parent stores childs pid in pid1
|
||||||
jmp init1 i
|
jmp init1 i " and returns
|
||||||
|
|
||||||
init2: 0
|
init2: 0
|
||||||
sys fork
|
sys fork " Fork a child process
|
||||||
jmp 1f
|
jmp 1f
|
||||||
sys open; keybd; 0
|
sys open; keybd; 0 " which opens the keyboard
|
||||||
sys open; displ; 1
|
sys open; displ; 1 " and display files, and
|
||||||
jmp login
|
jmp login " waits for a user to log in
|
||||||
1:
|
1:
|
||||||
dac pid2
|
dac pid2 " Parent stores childs pid in pid2
|
||||||
jmp init2 i
|
jmp init2 i " and returns
|
||||||
|
|
||||||
login:
|
login:
|
||||||
-1
|
-1
|
||||||
sys intrp
|
sys intrp
|
||||||
sys open; password; 0
|
sys open; password; 0 " Open the passwd file
|
||||||
lac d1
|
lac d1
|
||||||
sys write; m1; m1s
|
sys write; m1; m1s " Write "\nlogin:" on the terminal
|
||||||
jms rline
|
jms rline " and read the user's username
|
||||||
lac ebufp
|
lac ebufp
|
||||||
dac tal
|
dac tal
|
||||||
1:
|
1:
|
||||||
@@ -67,8 +67,8 @@ login:
|
|||||||
tad 9
|
tad 9
|
||||||
dac 9
|
dac 9
|
||||||
lac d1
|
lac d1
|
||||||
sys write; m3; m3s
|
sys write; m3; m3s " Write "password: " on the terminal
|
||||||
jms rline
|
jms rline " and read the user's password
|
||||||
law ibuf-1
|
law ibuf-1
|
||||||
dac 8
|
dac 8
|
||||||
2:
|
2:
|
||||||
@@ -123,13 +123,13 @@ login:
|
|||||||
jmp 1b
|
jmp 1b
|
||||||
2:
|
2:
|
||||||
lac nchar
|
lac nchar
|
||||||
sys setuid
|
sys setuid " Set the user's user-id
|
||||||
sys chdir; dd
|
sys chdir; dd " and change into the "dd" directory
|
||||||
sys chdir; dir
|
sys chdir; dir
|
||||||
|
|
||||||
lac d2
|
lac d2 " Close file descriptor 2
|
||||||
sys close
|
sys close
|
||||||
sys open; sh; 0
|
sys open; sh; 0 " Open the shell executable file (we get fd 2)
|
||||||
sma
|
sma
|
||||||
jmp 1f
|
jmp 1f
|
||||||
sys link; system; sh; sh
|
sys link; system; sh; sh
|
||||||
@@ -140,25 +140,26 @@ login:
|
|||||||
jmp error
|
jmp error
|
||||||
sys unlink; sh
|
sys unlink; sh
|
||||||
1:
|
1:
|
||||||
law 017700
|
law 017700 " Copy the code at the boot label below
|
||||||
dac 9
|
dac 9 " up to location 017700
|
||||||
law boot-1
|
law boot-1
|
||||||
dac 8
|
dac 8
|
||||||
1:
|
1:
|
||||||
lac 8 i
|
lac 8 i
|
||||||
dac 9 i
|
dac 9 i
|
||||||
sza
|
sza " Stop copying when we hit the 0 marker
|
||||||
jmp 1b
|
jmp 1b
|
||||||
jmp 017701
|
jmp 017701 " and then jump to the code
|
||||||
|
|
||||||
boot:
|
boot:
|
||||||
lac d2
|
lac d2
|
||||||
lmq
|
lmq " Save the fd into MQ
|
||||||
sys read; 4096; 07700
|
sys read; 4096; 07700 " Read 4,032 words into locations 4096 onwards
|
||||||
lacq
|
" leaving the top 64 words for this boot code.
|
||||||
|
lacq " Get the fd back and close the file
|
||||||
sys close
|
sys close
|
||||||
jmp 4096
|
jmp 4096 " and jump to the beginning of that executable
|
||||||
0
|
0 " 0 marks the end of the code, used by the copy routine above
|
||||||
|
|
||||||
rline: 0
|
rline: 0
|
||||||
law ibuf-1
|
law ibuf-1
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
" Fork text code
|
||||||
|
|
||||||
|
main:
|
||||||
|
sys fork
|
||||||
|
jmp parent
|
||||||
|
|
||||||
|
child:
|
||||||
|
lac d1
|
||||||
|
sys write; childmsg; 3
|
||||||
|
sys exit
|
||||||
|
|
||||||
|
parent:
|
||||||
|
lac d1
|
||||||
|
sys write; parentmsg; 4
|
||||||
|
sys exit
|
||||||
|
|
||||||
|
d1: 1 " stdout fd
|
||||||
|
|
||||||
|
parentmsg: <pa>; <re>; <nt>; 012000
|
||||||
|
childmsg: <ch>; <il>; <d 012
|
||||||
+22
@@ -533,9 +533,11 @@ sub cal {
|
|||||||
9 => \&sys_close,
|
9 => \&sys_close,
|
||||||
11 => \&sys_unlink,
|
11 => \&sys_unlink,
|
||||||
14 => \&sys_exit,
|
14 => \&sys_exit,
|
||||||
|
16 => \&sys_intrp,
|
||||||
17 => \&sys_chdir,
|
17 => \&sys_chdir,
|
||||||
18 => \&sys_chmod,
|
18 => \&sys_chmod,
|
||||||
19 => \&sys_chown,
|
19 => \&sys_chown,
|
||||||
|
29 => \&sys_fork,
|
||||||
);
|
);
|
||||||
|
|
||||||
# Simulate the syscall. Each syscall updates the $PC
|
# Simulate the syscall. Each syscall updates the $PC
|
||||||
@@ -553,6 +555,26 @@ sub sys_exit {
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Intrp system call
|
||||||
|
sub sys_intrp {
|
||||||
|
# For now, do nothing
|
||||||
|
dprint("intrp system call\n");
|
||||||
|
$PC += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fork system call
|
||||||
|
sub sys_fork {
|
||||||
|
|
||||||
|
# Fork and get the child's process-id back, or zero if we are the child
|
||||||
|
my $pid= fork();
|
||||||
|
$AC= $pid;
|
||||||
|
|
||||||
|
# The parent returns back to PC+1, the child returns to PC+2
|
||||||
|
$PC += ($pid) ? 1 : 2;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
# Close system call
|
# Close system call
|
||||||
sub sys_close {
|
sub sys_close {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user