It looks like you're new here. If you want to get involved, click one of these buttons!
//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);
}
//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);
}