/////////////////////////////////////////////////////////////////////////////////////// // File : init.c // Date : January 2018 // Author : Alain Greiner /////////////////////////////////////////////////////////////////////////////////////// // This single thread application implement the "init" process for ALMOS-MKH. // It uses the fork/exec syscalls to create N KSH child processes // (one child process per user terminal). // It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter. // // TODO : Register the PIDs for all KSH[i] in a ksh_pid[] array. // Then calls the wait() function to block, and reactivate any child KSH process // that has been deleted, using a new fork/exec. /////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #define DELAY_BETWEEN_FORK 100000 ////////// int main() { int i; int ret_fork; // fork return value int ret_exec; // fork return value int received_pid; // pid received from the wait syscall int status; // used by the wait syscall char string[64]; // check number of TXT channels assert( NB_TXT_CHANNELS > 1 ); // create the KSH processes (one per user terminal) for( i = 1 ; i < NB_TXT_CHANNELS ; i++ ) { // INIT process fork process CHILD[i] ret_fork = fork(); if( ret_fork< 0 ) // error in fork { // INIT display error message on TXT0 terminal snprintf( string , 64 , "INIT cannot fork child[%d]\n" , i ); display_string( string ); // INIT exit exit( 0 ); } else if( ret_fork == 0 ) // we are in CHILD[i] process { // CHILD[i] process exec process KSH[i] ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); if ( ret_exec ) // error in exec { // display error message on TXT0 terminal snprintf( string , 64 , "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d\n" , i , i , ret_exec ); display_string( string ); } } else // we are in INIT process { // INIT display CHILD[i] process PID snprintf( string , 64 , "INIT forked CHILD[%d] / pid = %x\n", i , ret_fork ); display_string( string ); } } // This loop detects the termination of the KSH[i] processes, // to recreate these process when required. while( 1 ) { // block on child processes termination received_pid = wait( &status ); if( WIFSTOPPED( status ) ) // stopped => unblock it { // display string to report unexpected KSH process block snprintf( string , 64 , "KSH process %x unexpectedly stopped" , received_pid ); display_string( string ); } if( WIFSIGNALED( status ) || WIFEXITED( status ) ) // killed => recreate it { // display string to report unexpected KSH process termination snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid ); display_string( string ); } } } // end main()