Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C PROGRAMMING: Write a c program which does the following: the program is much like init and You will read from a configuration file whose

C PROGRAMMING:

Write a c program which does the following: the program is much like "init" and 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).

The config file will look like this(also provided below with output): (run-level):(once/respawn):(command-line to be executed)

i.e.config file

:once:echo Hello, inittab-using world # This comment is not output

4:respawn:echo Aha, run-level four! :respawn:(listener)

and if you $gcc -Wall myinit.c

$./a.out -r4 [runlevel] config_file

$Hello, inittab-using world

Aha, run-level four!

Aha, run-level four!

....

("Aha, run-level four!" will continue to output until user press control + c to exit)

REQ: The program is to read from the config file and if there is a run-level execute the given process at the level if there is no run-level, then the line applies to all run-levels. (execute all run-levels starts with ':')

and if there is a 'once' after the first ':' just output the command-line to be executed with sh ?c string after the second ":" once;

or if there is a 'respawn' after the first ':' just keep outputting the command-line to be executed with sh ?c string after the second ":" until user press control+c to exit;

and the 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;

also ignore the line starts with "#";

)

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

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 -r inittab

./myinit: runlevel must be a single character

usage: ./myinit [-r runlevel] file

$./myinit inittab

Hello, inittab-using world

^C

$./myinit -r1 inittab

Hello, inittab-using world

(nothing else will be outputed here)

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. Ignore the lines started with "#" treated as comments.

example config file:

$ cat test.txt

:once:echo 1 hello #asfff

:respawn:echo 2 yoyo

4:once:echo run-level

3:once:234 * 111

2:once:echo 111111

#wdasdasdasdasdasdasdad

$ ./myinit -r2 test.txt

1 hello

111111

2 yoyo

2 yoyo

2 yoyo

......

(control + c to exit)

$./myinit -r3 test.txt

1 hello

sh: 1: 2 yoyo

234: not found

2 yoyo

2 yoyo

2 yoyo

2 yoyo

........

(control + c to exit)

$./myinit test.txt

1 hello

2 yoyo

sh: 1: 234: not found

2 yoyo

2 yoyo

2 yoyo

......

(control + c to exit)

$./myinit -r4 test.txt

1 hello

2 yoyo

run-level

2 yoyo

2 yoyo

2 yoyo

(control + c to exit)

another expamle output: (config file)$ cat test1.txt

:once:echo 1 hello #asfff

4:once:echo run-level

3:once:234 * 111

2:once:echo 111111

#wdasdasdasdasdasdasdad

:once: echo 333

:once:sleep 5

$ ./myinit -r test1.txt

./myinit: runlevel must be a single character

usage: ./myinit [-r runlevel] file

$ ./myinit -r2 test1.txt

user:/Desktop/cprogram$ 1 hello

111111

333

(control + c to exit)

$./myinit test1.txt

user:/Desktop/cprogram$ 333

sh: 1: 234: not found

1 hello

(control + c to exit)

$./myinit -r4 test1.txt

user:/Desktop/cprogram$1 hello

run-level

333

(control + c to exit)

$./myinit -r9 test1.txt

user:/Desktop/cprogram$1 hello

333

(control + c to exit)

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

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

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions