diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index 3f32ed29533759f177d711d0eed299b32b9dd78d..66fafabbb4ba0784fac5132442eaf6b7f1505cae 100644 --- a/src/platform/src/linux/mod.rs +++ b/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 } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 95f149bddda834a21d45642b0da91cde3c8b5bca..e9fe6ddaff40ae61c7bd064cd1e820c6a8ee46fa 100644 --- a/src/platform/src/redox/mod.rs +++ b/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 } diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs index c9dc8804fd03ce31aeba5c24caaa77887689189e..ea4609ba50405de7305fd72437092fa666a23e92 100644 --- a/src/unistd/src/lib.rs +++ b/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] diff --git a/tests/.gitignore b/tests/.gitignore index dead47ff3579202585046ec2979f6a161fa6702e..e5819ec54577f63583b4398ca86d61af361a7ef1 100644 --- a/tests/.gitignore +++ b/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 diff --git a/tests/Makefile b/tests/Makefile index 7d293f7246448f9a0ef3ae77d318a438f2478436..b371961d99a6ce9fcc32e2cdcb2bf424a1008cd8 100644 --- a/tests/Makefile +++ b/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 diff --git a/tests/pipe.c b/tests/pipe.c new file mode 100644 index 0000000000000000000000000000000000000000..0ad78bc3e526891ee72e6e3c36a6a0e4deeef21d --- /dev/null +++ b/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); + } +}