Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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. 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.

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