ans = 1. -->exec('Example16.sci') -->clear -->flag=1 flag = 1. -->mode(-1) Current date is 23-Jun-2013 Welcome to the Textbook Companionship Project 2013 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Book Title : UNIX CONCEPTS AND APPLICATIONS Book Edition : 4 Book Author : Sumitabha Das +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Code Author : Pranav Bhat T +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chapter Number : 24 Chapter Title : Systems programming II- Files +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Example 16 : Show the method of using fork and exec to run a user-defined program and kill it in 5 seconds if not completed **************************************************************** Answer : INSTRUCTIONS : 1.These programs are part of systems programming PURELY in Unix and the commands have NO EQUIVALENT IN SCILAB 2.However the .c files which are displayed here are also made into a seperate file.If you are a unix user then try compiling and running the programme with gcc or cc compiler 3.The outputs displayed here are just MOCK OUTPUTS which are DISPLAYED IN THE TEXTBOOK 4.The inconvenience is regretted. .............Press [ENTER] to continue..... UNIX SHELL SIMULATOR(DEMO VERSION WITH PRELOADED COMMANDS) $ cat killprocess.c # to open the file emp.lst /* Program: killprocess.c -- Uses fork and exec to run a user-defined program and kills it if it doesnt complete in 5 seconds */ #include #include #include #include pid_t pid; int main(int argc, char **argv) { int i,status; void death_handler(int signo); /* A common signal handler this time */ signal(SIGCHLD, death_handler); /* death_handler is invoked when a */ signal(SIGALRM, death_handler); /* child dies or an alarm is recieved */ switch (pid = fork()) { case -1: printf("Fork error\n"); case 0: execvp(argv[1],&argv[1]); /* Execute command */ perror("exec"); break; default: alarm(5); /* Will send SIGALRM after 5 seconds */ pause(); /* Will return when SIGCHILD signal is received */ printf("Parent dies\n"); } exit(0); } void death_handler(int signo) { /* This common handler pics up the */ int status; /* exit status for normal termination */ signal(signo, death_handler); /* but sends the SIGTERM signal if */ switch(signo) { /* command doesnt complete in 5 seconds */ case SIGCHLD: waitpid(-1, &status, 0); /* Same as wait(&status); */ printf("Child dies; exit status: %d\n",WEXITSTATUS(status)); break; case SIGALRM: if (kill(pid, SIGTERM) == 0) fprintf(stderr, "5 seconds over,child killed\n"); } } $ cc killprocess.c $ a.out date Sat Jun 20 22:29:27 IST 2013 Child dies: exit status: 0 Parent dies $ a.out find /home -name a.out -print /home /sumit/personal/project8/a.out /home/sumit/personal/books_code/glass_ables/12/a.out /home/sumit/personal/books_code/stevens_c/ch08/a.out ..after 5 second time interval ... 5 seconds over, child killed Parent dies $ exit #To exit the current simulation terminal and return to Scilab console ........# (hit [ENTER] for result) BACK TO SCILAB CONSOLE... Loading initial environment -->diary(0)