J'ai une fonction bomb() qui devrait tuer un processus fils.Processus de contrôle à partir du terminal dans C
void bomb(id){
kill(id, SIGKILL);
printf("%d is ruined!", id);
}
Il doit être invoqué par l'entrée des utilisateurs dans le terminal comme « bombe 2355 » (où 2355 est pid du processus de l'enfant). COMMENT FAIRE ÇA?
Je suis aussi coincé avec autre chose. Comment ajouter tous les PID de processus enfants à un tableau lancé [10]? Donc, qu'il pourrait être accessible si commande == "statut"?
Je suis nouveau à C .. Cherché Internet partout :(Le code complet si ci-dessous. apprécierait toute aide! Merci!
char planes();
void plane_function();
void refuel();
void bomb(int id);
void handle_signal(int signo);
char command[12];
int launched[10];
int main()
{
planes();
return 0;
}
char planes(){
printf("Enter a command: ");
scanf("%s", command);
pid_t main_process; //main process created
//struct sigaction sa; //handling signals
//printf("My pid is: %d\n", getpid());
if (strcmp(command, "launch") == 0){
main_process = fork();
if (main_process == 0){
printf ("the main process ID is %d\n", getppid()); // main_process ID
printf ("the new plane ID is %d\n", getpid()); // child ID
//printf("launched: %d", launched[n]);
launched[0] = getpid();
plane_function(launched[0], main_process);
}
else
{
//printf("Parent");
}
}
else if (strcmp(command, ("bomb")) == 0){ // how to access a PID
printf("Bomb N\n");
bomb(plane_id);
}
else if (strcmp(command, "refuel") == 0){
printf("Refuel N\n");
}
else if (strcmp(command, "status") == 0){
printf("STATUS: \n");
printf("Planes launched: \n");
printf("%d\n ", launched[0]);
}
else if (strcmp(command, "quit") == 0){
printf("Quit\n");
}
else {
printf("Error! Wrong command!\n");
}
planes();
return 0;
}
void plane_function(id) {
int fuel = 100;
while (fuel >= 15){
sleep(3);
fuel = fuel - 15;
printf("Bomber %d to base. Fuel left: %d\n", id, fuel);
if(fuel == 10){
printf("%d, you're done kid\n", id);
kill(id, SIGKILL);
}
}
}
void bomb(id){
kill(id, SIGKILL);
printf("%d is ruined!", id);
}
void handle_signal(int signo){
const char *signal_name;
sigset_t pending;
printf("SIGNAL!!!!");
if (signo == SIGUSR1)
printf("received SIGUSR1\n");
else if (signo == SIGKILL)
printf("received SIGKILL\n");
}
Quel est le problème en utilisant 'kill'? –
Olaf
Le processus devrait être tué par la commande du terminal comme "bombe N" où N est pid, pas "tuer N". – Ria
Si vous voulez juste utiliser un nom différent, utilisez un alias. – Olaf