Changeset 436 for trunk/user/init/init.c


Ignore:
Timestamp:
Mar 7, 2018, 9:02:03 AM (6 years ago)
Author:
alain
Message:

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/user/init/init.c

    r435 r436  
    2020#include <pthread.h>
    2121
    22 #define DELAY_BETWEEN_FORK 100000
    23 
    2422//////////
    2523int main()
     
    2826    int     ret_fork;      // fork return value 
    2927    int     ret_exec;      // fork return value 
    30     int     received_pid;  // pid received from the wait syscall
     28    int     rcv_pid;       // pid received from the wait syscall
    3129    int     status;        // used by the wait syscall
    3230    char    string[64];
     
    4139        ret_fork = fork();
    4240
    43         if( ret_fork< 0 )   // error in fork
     41        if( ret_fork < 0 )   // error in fork
    4442        {
    4543            // INIT display error message on TXT0 terminal
    46             snprintf( string , 64 ,
    47             "INIT cannot fork child[%d]\n" , i );
     44            snprintf( string , 64 , "INIT cannot fork child[%d]" , i );
    4845            display_string( string );
    4946
    50             // INIT exit
     47            // INIT suicide
    5148            exit( 0 );
    5249        }
     
    5855            if ( ret_exec )   // error in exec             
    5956            {
    60                 // display error message on TXT0 terminal
     57                // CHILD[i] display error message on TXT0 terminal
    6158                snprintf( string , 64 ,
    62                 "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d\n" , i , i , ret_exec );
     59                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
    6360                display_string( string );
    6461            }
     
    6764        {
    6865             // INIT display CHILD[i] process PID
    69              snprintf( string , 64 ,
    70              "INIT forked CHILD[%d] / pid = %x\n", i , ret_fork );
     66             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork );
    7167             display_string( string );
    7268        }
    73 
    7469    }
    7570
    7671    // This loop detects the termination of the KSH[i] processes,
    77     // to recreate these process when required.
     72    // and recreate a new KSH[i] process when required.
    7873    while( 1 )
    7974    {
    8075        // block on child processes termination
    81         received_pid = wait( &status );
     76        rcv_pid = wait( &status );
    8277
    8378        if( WIFSTOPPED( status ) )                         // stopped => unblock it
    8479        {
    8580            // display string to report unexpected KSH process block
    86             snprintf( string , 64 , "KSH process %x unexpectedly stopped" , received_pid );
     81            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
    8782            display_string( string );
    8883
    89         }
     84            // TODO : unblock KSH
     85
     86        }  // end KSH stopped handling
    9087
    9188        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
    9289        {
    9390            // display string to report unexpected KSH process termination
    94             snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid );
     91            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
    9592            display_string( string );
    96         }
     93
     94            // INIT process fork a new CHILD process
     95            ret_fork = fork();
     96
     97            if( ret_fork < 0 )                          // error in fork
     98            {
     99                // INIT display error message on TXT0 terminal
     100                snprintf( string , 64 , "INIT cannot fork child");
     101                display_string( string );
     102
     103                // INIT suicide
     104                exit( 0 );
     105            }
     106            else if( ret_fork == 0 )                    // we are in CHILD process
     107            {
     108                // CHILD process exec process KSH
     109                ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL );
     110
     111                if ( ret_exec )   // error in exec             
     112                {
     113                    // CHILD display error message on TXT0 terminal
     114                    snprintf( string , 64 , "CHILD cannot exec KSH" );
     115                    display_string( string );
     116                }
     117            }
     118            else                                       // we are in INIT process
     119            {
     120                // INIT display new CHILD process PID
     121                snprintf( string , 64 , "INIT forked CHILD / pid = %x", ret_fork );
     122                display_string( string );
     123            }
     124        }  // end KSH kill handling
    97125    }
    98126
Note: See TracChangeset for help on using the changeset viewer.