Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that accepts an integer N as a command line input and then proce

ID: 3908741 • Letter: W

Question

Write a program that accepts an integer N as a command line input and then proceeds to launch N processes on your system. Each process should consist of an infinite loop that prints out “Process k running” (once every second) where k is a process index (from 0 to N-1). After 5 seconds of running, your main program should kill process 0, then after another 5 seconds, should kill process 1, and so on until all processes have been terminated. At that time, your main program should itself terminate.solution to these problems should be written in C or C++ for Linux.

Explanation / Answer

Answer:

Here, I have following program code as shown below

Code:

// define a header file

#include <stdlib.h>

#include <unistd.h>

#include <iostream>

#include <signal.h>

using namespace std;

const int arraySIZE = 10;

int pArray[arraySIZE];

int pt = 0;

// method definition

void infiniteLoop(int n){

int t =0;

while(t - 6){

cout << " Process : " << n << " Running " << endl;

sleep(1);

t++;

}

}

// method definition

void creating_Process()

{

pid_t Pid = fork();

if (Pid == 0) {

cout << "I am child Process.." << endl;

infiniteLoop(int(getpid()));

exit(0);

}

else if (Pid < 0){

cout << "Failed to fork!" << endl;

exit(-1);

}

else {

cout << "Parent Process : " << getpid() << endl;

pArray[pt] = int(getpid());

pt++;

}

}

// method definition

void Processkilled(int n){

int PID = n;

// display

cout << "The Following Parent pid of the process is about to killed is : " << PID << endl;

kill(PID,SIGTERM);

}

// Main function

int main(int argc, char* argv[]) {

int num = strtol(argv[1], NULL, 10);

for (int i = 0; i < num; i++) {

creating_Process();

sleep(1);

}

sleep(5);

for (int i = 0; i < arraySIZE;i++) {

int temp = pArray[i];

Processkilled(temp);

sleep(5);

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote