3. Copy a File Using Pipes: Implement a command (lastnameCP) that copies one fil
ID: 3601601 • Letter: 3
Question
3. Copy a File Using Pipes: Implement a command (lastnameCP) that copies one file (source) to another (destination) using pipes. This is a lab in C
Hint: A possible solution:
- Parent process creates a pipe then forks a child process:
The parent process opens and reads the input file, that was passed as an argument, and writes the data to the upstream end of a pipe.
The child process 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.
Explanation / Answer
**************************************************CODE********************************************************************
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
//defining the buffer size
#define SIZE_OF_BUFFER 50
int main( int argc, char* argv[] ) {
int fpipe[2];
pid_t child_id;
//buffer to read the input file
char readBuffer[SIZE_OF_BUFFER];
//creating a pipe
pipe(fpipe);
//checking for valid number of parameters entered
if( argc != 3 ) {
printf("ERROR: Enter 2 parameters only. ");
exit(1);
}
//Source file received as argument 1
int sourceFile = open(argv[1], 0);
//Destination file received as argument 2,if file does not exist it will create one
int destinationFile = open(argv[2], O_RDWR|O_CREAT|O_APPEND, 0666);
//check if file is able to open or not
if (sourceFile == -1 || destinationFile == -1) {
printf("ERROR: Couldnt open file ");
exit(1);
}
//fork the child
child_id = fork();
if (child_id == 0) {
// inside the child prcocess
close(fpipe[1]);
while (read(fpipe[0], readBuffer, sizeof(readBuffer)) > 0) {
// Writing to the destination file from pipe
write(destinationFile, readBuffer, strlen(readBuffer) - 1);
}
close(fpipe[0]);
close(destinationFile);
}
else {
// inside the parent process
close(fpipe[0]);
// code to read from a text file and store in pipe
while (read(sourceFile, readBuffer, sizeof(readBuffer)) > 0) {
write(fpipe[1], readBuffer, sizeof(readBuffer));
memset(readBuffer, 0, SIZE_OF_BUFFER);
}
close(fpipe[1]);
close(sourceFile);
wait(NULL);
}
}
***************************************************CODE*********************************************************************
To create child process we use fork().
fork() returns :
a) <0 failed to create child process
b) =0 for child process
c) >0 parent process will execute.
pipe() is used for passing information from one process to another. It can be used in 2 way communication.
fpipe[0] is one end of pipe(read end) and fpipe[1] is another end(write end).
**********HOW TO RUN***************
copy the above code and store in .c file
first create an inputfile.txt with some message at the same location of this C file
run the following in ubuntu terminal
gcc filename.c
./a.out inputfile.txt outputfile.txt
check the output file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.