[427] | 1 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : init.c |
---|
| 3 | // Date : January 2018 |
---|
| 4 | // Author : Alain Greiner |
---|
| 5 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 6 | // This single thread application implement the "init" process for ALMOS-MKH. |
---|
[434] | 7 | // It uses the fork/exec syscalls to create N KSH child processes |
---|
| 8 | // (one child process per user terminal). |
---|
| 9 | // It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter. |
---|
| 10 | // |
---|
| 11 | // TODO : Register the PIDs for all KSH[i] in a ksh_pid[] array. |
---|
| 12 | // Then calls the wait() function to block, and reactivate any child KSH process |
---|
| 13 | // that has been deleted, using a new fork/exec. |
---|
[427] | 14 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 15 | |
---|
| 16 | #include <hard_config.h> |
---|
| 17 | |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <stdio.h> |
---|
| 20 | #include <pthread.h> |
---|
| 21 | |
---|
[434] | 22 | #define DELAY_BETWEEN_FORK 100000 |
---|
[427] | 23 | |
---|
| 24 | ////////// |
---|
| 25 | int main() |
---|
| 26 | { |
---|
| 27 | int i; |
---|
[434] | 28 | int ret_fork; // fork return value |
---|
| 29 | int ret_exec; // fork return value |
---|
| 30 | int received_pid; // pid received from the wait syscall |
---|
| 31 | int status; // used by the wait syscall |
---|
[427] | 32 | char string[64]; |
---|
| 33 | |
---|
| 34 | // check number of TXT channels |
---|
| 35 | assert( NB_TXT_CHANNELS > 1 ); |
---|
| 36 | |
---|
| 37 | // create the KSH processes (one per user terminal) |
---|
[434] | 38 | for( i = 1 ; i < NB_TXT_CHANNELS ; i++ ) |
---|
[427] | 39 | { |
---|
[434] | 40 | // INIT process fork process CHILD[i] |
---|
| 41 | ret_fork = fork(); |
---|
| 42 | |
---|
| 43 | if( ret_fork< 0 ) // error in fork |
---|
[427] | 44 | { |
---|
[434] | 45 | // INIT display error message on TXT0 terminal |
---|
[435] | 46 | snprintf( string , 64 , |
---|
| 47 | "INIT cannot fork child[%d]\n" , i ); |
---|
[427] | 48 | display_string( string ); |
---|
| 49 | |
---|
[434] | 50 | // INIT exit |
---|
| 51 | exit( 0 ); |
---|
[427] | 52 | } |
---|
[434] | 53 | else if( ret_fork == 0 ) // we are in CHILD[i] process |
---|
[427] | 54 | { |
---|
[434] | 55 | // CHILD[i] process exec process KSH[i] |
---|
| 56 | ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
| 57 | |
---|
| 58 | if ( ret_exec ) // error in exec |
---|
[427] | 59 | { |
---|
| 60 | // display error message on TXT0 terminal |
---|
[435] | 61 | snprintf( string , 64 , |
---|
| 62 | "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d\n" , i , i , ret_exec ); |
---|
[434] | 63 | display_string( string ); |
---|
[427] | 64 | } |
---|
| 65 | } |
---|
[434] | 66 | else // we are in INIT process |
---|
| 67 | { |
---|
| 68 | // INIT display CHILD[i] process PID |
---|
[435] | 69 | snprintf( string , 64 , |
---|
| 70 | "INIT forked CHILD[%d] / pid = %x\n", i , ret_fork ); |
---|
[434] | 71 | display_string( string ); |
---|
| 72 | } |
---|
[427] | 73 | |
---|
[434] | 74 | } |
---|
| 75 | |
---|
[435] | 76 | // This loop detects the termination of the KSH[i] processes, |
---|
| 77 | // to recreate these process when required. |
---|
[427] | 78 | while( 1 ) |
---|
| 79 | { |
---|
[435] | 80 | // block on child processes termination |
---|
| 81 | received_pid = wait( &status ); |
---|
[427] | 82 | |
---|
[435] | 83 | if( WIFSTOPPED( status ) ) // stopped => unblock it |
---|
| 84 | { |
---|
| 85 | // display string to report unexpected KSH process block |
---|
| 86 | snprintf( string , 64 , "KSH process %x unexpectedly stopped" , received_pid ); |
---|
| 87 | display_string( string ); |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if( WIFSIGNALED( status ) || WIFEXITED( status ) ) // killed => recreate it |
---|
| 92 | { |
---|
| 93 | // display string to report unexpected KSH process termination |
---|
| 94 | snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid ); |
---|
| 95 | display_string( string ); |
---|
| 96 | } |
---|
[427] | 97 | } |
---|
| 98 | |
---|
| 99 | } // end main() |
---|
| 100 | |
---|