Help needed Copy a file using pipes A parent process creates
Help needed!
Copy a file using pipes. A parent process creates a pipe then forks a child process. The parent process then opens and reads the input file, that was passed as an argument, and writes the data to the upstream of a pipe. The child process then opens the output file, reads data from the pipe and
writes it to the output file. When done, the parent process closes the upstream and the child closes downstream. Modify the code given below:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFER 512
#define PERM 0644
int copyfile(const char *name1, const char *name2)
{
int inp, outp;
ssize_t reader;
char buffer[BUFFER];
// Open File 1 for reading
if((inp = open(name1, O_RDONLY)) == -1)
{
printf(\"Error opening file: %s\ \", name1);
return(-1);
}
// Open File 2 for writing/appending
if((outp = open(name2, O_WRONLY |O_CREAT|O_APPEND, PERM)) == -1)
{
printf(\"Error opening file: %s\ \", name2);
return(-1);
}
// Copy
while((reader = read(inp, buffer, BUFFER)) > 0)
{
if(write(outp, buffer, reader) < reader)
{
close(inp);
close(outp);
return(-2);
}
}
close(inp);
close(outp);
if(reader == -1)
return(-4);
else
return(0);
}
/* Main() */
int main(int argc, char *argv[])
{
if( argc != 3) //Wrong number of command line arguments
{
printf(\"Usage: sampleCP file1 file2\ \");
return 1;
}
copyfile(argv[1], argv[2]);
return 0;
}
Note:
Must use any of the functions given below:
int fd[2];
int pipe(int filedes[2]);
int open(const char*path, int oflags);
int open(const char *path, int oflags, mode_t mode);
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
program:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFER 50
int readfile(const char *,int); //prototype of the function to be invoked by child process
int writefile(const char *,int); //prototype of the function to be invoked by parent process
int readfile(const char *name1, int fd) //this part is executed by child process only.
{ //Here, child process reads from downstream of pipe and writes the content into destination file (argv[2])
int inp;
char buffer[BUFFER];
read(fd, buffer, BUFFER); //reading from downstream (fd) and storing the read content in an array buffer.
if((inp = open(name1, O_WRONLY|O_CREAT,0666)) == -1)
{ //opening the destination file to write the content read from downstream of pipe.
printf(\"Error opening file: %s\ \", name1); //if failed to open a file, an error is displayed
return(-1);
}
write(inp,buffer,BUFFER); //writing into opened destination file
close(inp); //closing the opened destination file
close(fd); //closing the downstream of pipe
return 1;
}
int writefile(const char *name2, int fd) //this part is executed by parent process only
{ //here, parent process reads content from source file (name2) and writes that into the upstream of pipe.
int outp;
char buffer1[BUFFER];
if((outp = open(name2, O_RDONLY)) == -1)
{ //opening the source file for reading the content
printf(\"Error opening file: %s\ \", name2);
return(-1);
}
read(outp, buffer1, BUFFER);
//the read content is stored in the array buffer1.
write(fd,buffer1,BUFFER); //writing the content in buffer1 to the upstream of pipe.
close(outp); //closing the source file
close(fd); //closing the upstream
return 1;
}
/* Main() */
int main(int argc, char *argv[])
{
int fd[2],pid;
if( argc != 3) //Wrong number of command line arguments
{
printf(\"Usage: sampleCP file1 file2\ \");
return 1;
}
pipe(fd); //creating a pipe
pid=fork(); //creating a child process
if(pid<0)
{
printf(\"Fork failed\");
return 1;
}
else if(pid==0)
{ //child process
close(fd[1]); //closing the upstream as it is not needed by child
readfile(argv[2],fd[0]); //invoking the readfile
}
else
{ //parent process
close(fd[0]); //closing the downstream as it is not needed by parent
writefile(argv[1],fd[1]); //invoking the writefile
}
return 0;
}
PS: 1. I need to append into file 2 if it already exists and 2. when i compiled it there are some garbage values written into file2. can u give me a soln for this? when i browsed i found that i can solve it by passing same no of bytes in write() call as u have used to read in read() call. can u ls help out with these two doubts? Can u please help me with the program:
Solution
Recursively, when new process forked, we want the connection evolve to:
So we need another pipe between C1 and C2then redirect the I/O.
Program:
void main()
{
char buffer[100];
char childbuff[100];
pipe (fd);
/*parent process closes the downstream*/
close(fd[0]); /* reads the file*/
write(fd[1], buffer, bytes);
} else {
/* child process closes the updtream*/
close(fd[1]); /*reads from the pipe*/



