Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Demo.cpp args.cpp Goals By completing this assignment, you will know how to develop a program in the Linux platform and to make system

C++ Programming

image text in transcribedimage text in transcribedimage text in transcribed

Demo.cpp

image text in transcribed

args.cpp

image text in transcribed

Goals By completing this assignment, you will know how to develop a program in the Linux platform and to make system calls related to process management. Introduction You need to implement a C++ program called BP that allows user to run multiple processes (up to three at the same time) at the background. When three processes are currently running, further execution request will be pended (the process state is changed to "stopped") and wait until another process is stopped or terminated. While processes are running at the background, the user can input command to display information of the background processes, stop or kill a background process. Requirements 1. Your BP needs to show a prompt for user input as follows. $ ./BP BP > 2. BP accepts the following commands from the user and takes the corresponding action. BP >bg [name of executable file] [a list of arguments] Action: BP runs the executable file with a list of arguments at the background and continues to accept input from the user. If there are already 3 running processes, the process is stopped. Example: BP runs the executable file demol with a list of arguments): running 2 5 at the background and continues to accept input from the user. BP >bg demol running 2 5 BP > BP >bglist Action: Display the process id(s), name(s) and the state(s) of ALL background processes. Example: BP >bglist 16529: demol (running) 16605: demo2 (stopped) 16613: demo 3 (terminated) BP >bgstop [pid] Action: Stop the process with process id pid and display a message. If there exists another stopped process (e.g. stopped earlier because there're already 4 running processes), the earliest process in stopped state (creation order, not runtime order) should be automatically restarted. Example: BP >bgstop 16529 16529 stopped 16624 automatically restarted BP >bgkili [pid] Action: Terminate the process with process id pid and display a message. Similar to bgstop, if there exists another stopped process, the earliest process (creation order, not runtime order) in stopped state should be automatically restarted. Example: BP >bgkill 16529 16529 killed BP >exit Action: BP executes bgkill to terminate all background processes, if any, and exits. Example: BP >exit 16605 killed 16607 killed 3. BP should display a message after a background process has completed. Example: 16529 completed 4. You may assume that the syntax of the input commands and pids are valid, but BP needs to handle redundant commands (e.g.bgkill a process which is already terminated) by displaying a message. Examples: 16529 already stopped 16529 already terminated 16529 does not exist Hints Use fork() and execvp() so that the parent process accepts user input and the child process executes the background process. When you use fork(), it is important that you do not create a fork bomb, which easily eats up all the resources allocated to you. Use waitpid () with an option WNOHANG to check if a background process has completed. Use kill() to send a signal to a process, e.g., a SIGTERM signal to terminate a process. Do note that kill() can also be used to stop / resume a process, regardless of it's name. Study the man pages of the system calls used in your program. For example, the following command displays the man pages of kill() in Section 2. $ man 2 kill Helper programs demo.cpp This demo program can be used to act as a background process for testing your BP as its execution can be visualized by displaying a word every few seconds a number of times. This program takes three arguments, word, interval and times. The first argument word is a single word to be displayed repeatedly. The second argument interval is the number of seconds between two consecutive displays of the word. The third argument times is the number of times the word to be displayed. For example, the following command displays the word "running" 5 times in 2-second interval. $ demo running 2 5 args.cpp This example program shows how to read a line from terminal, as well as parsing (cutting) the string using the strtok () function. To compile the program, use the following command. $ g++ args.cpp -lreadline -o args Marking Marking scheme (total: 100%): obg obglist o bgstop bgkill exit O Correctly having 3 processes running at the same time (not 2 or 4...) o Correctly restart another process upon stop/termination o Handling of redundant calls (e.g. stop a stopped process) o programming style and in-program comments OOOOO 20% 10% 10% 10% 10% 10% 10% 10% 10% #include #include #include on m+ in No using namespace std; int Omain(int argc, char* argv[]), - - 10 11 - - - - int i; const char* word = argv[1]; int interval = atoi(argv[2]); if (argc == 4) - 12 - - 13 - - - 14 - - - 15 - - int times = atoi(argv[3]); for (i=0; i #include #include #include using namespace std; Qint main(){ char *input = NULL; char prompt[] = "Your command:"; input = readline("Your command: "); cout or 2. BP accepts the following commands from the user and takes the corresponding action. BP >bg [name of executable file] [a list of arguments] Action: BP runs the executable file with a list of arguments at the background and continues to accept input from the user. If there are already 3 running processes, the process is stopped. Example: BP runs the executable file demol with a list of arguments): running 2 5 at the background and continues to accept input from the user. BP >bg demol running 2 5 BP > BP >bglist Action: Display the process id(s), name(s) and the state(s) of ALL background processes. Example: BP >bglist 16529: demol (running) 16605: demo2 (stopped) 16613: demo 3 (terminated) BP >bgstop [pid] Action: Stop the process with process id pid and display a message. If there exists another stopped process (e.g. stopped earlier because there're already 4 running processes), the earliest process in stopped state (creation order, not runtime order) should be automatically restarted. Example: BP >bgstop 16529 16529 stopped 16624 automatically restarted BP >bgkili [pid] Action: Terminate the process with process id pid and display a message. Similar to bgstop, if there exists another stopped process, the earliest process (creation order, not runtime order) in stopped state should be automatically restarted. Example: BP >bgkill 16529 16529 killed BP >exit Action: BP executes bgkill to terminate all background processes, if any, and exits. Example: BP >exit 16605 killed 16607 killed 3. BP should display a message after a background process has completed. Example: 16529 completed 4. You may assume that the syntax of the input commands and pids are valid, but BP needs to handle redundant commands (e.g.bgkill a process which is already terminated) by displaying a message. Examples: 16529 already stopped 16529 already terminated 16529 does not exist Hints Use fork() and execvp() so that the parent process accepts user input and the child process executes the background process. When you use fork(), it is important that you do not create a fork bomb, which easily eats up all the resources allocated to you. Use waitpid () with an option WNOHANG to check if a background process has completed. Use kill() to send a signal to a process, e.g., a SIGTERM signal to terminate a process. Do note that kill() can also be used to stop / resume a process, regardless of it's name. Study the man pages of the system calls used in your program. For example, the following command displays the man pages of kill() in Section 2. $ man 2 kill Helper programs demo.cpp This demo program can be used to act as a background process for testing your BP as its execution can be visualized by displaying a word every few seconds a number of times. This program takes three arguments, word, interval and times. The first argument word is a single word to be displayed repeatedly. The second argument interval is the number of seconds between two consecutive displays of the word. The third argument times is the number of times the word to be displayed. For example, the following command displays the word "running" 5 times in 2-second interval. $ demo running 2 5 args.cpp This example program shows how to read a line from terminal, as well as parsing (cutting) the string using the strtok () function. To compile the program, use the following command. $ g++ args.cpp -lreadline -o args Marking Marking scheme (total: 100%): obg obglist o bgstop bgkill exit O Correctly having 3 processes running at the same time (not 2 or 4...) o Correctly restart another process upon stop/termination o Handling of redundant calls (e.g. stop a stopped process) o programming style and in-program comments OOOOO 20% 10% 10% 10% 10% 10% 10% 10% 10% #include #include #include on m+ in No using namespace std; int Omain(int argc, char* argv[]), - - 10 11 - - - - int i; const char* word = argv[1]; int interval = atoi(argv[2]); if (argc == 4) - 12 - - 13 - - - 14 - - - 15 - - int times = atoi(argv[3]); for (i=0; i #include #include #include using namespace std; Qint main(){ char *input = NULL; char prompt[] = "Your command:"; input = readline("Your command: "); cout or

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

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions