Os/spring2019/simple-pipes

From Maryville College CS Wiki
< Os‎ | spring2019
Jump to: navigation, search
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>

int main()
{
    int p[2];  /* My pipe */
    char buf[100];
    int n;
  
    close(1);
    open("out.txt", O_CREAT|O_WRONLY|O_TRUNC);
    
    /* Establish the Pipeline */
    pipe(p);
    
    printf("PIPE: %d, %d\n", p[0], p[1]);
    
    /* talk to ourself */
    n=write(p[1], "hello", 5);
    printf("Wrote %d Bytes\n", n);
    n=read(p[0], buf, 99);
    printf("Read %d Bytes\n", n);
    
    /* terminate and print my string */
    buf[n] = '\0';
    printf("Buffer: %s\n", buf);
    
    return 0;
}