/////////////////////////////////////////////////////////////////////////////////////// // 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++ ) { snprintf( string , 64 , "@@@ before fork / iter = %d\n" , i ); display_string( string ); // INIT process fork process CHILD[i] ret_fork = fork(); snprintf( string , 64 , "@@@ after fork / iter = %d / ret_fork = %d\n" , i , ret_fork ); display_string( string ); 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]\n" , i , i ); display_string( string ); // CHILD[i] exit // exit( 0 ); } } 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 ); // INIT delay int x; for( x=0 ; x