Simple C programs demonstarting use of mkfifo( ) system call.

SYS CALL NAME
mkfifo – make FIFOs (named pipes)SYNOPSIS
mkfifo [OPTION]… NAME…
DESCRIPTION
Create named pipes (FIFOs) with the given NAMEs.
Mandatory  arguments  to  long  options are mandatory for short options
too.
-m, –mode=MODE
set file permission bits to MODE, not a=rw – umask
-Z, –context=CTX
set the SELinux security context of each NAME to CTX
–help display this help and exit
–version
output version information and exit

 

#include<stdio.h>#include<sys/errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include<string.h>void main (){    int fd;    char a[10];    char buff[10];    int i=0;    printf("nnEnter numbersnn");    for(i=0;i<10;i++)    scanf("%c",&a[i]);    char * myfifo = "/home/student/shubham";    /* create the FIFO (named pipe) */    mkfifo(myfifo, 0666);    /* write to the FIFO */    fd = open(myfifo,O_WRONLY);    write(fd,a,sizeof(a));    close(fd);    /* remove the FIFO */    //unlink(myfifo);}

 

shubham2.c

#include<stdio.h>#include<sys/errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include<string.h>void main (){ int fd; char * myfifo = "/home/student/shubham"; char buf[10];/* open, read, and display the text from the FIFO file */  fd = open(myfifo, O_RDONLY);  read(fd, buf, sizeof(buf));  int i,j;   for(i=0;i<10;i++)  {      for(j=0;j<10;j++)      {          if(buf[i]<buf[j])          {              char temp;              temp=buf[i];              buf[i]=buf[j];              buf[j]=temp;          }           }  }    printf("nSorted Array is as Follows:n");  for(i=0;i<10;i++)  printf("%cn", buf[i]);  close(fd); }

 
Output:

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+