Skip to content

Instantly share code, notes, and snippets.

@navin-mohan
Created April 11, 2018 03:34
Show Gist options
  • Save navin-mohan/6c48959bf0fe708dbd36f3882b035b26 to your computer and use it in GitHub Desktop.
Save navin-mohan/6c48959bf0fe708dbd36f3882b035b26 to your computer and use it in GitHub Desktop.
FIFO NET LAB
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int n,i,a,fd1;
char *fifo1 = "/tmp/test";
char *fifo2 = "/tmp/test1";
mkfifo(fifo1,0666);
mkfifo(fifo2,0666);
scanf("%d",&n);
// arr = (int*) malloc(sizeof(int)*n);
fd1 = open(fifo1,O_WRONLY);
write(fd1,&n,sizeof(int));
for(i=0;i<n;++i){
scanf("%d",&a);
write(fd1,&a,sizeof(int));
}
close(fd1);
fd1 = open(fifo2,O_RDONLY);
read(fd1,&a,sizeof(int));
close(fd1);
printf("%d\n",a);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int n,i,a,fd1,*arr,c=0;
char *fifo1 = "/tmp/test";
char *fifo2 = "/tmp/test2";
mkfifo(fifo1,0666);
mkfifo(fifo2,0666);
fd1 = open(fifo1,O_RDONLY);
read(fd1,&n,sizeof(int));
arr = (int*) malloc(sizeof(int)*n);
for(i=0;i<n;++i){
read(fd1,arr+i,sizeof(int));
if(arr[i]>50){
printf("%d: %d\n",i,arr[i]);
c++;
}
}
close(fd1);
fd1 = open(fifo2,O_WRONLY);
write(fd1,&c,sizeof(int));
for(i=0;i<n;++i){
if(arr[i]>50){
write(fd1,arr+i,sizeof(int));
}
}
close(fd1);
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int max=-1,a,n,fd1,i;
char *fifo1 = "/tmp/test";
char *fifo2 = "/tmp/test1";
char *fifo3 = "/tmp/test2";
mkfifo(fifo1,0666);
mkfifo(fifo2,0666);
mkfifo(fifo3,0666);
fd1 = open(fifo3,O_RDONLY);
read(fd1,&n,sizeof(int));
for(i=0;i<n;++i){
read(fd1,&a,sizeof(int));
max = max>a?max:a;
}
close(fd1);
fd1 = open(fifo2,O_WRONLY);
write(fd1,&max,sizeof(int));
close(fd1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment