Posts

Showing posts with the label pipes

Chat application in C using named PIPES

Image
This is a simple set of two C Programs which can act as a chat application. Basically there are two programs which need to run simultaneously. One is the client program and other the server program. This core concept used behind this is FIFO, named pipes. Screenshots [Demo] Client Side Chat Terminal running the client output file Server Side Chat Terminal running the server output file The Code --> Receiver(Server) Side code //client_chat #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<unistd.h> #include<string.h> #include<linux/stat.h> #define FIFO_FILE "MYFIFO" int main() { FILE *fp; char a[40]; char readbuf[80]; start: if((fp=fopen(FIFO_FILE,"r+"))==NULL) { perror("fopen"); exit(1); } printf("Me : "); gets(a); if(a[strlen(a)-1]=='.') { fputs(a,fp); fclose(fp); ...

Find the factorial using PIPES in C

Image
This is a simple program in C which uses the pipes to find the factorial of a number. Initially the Child get the number from the user and performs the factorial operation. After performing the factorial operation it writes and gets closed. Now the parent extracts the stored data and displays it in the console. Screenshots[Demo] The Code: #include<stdio.h> #include<stdlib.h> #include<sys/wait.h> #include<unistd.h> int main() { int pfds[2]; int n,f=1,i,n1; pipe(pfds); if(!fork()) { printf("Child: Writing to the PIpe \n"); printf("Child: Enter the NUmber:"); scanf("%d",&n); for(i=1;i<=n;i++) f=f*i; write(pfds[1],(char *)&f,sizeof(f)); close(pfds[1]); printf("Child: Exiting\n"); } else { wait(0); printf("Parent: reaading from pipe\n"); read(pfds[0],&n1,sizeof(int)); printf("Parent : Read %d\n",n1); sleep(1); close(pfds[0]); } return 0; } Got...

Implementation of Pipe in parallelism in c

Image
The below program is a simple implementation of pipe in c. In this, the child thread takes the input and the parent child reads it. Screenshots[Demo] Code :  #include<stdlib.h> #include<stdio.h> #include<unistd.h> int main() { int pfds[2];char buf[30]; pipe(pfds); if(!fork()) { printf("CHILD:writing to the pipe\n"); write(pfds[1],"test",5); close(pfds[1]); printf("CHILD:exiting \n"); } else { sleep(3); printf("PARENT : reading from pipe\n"); read(pfds[0],buf,5); printf("PARENT:read %s",buf); close(pfds[0]); } return 0; }