Embed presentation
Download to read offline
![#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
//SENDER PROCESS
//
//first start sender
//function open waits until receiver process has been started
#define MAX 100
void main ()
{
int fd;
char buf[MAX];
printf("Creating Pipe... Waiting for receiver process...nn");
//TRY TO CRATE A NAMED PIPE
if (mkfifo("FIFO_PIPE",0666)<0)
{
perror("FIFO (named pipe) could not be created.");
exit(-1);
}
else printf("nPipe has been created...");
//OPEN PIPE
if ((fd = open ("FIFO_PIPE", O_WRONLY))<0)
{
perror("Could not open named pipe.");
exit(-1);
}
else printf("Pipe has been opened.");
//WRITE TO PIPE
strcpy(buf,"Named Pipe experiment.");
if( write (fd,buf, strlen(buf)+1) < 0 )
{
perror("Error writing to named Pipe (FIFO)");
exit(-1);
}
else printf("Message has been written to pipe.");
//CLOSE FIFO
if (close(fd)<0)
{
perror("Error closing FIFO");
exit(-1);
}
exit(0);
}](https://image.slidesharecdn.com/sender-150316042613-conversion-gate01/75/Sender-1-2048.jpg)
This C code creates a named pipe called "FIFO_PIPE" between a sender and receiver process. The sender process opens the pipe for writing, copies a test string into a buffer, writes it to the pipe, and closes the pipe. Any errors in creating, opening, writing to, or closing the pipe cause the sender process to exit with an error message.
![#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
//SENDER PROCESS
//
//first start sender
//function open waits until receiver process has been started
#define MAX 100
void main ()
{
int fd;
char buf[MAX];
printf("Creating Pipe... Waiting for receiver process...nn");
//TRY TO CRATE A NAMED PIPE
if (mkfifo("FIFO_PIPE",0666)<0)
{
perror("FIFO (named pipe) could not be created.");
exit(-1);
}
else printf("nPipe has been created...");
//OPEN PIPE
if ((fd = open ("FIFO_PIPE", O_WRONLY))<0)
{
perror("Could not open named pipe.");
exit(-1);
}
else printf("Pipe has been opened.");
//WRITE TO PIPE
strcpy(buf,"Named Pipe experiment.");
if( write (fd,buf, strlen(buf)+1) < 0 )
{
perror("Error writing to named Pipe (FIFO)");
exit(-1);
}
else printf("Message has been written to pipe.");
//CLOSE FIFO
if (close(fd)<0)
{
perror("Error closing FIFO");
exit(-1);
}
exit(0);
}](https://image.slidesharecdn.com/sender-150316042613-conversion-gate01/75/Sender-1-2048.jpg)