Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I structure a C program in Linux to do this assignment using execvp(), fork(), dup(),open() and close() and sigaction()? in this assignment we

How do I structure a C program in Linux to do this assignment using execvp(), fork(), dup(),open() and close() and sigaction()?

in this assignment we will write a simple command, forall, that will executed a single command multiple times with different arguments. the output of each command will be directed to a file. for example:

$ ./forall wc /etc/passwd /etc/issue $ cat 1.out Executing wc /etc/passwd 44 75 2567 /etc/passwd Finished executing wc /etc/passwd exit code = 0 $ cat 2.out Executing wc /etc/issue 2 5 26 /etc/issue Finished executing wc /etc/issue exit code = 0 

the first argument will be the command to run. then for each of the rest of the arguments forall will run that command with each argument one at a time. the output (both stdout and stderr) will be redirected to a file that is created for each run (starting at 1). before running the command, the first line of the file will contain what we are going to run, and after the command finishes we print a line with the exit code.

in the above example, the first argument is the command, wc, followed by two parameters, /etc/passwd and /etc/issue. forall will execute wc /etc/passwd and redirect stdout and stderr to 1.out, when that finishes then forall will execute wc /etc/issue and redirect stdout and stderr to 2.out.

some commands might take a long time, so we need to be able to interrupt them. normally system calls are requests that the program needs to make to the kernel. what happens when the kernel needs to ask something of the program? signals are a way the kernel does this. you have used this quite often in the form of ^C. when you push ^C the kernel will send a signal to the program that it should terminate. as we will see shortly, there are some requests that the program can ignore :)

we will install a signal handler for SIGINT (this happens when you push ^C) to terminate the child rather than the forall program. when the child terminates due to a signal, that needs to be indicated in the output file (see below). since ^C terminates the child we need a way to terminate the parent. we will use SIGQUIT (^\) for that. SIGQUIT should send a SIGINT to any child that is running and then exit the forall program. here is an example of such a run:

$ ./forall sleep 100 3 200 300 ^CSignaling 6220 ^\Signaling 6223 Exiting due to quit signal $ cat 1.out Executing sleep 100 Stopped executing sleep 100 signal = 2 $ cat 2.out Executing sleep 3 Finished executing sleep 3 exit code = 0 $ cat 3.out Executing sleep 200 bcr33d@bcr33d-hp:~/Downloads$ cat 4.out cat: 4.out: No such file or directory

as you can see, whenever we signal a child to quit we print a message with the PID of the child we are signaling. in a normal run 4 files would be created and it would take a little over 600 seconds to run. in this case when we hit ^C while running the sleep 100 and we hit ^\ when running sleep 200. looking at the output we see the message about early termination due to a signal for the first run; the second terminated normally; we don't see an end message for the third; and we never created anything for the forth run because it never happened due to early termination.

objectives

this program will give you experience with fork() and exec() as well as signals (use sigaction()) and file redirection (open(), close() and dup()).

grading

10 program readable and the logical is clear

points requirement
5 program compiles
10 no memory problems (we aren't overflowing memory allocations or returning pad pointers)
10 each run of the program happens after the previous has finished (no concurrent allocations)
10 messages are all formatted correctly
10 there is an output file for each program starting at 1.out and continuing to n.out, where n is the number of different arguments to run the program with
5 at least 1000 arguments can be given to forall
10 ^C will terminate the child but not the parent
10 ^\ terminates both the parent and child.
8 exit status is output correctly in the Finished message.
8 signal received is output correctly in the Stopped message.
4 the submitted zip file has exactly one file in it called forall.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago