Skip to content
Snippets Groups Projects
Commit cfbe2749 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Remove allocation, fix pipe example

parent 0bcabc21
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,3 @@ sc = "0.2" ...@@ -8,6 +8,3 @@ sc = "0.2"
[target.'cfg(target_os = "redox")'.dependencies] [target.'cfg(target_os = "redox")'.dependencies]
redox_syscall = "0.1" redox_syscall = "0.1"
[dependencies]
alloc-no-stdlib = "1.2"
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#![no_std] #![no_std]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
//TODO #![feature(thread_local)] //TODO #![feature(thread_local)]
#![feature(alloc)]
extern crate alloc;
#[cfg(all(not(feature = "no_std"), target_os = "linux"))] #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
#[macro_use] #[macro_use]
......
extern crate alloc;
use core::ptr; use core::ptr;
use core::slice; use core::slice;
use alloc::Vec;
use syscall; use syscall;
use c_str; use c_str;
...@@ -127,11 +124,11 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { ...@@ -127,11 +124,11 @@ pub fn open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
} }
pub fn pipe(fds: [c_int; 2]) -> c_int { pub fn pipe(fds: [c_int; 2]) -> c_int {
let usize_vec = fds.iter().map(|x| *x as usize).collect::<Vec<usize>>(); let mut usize_fds: [usize; 2] = [0; 2];
let usize_slice = usize_vec.as_slice(); let res = e(syscall::pipe2(&mut usize_fds));
let mut usize_arr: [usize; 2] = Default::default(); fds[0] = usize_fds[0] as c_int;
usize_arr.copy_from_slice(usize_slice); fds[1] = usize_fds[1] as c_int;
e(syscall::pipe2(&mut usize_arr, 0)) as c_int res as c_int
} }
pub fn read(fd: c_int, buf: &mut [u8]) -> ssize_t { pub fn read(fd: c_int, buf: &mut [u8]) -> ssize_t {
......
//http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html //http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html
#include <string.h>
#include <unistd.h> #include <unistd.h>
int main() int main()
...@@ -6,18 +7,27 @@ int main() ...@@ -6,18 +7,27 @@ int main()
int pid, pip[2]; int pid, pip[2];
char instring[20]; char instring[20];
char * outstring = "Hello World!";
pipe(pip); pipe(pip);
pid = fork(); pid = fork();
if (pid == 0) /* child : sends message to parent*/ if (pid == 0) /* child : sends message to parent*/
{ {
/* close read end */
close(pip[0]);
/* send 7 characters in the string, including end-of-string */ /* send 7 characters in the string, including end-of-string */
write(pip[1], "Hi Mom!", 7); write(pip[1], outstring, strlen(outstring));
/* close write end */
close(pip[1]);
} }
else /* parent : receives message from child */ else /* parent : receives message from child */
{ {
/* close write end */
close(pip[1]);
/* read from the pipe */ /* read from the pipe */
read(pip[0], instring, 7); read(pip[0], instring, 7);
/* close read end */
close(pip[0]);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment