浏览代码

add test and fork

Paul Sajna 7 年之前
父节点
当前提交
12833a5a5c
共有 6 个文件被更改,包括 38 次插入1 次删除
  1. 4 0
      src/platform/src/linux/mod.rs
  2. 4 0
      src/platform/src/redox/mod.rs
  3. 1 1
      src/unistd/src/lib.rs
  4. 3 0
      tests/.gitignore
  5. 3 0
      tests/Makefile
  6. 23 0
      tests/pipe.c

+ 4 - 0
src/platform/src/linux/mod.rs

@@ -62,6 +62,10 @@ pub fn fchdir(fildes: c_int) -> c_int {
     e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
 }
 
+pub fn fork() -> pid_t {
+    e(unsafe {syscall!(FORK) }) as pid_t
+}
+
 pub fn fsync(fildes: c_int) -> c_int {
     e(unsafe { syscall!(FSYNC, fildes) }) as c_int
 }

+ 4 - 0
src/platform/src/redox/mod.rs

@@ -66,6 +66,10 @@ pub fn fchdir(fd: c_int) -> c_int {
     }
 }
 
+pub fn fork() -> pid_t {
+   e(unsafe { syscall::clone(0) }) as pid_t
+}
+
 pub fn fsync(fd: c_int) -> c_int {
     e(syscall::fsync(fd as usize)) as c_int
 }

+ 1 - 1
src/unistd/src/lib.rs

@@ -140,7 +140,7 @@ pub extern "C" fn fdatasync(fildes: c_int) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn fork() -> pid_t {
-    unimplemented!();
+    platform::fork()
 }
 
 #[no_mangle]

+ 3 - 0
tests/.gitignore

@@ -1,9 +1,11 @@
 /alloc
 /args
+/atoi
 /brk
 /chdir
 /create
 /create.out
+/ctype
 /dup
 /dup.out
 /error
@@ -15,5 +17,6 @@
 /link
 /link.out
 /math
+/pipe
 /printf
 /write

+ 3 - 0
tests/Makefile

@@ -1,9 +1,11 @@
 BINS=\
 	alloc \
+	atoi \
 	brk \
 	args \
 	chdir \
 	create \
+	ctype \
 	dup \
 	error \
 	fchdir \
@@ -12,6 +14,7 @@ BINS=\
 	getid \
 	link \
 	math \
+	pipe \
 	printf \
 	write
 

+ 23 - 0
tests/pipe.c

@@ -0,0 +1,23 @@
+//http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html
+#include <unistd.h>
+
+int main()
+{
+
+    int pid, pip[2];
+    char instring[20];
+
+    pipe(pip); 
+
+    pid = fork();
+    if (pid == 0)           /* child : sends message to parent*/
+    {
+        /* send 7 characters in the string, including end-of-string */
+        write(pip[1], "Hi Mom!", 7);  
+    }
+    else			/* parent : receives message from child */
+    {
+        /* read from the pipe */
+        read(pip[0], instring, 7);
+    }
+}