Knowledge Base Nr: 00086 fifo_linux.cpp - http://www.swe-kaiser.de

Downloads:

linux: beispiel fifo (named pipe)

  

#include <iostream.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
cout << "Start!" << endl;

char* lpszPipe = "/home/testpipe1";

int nErr = mkfifo(lpszPipe, S_IRWXU|S_IRWXG|S_IRWXO);
if (nErr)
cerr << "Fehler: mkfifo " << nErr << endl;

int fd;
if (argc > 1)
{
cout << "Schreiben!" << endl;
fd = open(lpszPipe, O_WRONLY);
}
else
{
cout << "Lesen!" << endl;
fd = open(lpszPipe, O_RDONLY);
}

if (fd < 0)
cerr << "Fehler: open " << fd << endl;

if (argc > 1)
{
write(fd, "lulli", 5);
}
else
{
char buffer[200];
int nRead = read(fd, buffer, 200);
for (int n=0; n<nRead; n++)
cout << buffer[n];
cout << endl;
}

close (fd);

cout << "Ende!" << endl;

return EXIT_SUCCESS;
}