PHP Code:
#include <st***.h>
#include <stdlib.h>
#include <signal.h>
/*
* _ _
* (_) (_)
* __ ____ _ _ _____ ___ ___ ___ ___ _ __ ___
* \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
* > < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
* /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
* __/ |
* |___/
*
* Giovanni Di Grezia - 0512101xxx
*
* ////// SECONDA PROVA INTERCORSO //////
*
* Advanced Edition
*
*
*
*/
int fibonacci ( int i) {
if ( i <= 1){ return 0;} else { if ( i == 2) { return 1; } else {
return (fibonacci( i - 1) + fibonacci ( i - 2));
}
}
}
void null_action (int signal) {}
void action ( int signal) {
int pid,status; char character;
pid = fork();
if ( pid == -1) { exit(-2);} else {
if ( pid == 0) {
fprintf ( stdout, "Pid padre %d \n", getppid());
fprintf(stdout, "Terminare?\n");
fscanf(stdin,"%c",&character);
if (character == 'y'){
kill (getppid(),SIGKILL);
kill (getpid(),SIGKILL);
} else {
kill ( getppid(),SIGCONT);
}
exit(0);
} else {
kill ( getpid(),SIGSTOP);
waitpid(-1,&status,0);
}
}
}
int main ( void ){
int pid,i,status,err;
struct sigaction sigintaction;
sigintaction.sa_handler= null_action;
sigintaction.sa_flags=0;
sigaction (SIGINT, &sigintaction, NULL);
pid=fork();
if ( pid == -1 ) { /* errore */ exit(-1);} else {
if ( pid == 0){
sigintaction.sa_handler= action;
sigaction (SIGINT, &sigintaction, NULL);
struct sigaction sigusr1action;
sigusr1action.sa_handler= null_action;
sigusr1action.sa_flags=0;
sigaction (SIGUSR1, &sigusr1action, NULL);
for (i=0;i<300;i++){
fprintf(stdout,"Fib %d\n", fibonacci(i+1));
}
} else {
err= waitpid(-1,&status,0);
for (;err != pid;){
err= waitpid(-1,&status,0);
}
fprintf(stdout,"Figlio con pid %d terminato\n",pid);
}
}
return 0;
}