Write a C program that spawns and controls multiple processes. The program shoul
ID: 3693115 • Letter: W
Question
Write a C program that spawns and controls multiple processes. The program should use system calls such as fork(), wait(), execvp() to manage the processes. The program should use signal system calls to communicate between the processes. The processes should coordinate a simple command structure where the main process is "base" and child processes are "planes".
The base process is the main process. It should run in a simple loop, prompting the user for input command. Valid input commands include "launch", "bomb N", "status", "refuel N", and "quit". Invalid commands should produce a suitable error message. Upon receiving the command to quit, the program should end.
The launch command should cause a child process to start. All child processes should execute the same code. A child process should execute a simple loop counting "fuel" downward from 100 at a rate of 5 fuel/second. Every 3 seconds it should report its fuel status by printing out the line "Bomber N to base,# fuel left", where N is the child's process ID. Upon receiving the signal SIGUSR1, the child process should print out the line "Bomber N to base, bombs away!" where N is the child's process ID. Upon receiving the signal SIGUSR2, the child process should " refuel," resetting its fuel value to 100. Upon reaching zero fuel, the child process should send the signal SIGUSR2 to the main process, and then exit.
The main process must maintain a list of process IDs of the child processes. Given the bomb N command, the main process should send the signal SIGUSR1 to child process ID = N. Given the refuel N command, the main process should send the signal SIGUSR2 to child process ID = N. Given the status command, the main process should list the child processes IDs. Upon receiving the SIGUSR2 signal, the main process should print out "SOS!" Plane has crashed!" The main process will not know which child process sent the signal, so if this happens the status list will thereafter be in error.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#define FUEL_MAX (100)
typedef enum {
GROUND,
OUT,
BOMB_AWAY,
IN,
MAX
} STATE;
char *state_text[] = {
"gound",
"out",
"bomb away",
"in",
""
};
STATE State;
char *id;
int fuel;
static void mySigHand(
int sig
){
switch(sig){
case SIGUSR1:
State = BOMB_AWAY;
printf("Bomber %s to base, bombs away... coming home! ", id);
return;
case SIGUSR2:
fuel = FUEL_MAX;
printf("Bomber %s to base, max fuel=%d ", id, fuel);
return;
default:
printf("Bomber %s to base, unknown command ", id);
return;
}
}
int main(
int argc,
char **argv
){
unsigned int tim;
int parent;
if (3 < argc) {
printf("Bomber ?? to base, ID unknown... Exiting... ");
return 100;
}
id = argv[1];
parent = atoi(argv[2]);
if (SIG_ERR == signal(SIGUSR1, mySigHand)) {
printf("Bomber %s to base, cannot turn on USR1 channel Exiting... ", id);
return 11;
}
if (SIG_ERR == signal(SIGUSR2, mySigHand)) {
printf("Bomber %s to base, cannot turn on USR2 channel Exiting... ", id);
return 12;
}
printf("Status: Bomber %s to base, taking off... ", id);
for(fuel=FUEL_MAX, State=OUT, tim=0;
0 < fuel && (OUT == State || BOMB_AWAY == State);
++tim, fuel-=5, sleep(1)){
if (!(tim%3)) printf("Status: Bomber %s to base, Status=%s, Fuel=%d... ",
id,state_text[State],fuel);
}
printf("Status: Bomber %s to base, Status=%s, Fuel=%d... ", id,state_text[State],fuel);
kill(parent, SIGUSR2);
return (BOMB_AWAY != State);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.