mirror of
https://github.com/livingcomputermuseum/pdp7-unix.git
synced 2026-02-10 18:30:43 +00:00
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:
125
src/cmd/init.s
125
src/cmd/init.s
@@ -2,46 +2,46 @@
|
||||
|
||||
-1
|
||||
sys intrp
|
||||
jms init1
|
||||
jms init2
|
||||
jms init1 " Fork the first child connected to ttyin/ttyout
|
||||
jms init2 " Fork the second child connected to keyboard/display
|
||||
1:
|
||||
sys rmes
|
||||
sys rmes " Wait for a child to exit
|
||||
sad pid1
|
||||
jmp 1f
|
||||
jmp 1f " It was child 1, so jump to 1f and restart it
|
||||
sad pid2
|
||||
jms init2
|
||||
jmp 1
|
||||
jms init2 " It was child 2, so restart it
|
||||
jmp 1 " and loop back. XXX: weird use of 1: not 1b. I don't like it!
|
||||
1:
|
||||
jms init1
|
||||
jmp 1
|
||||
jmp 1 " Weird use of 1: not 1b. I don't like it!
|
||||
|
||||
init1: 0
|
||||
sys fork
|
||||
jmp 1f
|
||||
sys open; ttyin; 0
|
||||
sys open; ttyout; 1
|
||||
jmp login
|
||||
sys fork " Fork a child process
|
||||
jmp 1f
|
||||
sys open; ttyin; 0 " which opens the ttyin
|
||||
sys open; ttyout; 1 " and ttyout files, and
|
||||
jmp login " waits for a user to log in
|
||||
1:
|
||||
dac pid1
|
||||
jmp init1 i
|
||||
dac pid1 " Parent stores childs pid in pid1
|
||||
jmp init1 i " and returns
|
||||
|
||||
init2: 0
|
||||
sys fork
|
||||
jmp 1f
|
||||
sys open; keybd; 0
|
||||
sys open; displ; 1
|
||||
jmp login
|
||||
sys fork " Fork a child process
|
||||
jmp 1f
|
||||
sys open; keybd; 0 " which opens the keyboard
|
||||
sys open; displ; 1 " and display files, and
|
||||
jmp login " waits for a user to log in
|
||||
1:
|
||||
dac pid2
|
||||
jmp init2 i
|
||||
dac pid2 " Parent stores childs pid in pid2
|
||||
jmp init2 i " and returns
|
||||
|
||||
login:
|
||||
-1
|
||||
sys intrp
|
||||
sys open; password; 0
|
||||
sys open; password; 0 " Open the passwd file
|
||||
lac d1
|
||||
sys write; m1; m1s
|
||||
jms rline
|
||||
sys write; m1; m1s " Write "\nlogin:" on the terminal
|
||||
jms rline " and read the user's username
|
||||
lac ebufp
|
||||
dac tal
|
||||
1:
|
||||
@@ -55,31 +55,31 @@ login:
|
||||
sac o12
|
||||
lac o72
|
||||
sad 9 i
|
||||
skp
|
||||
skp
|
||||
jmp 1b
|
||||
sad o72
|
||||
skp
|
||||
skp
|
||||
jmp 2b
|
||||
lac 9 i
|
||||
sad o72
|
||||
jmp 1f
|
||||
jmp 1f
|
||||
-1
|
||||
tad 9
|
||||
dac 9
|
||||
lac d1
|
||||
sys write; m3; m3s
|
||||
jms rline
|
||||
sys write; m3; m3s " Write "password: " on the terminal
|
||||
jms rline " and read the user's password
|
||||
law ibuf-1
|
||||
dac 8
|
||||
2:
|
||||
lac 8 i
|
||||
sad o12
|
||||
lac o72
|
||||
lac o72
|
||||
sad 9 i
|
||||
skp
|
||||
skp
|
||||
jmp error
|
||||
sad o72
|
||||
skp
|
||||
skp
|
||||
jmp 2b
|
||||
1:
|
||||
dzm nchar
|
||||
@@ -88,11 +88,11 @@ login:
|
||||
1:
|
||||
lac 9 i
|
||||
sad o72
|
||||
jmp 1f
|
||||
jmp 1f
|
||||
dac char
|
||||
lac nchar
|
||||
sza
|
||||
jmp 2f
|
||||
jmp 2f
|
||||
lac char
|
||||
alss 9
|
||||
xor o40
|
||||
@@ -113,7 +113,7 @@ login:
|
||||
1:
|
||||
lac 9 i
|
||||
sad o12
|
||||
jmp 2f
|
||||
jmp 2f
|
||||
tad om60
|
||||
lmq
|
||||
lac nchar
|
||||
@@ -123,42 +123,43 @@ login:
|
||||
jmp 1b
|
||||
2:
|
||||
lac nchar
|
||||
sys setuid
|
||||
sys chdir; dd
|
||||
sys setuid " Set the user's user-id
|
||||
sys chdir; dd " and change into the "dd" directory
|
||||
sys chdir; dir
|
||||
|
||||
lac d2
|
||||
lac d2 " Close file descriptor 2
|
||||
sys close
|
||||
sys open; sh; 0
|
||||
sys open; sh; 0 " Open the shell executable file (we get fd 2)
|
||||
sma
|
||||
jmp 1f
|
||||
jmp 1f
|
||||
sys link; system; sh; sh
|
||||
spa
|
||||
jmp error
|
||||
jmp error
|
||||
sys open; sh; 0
|
||||
spa
|
||||
jmp error
|
||||
jmp error
|
||||
sys unlink; sh
|
||||
1:
|
||||
law 017700
|
||||
dac 9
|
||||
law 017700 " Copy the code at the boot label below
|
||||
dac 9 " up to location 017700
|
||||
law boot-1
|
||||
dac 8
|
||||
1:
|
||||
lac 8 i
|
||||
dac 9 i
|
||||
sza
|
||||
jmp 1b
|
||||
jmp 017701
|
||||
sza " Stop copying when we hit the 0 marker
|
||||
jmp 1b
|
||||
jmp 017701 " and then jump to the code
|
||||
|
||||
boot:
|
||||
lac d2
|
||||
lmq
|
||||
sys read; 4096; 07700
|
||||
lacq
|
||||
sys close
|
||||
jmp 4096
|
||||
0
|
||||
lmq " Save the fd into MQ
|
||||
sys read; 4096; 07700 " Read 4,032 words into locations 4096 onwards
|
||||
" leaving the top 64 words for this boot code.
|
||||
lacq " Get the fd back and close the file
|
||||
sys close
|
||||
jmp 4096 " and jump to the beginning of that executable
|
||||
0 " 0 marks the end of the code, used by the copy routine above
|
||||
|
||||
rline: 0
|
||||
law ibuf-1
|
||||
@@ -169,17 +170,17 @@ rline: 0
|
||||
lac char
|
||||
lrss 9
|
||||
sad o100
|
||||
jmp rline+1
|
||||
jmp rline+1
|
||||
sad o43
|
||||
jmp 2f
|
||||
dac 8 i
|
||||
sad o12
|
||||
jmp rline i
|
||||
jmp rline i
|
||||
jmp 1b
|
||||
2:
|
||||
law ibuf-1
|
||||
sad 8
|
||||
jmp 1b
|
||||
jmp 1b
|
||||
-1
|
||||
tad 8
|
||||
dac 8
|
||||
@@ -192,17 +193,17 @@ gline: 0
|
||||
jms gchar
|
||||
dac 8 i
|
||||
sad o12
|
||||
jmp gline i
|
||||
jmp gline i
|
||||
jmp 1b
|
||||
|
||||
gchar: 0
|
||||
lac tal
|
||||
sad ebufp
|
||||
jmp 1f
|
||||
jmp 1f
|
||||
ral
|
||||
lac tal i
|
||||
snl
|
||||
lrss 9
|
||||
lrss 9
|
||||
and o777
|
||||
lmq
|
||||
lac tal
|
||||
@@ -210,7 +211,7 @@ gchar: 0
|
||||
dac tal
|
||||
lacq
|
||||
sna
|
||||
jmp gchar+1
|
||||
jmp gchar+1
|
||||
jmp gchar i
|
||||
1:
|
||||
lac bufp
|
||||
@@ -220,14 +221,14 @@ gchar: 0
|
||||
isz tal
|
||||
lac tal
|
||||
sad ebufp
|
||||
skp
|
||||
skp
|
||||
jmp 1b
|
||||
lac bufp
|
||||
dac tal
|
||||
lac d2
|
||||
sys tead; buf; 64
|
||||
sna
|
||||
jmp error
|
||||
jmp error
|
||||
jmp gchar+1
|
||||
|
||||
error:
|
||||
|
||||
20
src/other/fork_test.s
Normal file
20
src/other/fork_test.s
Normal file
@@ -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
tools/a7out
22
tools/a7out
@@ -533,9 +533,11 @@ sub cal {
|
||||
9 => \&sys_close,
|
||||
11 => \&sys_unlink,
|
||||
14 => \&sys_exit,
|
||||
16 => \&sys_intrp,
|
||||
17 => \&sys_chdir,
|
||||
18 => \&sys_chmod,
|
||||
19 => \&sys_chown,
|
||||
29 => \&sys_fork,
|
||||
);
|
||||
|
||||
# Simulate the syscall. Each syscall updates the $PC
|
||||
@@ -553,6 +555,26 @@ sub sys_exit {
|
||||
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
|
||||
sub sys_close {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user