Question
Using C language,Write a program that takes a filename as a command-line argument and performs the following steps: 1. Open the file specified as the
Using C language,Write a program that takes a filename as a command-line argument and performs the following steps:
1. Open the file specified as the command-line argument.
2. Read contents of the file one-line at a time (you can use createarray4.c and readline.c from Chapter 5).
3. Use fork-exec to create a new process that executes the program specified in the input file along with the arguments provided.
4. The parent process will make note of the time the program was started (you can use a timer such as the time function defined in to capture the time before the fork method is invoked, you can find out more about time function by typing man time).
5.Then the parent process will wait for the child process to complete and when the child process terminates successfully, the parent process will capture the time the child process completed (you can again use a timer function to capture the time when the wait function returns).
6.Open a log file (say, output.log) and write the command executed along with arguments, start time of the process, and the end time of the process separated by tabs. Use ctime function to write the time in a human readable form.
7.Go to step 2 and repeat the above process for very command in the input file.
Please use standard I/O library function calls for all I/O operations in this assignment. Make sure you open the file in the appropriate modes for reading and writing and also make sure to close the file, especially when you are writing the file.
Here is a sample input file:
uname -a
/sbin/ifconfig
./hw1 500
./hw1 1000
Here is the corresponding sample output log file:
uname -a Thu Oct 10 17:43:44 2019 Thu Oct 10 17:43:44 2019
/sbin/ifconfig Thu Oct 10 17:43:44 2019 Thu Oct 10 17:43:44 2019
./hw1 500 Thu Oct 10 17:43:45 2019 Thu Oct 10 17:43:46 2019
./hw1 1000 Thu Oct 10 17:43:46 2019 Thu Oct 10 17:43:57 2019
You can download the C program hw1.c here: hw1.c. Compile the program using the following command:
$ gcc -Wall -O -o hw1 hw1.c
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started