Remove allocation, fix pipe example
This commit is contained in:
@@ -8,6 +8,3 @@ sc = "0.2"
|
||||
|
||||
[target.'cfg(target_os = "redox")'.dependencies]
|
||||
redox_syscall = "0.1"
|
||||
|
||||
[dependencies]
|
||||
alloc-no-stdlib = "1.2"
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#![no_std]
|
||||
#![allow(non_camel_case_types)]
|
||||
//TODO #![feature(thread_local)]
|
||||
#![feature(alloc)]
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(all(not(feature = "no_std"), target_os = "linux"))]
|
||||
#[macro_use]
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
extern crate alloc;
|
||||
|
||||
use core::ptr;
|
||||
use core::slice;
|
||||
use alloc::Vec;
|
||||
use syscall;
|
||||
|
||||
use c_str;
|
||||
@@ -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 {
|
||||
let usize_vec = fds.iter().map(|x| *x as usize).collect::<Vec<usize>>();
|
||||
let usize_slice = usize_vec.as_slice();
|
||||
let mut usize_arr: [usize; 2] = Default::default();
|
||||
usize_arr.copy_from_slice(usize_slice);
|
||||
e(syscall::pipe2(&mut usize_arr, 0)) as c_int
|
||||
let mut usize_fds: [usize; 2] = [0; 2];
|
||||
let res = e(syscall::pipe2(&mut usize_fds));
|
||||
fds[0] = usize_fds[0] as c_int;
|
||||
fds[1] = usize_fds[1] as c_int;
|
||||
res as c_int
|
||||
}
|
||||
|
||||
pub fn read(fd: c_int, buf: &mut [u8]) -> ssize_t {
|
||||
|
||||
+12
-2
@@ -1,4 +1,5 @@
|
||||
//http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
@@ -6,18 +7,27 @@ int main()
|
||||
|
||||
int pid, pip[2];
|
||||
char instring[20];
|
||||
char * outstring = "Hello World!";
|
||||
|
||||
pipe(pip);
|
||||
pipe(pip);
|
||||
|
||||
pid = fork();
|
||||
if (pid == 0) /* child : sends message to parent*/
|
||||
{
|
||||
/* close read end */
|
||||
close(pip[0]);
|
||||
/* 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 */
|
||||
{
|
||||
/* close write end */
|
||||
close(pip[1]);
|
||||
/* read from the pipe */
|
||||
read(pip[0], instring, 7);
|
||||
/* close read end */
|
||||
close(pip[0]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user