- FD reader = new InputStreamFD(readerStream);
- FD writer = new OutputStreamFD(writerStream);
- int fd1 = addFD(reader);
+
+ public class Writer extends FD {
+ protected FStat _fstat() { return new FStat(); }
+ public int write(byte[] buf, int off, int len) throws ErrnoException {
+ if(len == 0) return 0;
+ synchronized(Pipe.this) {
+ if(readPos == -1) throw new ErrnoException(EPIPE);
+ if(pipebuf.length - writePos < Math.min(len,PIPE_BUF)) {
+ // not enough space to atomicly write the data
+ while(readPos != -1 && readPos != writePos) {
+ try { Pipe.this.wait(); } catch(InterruptedException e) { /* ignore */ }
+ }
+ if(readPos == -1) throw new ErrnoException(EPIPE);
+ readPos = writePos = 0;
+ }
+ len = Math.min(len,pipebuf.length - writePos);
+ System.arraycopy(buf,off,pipebuf,writePos,len);
+ if(readPos == writePos) Pipe.this.notify();
+ writePos += len;
+ return len;
+ }
+ }
+ public void _close() { synchronized(Pipe.this) { writePos = -1; Pipe.this.notify(); } }
+ }
+ }
+
+ private int sys_pipe(int addr) {
+ Pipe pipe = new Pipe();
+
+ int fd1 = addFD(pipe.reader);