Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Design a file-copying program using ordinary pipes in C. This program named filecopy will be passed two parameters: the name of the file to be

Design a file-copying program using ordinary pipes in C. This program named filecopy will be passed two parameters: the name of the file to be copied and the name of the copied file. The program will then create an ordinary pipe and write the contents of the file to be copied to the pipe. The child process will read this file from the pipe and write it to the destination file. For example, if we invoke the program as follows: filecopy input.txt copy.txt

the file input.txt will be written to the pipe. The child process will read the contents of this file and write it to the destination file copy.txt. You may write this program using either UNIX or Windows pipes. Step1: Create pipe file descriptor Step2: Set up the pipe, open both input and output files. Make sure your program closes file handles.

Step3: Create the processes. Read from the input file in parent process and write to the pipe. Write to the output file in the child process. Make sure your program closes file handles.

Step4: Add a timestamp in the child process to record the time when the reading-writing is done. Print the timestamp to console output.

C-code outline

/********************************************************************/ #include

#include #include #include

#define READ_END 0 #define WRITE_END 1

int main(int argc, char *argv[]) { int rv; pid_t pid; int c; char rb[2], wb[2]; /* bytes for reading/writing */ int ffd[2]; /* file descriptor */ /* Step1: Create pipe descriptor */ pipe(ffd); //??

/* open the input file */ ffd[READ_END] = open(argv[1], O_RDONLY); if (ffd[READ_END] < 0) { fprintf(stderr,"Unable to open %s ",argv[1]); return 1; } /* open the output file */ ffd[1] = open(argv[2], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);

if (ffd[1] < 0) { fprintf(stderr,"Unable to open %s ",argv[2]);

/* close the input file */ close(ffd[0]);

return 1; }

/* Step2: set up the pipe */

/* make sure your program closes file handles*/

/* Step3: create the processes */

/* read from the input file and write to the pipe */

/* read from the pipe and write to the output file */

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions