Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (3)

Powered by Vanilla. Made with Bootstrap.
Unix pipes
  • GT3X
    Posts: 20
    write.c

    //Demonstration of named pipes
    //Write opens a pipe then it writes into the pipe

    #include <stdio.h>
    #include <stdlib.h>
    //Pipe call
    #include <fcntl.h>
    //mknod call
    #include <sys/types.h>
    #include <sys/stat.h>

    #define BUFLEN 100

    //Prototypes

    int main (void);
    void input(char *);

    //Var's

    //Filedescritpor
    int fdes;
    int a,i;
    char *s, buffer[BUFLEN];

    int main(void)
    {
    /*
    Creates a FIFO-File with the rights read/write
    for the user and read for others.
    */
    mknod(\"pipe1\",S_IFIFO | 0666,0);

    /*
    Opens a pipe. This process can only write into a pipe. If its opend successfull open() gives the filedescriptor back otherwise -1.
    */
    if((fdes=open(\"pipe1\",O_WRONLY))==-1){
    puts(\"Error couldn't open pipe\");
    exit(-1);
    }

    //Writes the buffer into the FIFO file
    input(buffer);
    if((i=write(fdes,buffer,BUFLEN)) !=BUFLEN) {
    printf(\"Error couldn't write\");
    exit(-1);
    }

    //Now we have to close the FIFO File
    close(fdes);
    exit(0);
    }

    //Get input?:0
    void input(char *buffer)
    {
    printf(\"Enter a String(max 100 Chars):\");
    buffer = gets(buffer);
    }


    http://nopaste.me/paste/17116484874e09e1adbeed2



    //open and reads a pipe
    #include <stdio.h>
    #include <stdlib.h>
    //Pipe call
    #include <fcntl.h>
    //mknod call
    #include <sys/types.h>
    #include <sys/stat.h>

    #define BUFLEN 100

    //Protoypes
    int main(void);

    //Vars
    int fdes;
    char buffer[BUFLEN];
    char c;

    int main(void)
    {
    //A pipe gets opend in \"read only\" mode.
    if((fdes=open(\"pipe1\",O_RDONLY))==(-1))
    {
    printf(\"Error couldn't open pipe\");
    exit(-1);
    }
    read(fdes,buffer,BUFLEN);
    puts(buffer);
    close(fdes);
    exit(0);
    }


    http://nopaste.me/paste/19938196674e09e1e237ba9