Question
C PROGRAMMING: The Great Ancestor Process (PLEASE WRITE IN C Language!) TASK: You will write a program much like init, although for testing you will
C PROGRAMMING: The Great Ancestor Process (PLEASE WRITE IN C Language!)
TASK:
You will write a program much like init, although for testing you will run programs which are slightly more fun than getty. You will read from a configuration file whose name must be specified on the command-line (init defaults to reading /etc/inittab, but that default would be pointless for this assignment). Your program takes an optional r parameter which indicates the run- level, which is a single character whose meaning is described below. If this parameter is not present, the run-level defaults to 3.
Use getopt() to parse the command-line, and output a fatal error message if the subsequent number of file name parameters is not exactly 1.
The configuration file has one entry per line, with fields separated by colons. Comments are introduced by # and run to the end of the line; and blank lines are permitted (that is, they must be ignored, rather than yielding an error message).
Hint:
if ((p = strchr(buf, ' ')))
*p = '\0';
if ((p = strchr(buf, ' ')))
*p = '\0';
If the line is not empty (nor is solely a comment), it contains three colon-separated fields (the real inittabs first field is omitted here):
The first field is a list of which run-levels the given process should be executed for, as a string of individual run-level characters. For example, 23 indicates that the process should be run in run- levels 2 and 3. If this first field on the line is empty, then the line applies to all run-levels. Otherwise, if the current run-level as specified by the r option is not listed, your program will discard this line after parsing.
The second field specifies the re-spawning behaviour for this process. For this assignment, the contents of this field must be either the string once, which means that the process should be executed once and your program will ignore whether or when it terminates; or the string respawn, which indicates that your program should watch for the process to terminate and then start a new one. Any other value is a fatal error.
The third field is a command-line to be executed with sh c string.
You may impose a maximum of 100 entries, but if this is exceeded you must exit with an appropriate error message rather than exceeding array bounds.
The configuration file has one entry per line, with fields separated by colons. Comments are introduced by # and run to the end of the line; and blank lines are permitted (that is, they must be ignored, rather than yielding an error message).
Your subprocesses MUST run in parallel. If the inittab lists commands "cmd1" and "cmd2" (on separate lines), you can't run cmd1 and wait for it to finish and then run cmd2. You need to start all of the commands in the file before doing any wait() calls.
(input file)inittab:
# Example inittab, running various silly programs
:once:echo Hello, inittab-using world # This comment is not output 4:once:echo Aha, run-level four! :respawn:listener -p 1487 /bin/echo $USER says that this is the end of the internet. \ Please turn around.
12:respawn:333 123:respawn:222
# enable 'biff' only on , not a workstation, and send yourself some e-mail #12:respawn:111
The program should output as follow: $ ./myinit -r12 inittab
output: ./myinit: runlevel must be a single character
usage: ./myinit [-r runlevel] file
$./myinit -r4 inittab
Hello, inittab-using world
Aha, run-level four!
^C (control+c to exit)
$./myinit -r1 inittab
Hello, inittab-using world
^C
NOTE: ":once:echo Hello" means "Hello" only output once where ":respawn:echo Hello" means "Hello" will output forever until user press "control + c" to exit.
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