Description: This c program takes 2 strings as Command Line Arguments c from the console/terminal. These 2 strings are the names of input and output files respectively. Then, fork() is done making 2 processes running parallel, side-by-side. In the child process, the input file is read and the text is put on the writing end of the pipe. After that, the parent process reads the reading end of the pipe and place the whole text in a new output file.
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char *argv[]){
char input_file[25],output_file[25],ch,ch1,data[1000], data1[1000];
strcpy(output_file,argv[2]);
strcpy(input_file,argv[1]);
int fd[2];
pipe(fd);
pid_t
pid;
pid=fork();
if(pid>0){
FILE* fp;
fp=fopen(input_file,"r");
int i=0;
ch=getc(fp);
while(ch!=EOF){
data[i++]=ch;
ch=getc(fp);
}
data[i]='\0';
close(fd[0]);
write(fd[1],data,strlen(data)+1);
close(fd[1]);
}
else{
close(fd[1]);
read(fd[0],data1,999);
close(fd[0]);
FILE* fp1;
fp1=fopen(output_file,"w");
fprintf(fp1,"%s",data1);
}
return 0;
}
No comments
Post a Comment