source: trunk/user/init/init.c @ 574

Last change on this file since 574 was 574, checked in by alain, 6 years ago

Cosmetic.

File size: 6.6 KB
RevLine 
[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// Then calls the wait() function to block, and reactivate any child KSH process
10// that has been deleted, using a new fork/exec.
[440]11// It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter.
[427]12///////////////////////////////////////////////////////////////////////////////////////
13
14#include <hard_config.h>
[444]15#include <unistd.h>
[427]16#include <stdlib.h>
17#include <stdio.h>
18#include <pthread.h>
[445]19#include <almosmkh.h>
[574]20#include <hal_macros.h>
[444]21#include <sys/wait.h>
[427]22
[442]23#define DEBUG_PROCESS_INIT    0
[440]24
[445]25// TODO improve the get_config() syscall to return nb_txt_channels
26// and avoid the hard_config include [AG]
27
[574]28// TODO introduce a communication channel between INIT and KSH
29// to allow KSH to signal INIT the exec completion.
30
31////////////////
[475]32int main( void )
[427]33{
[438]34    int           i;
35    int           ret_fork;      // fork return value 
36    int           ret_exec;      // exec return value 
37    int           rcv_pid;       // pid received from the wait syscall
38    int           status;        // used by the wait syscall
39    char          string[64];    // log messages on kernel TXT0
[427]40
[574]41#if DEBUG_PROCESS_INIT
42display_string("[INIT] process enters");
43#endif
44
[427]45    // check number of TXT channels
[445]46    if( NB_TXT_CHANNELS < 2 )
47    {
48        printf("\n[ERROR] in init process : number of TXT channels must be larger than 1\n");
49        exit( EXIT_FAILURE );
50    }
[427]51
52    // create the KSH processes (one per user terminal)
[434]53    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
[427]54    {
[457]55
56#if DEBUG_PROCESS_INIT
57snprintf( string , 64 , "[INIT] start child[%d] creation" , i );
58display_string( string );
59#endif
60
[434]61        // INIT process fork process CHILD[i]
62        ret_fork = fork();
63
[436]64        if( ret_fork < 0 )   // error in fork
[427]65        {
[437]66            // INIT display error message 
[457]67            snprintf( string , 64 , "[INIT ERROR] cannot fork child[%d] => suicide" , i );
[427]68            display_string( string );
69
[436]70            // INIT suicide
[434]71            exit( 0 );
[427]72        }
[434]73        else if( ret_fork == 0 )                    // we are in CHILD[i] process
[427]74        {
[434]75            // CHILD[i] process exec process KSH[i]
[444]76            ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL ); 
[434]77
78            if ( ret_exec )   // error in exec             
[427]79            {
[437]80                // CHILD[i] display error message
[435]81                snprintf( string , 64 , 
[440]82                "[INIT ERROR] CHILD[%d] cannot exec KSH / ret_exec = %d" , i , ret_exec );
[434]83                display_string( string );
[427]84            }
85        }
[434]86        else                                      // we are in INIT process
87        {
[457]88            // INIT display CHILD[i] process PID
89            snprintf( string , 64 , "[INIT] created KSH[%d] / pid = %x", i , ret_fork ); 
90            display_string( string );
[434]91        }
92    } 
[440]93 
[442]94#if DEBUG_PROCESS_INIT
[528]95{
96    unsigned int  x_size;        // number of clusters in a row
97    unsigned int  y_size;        // number of clusters in a column
98    unsigned int  ncores;        // number of cores per cluster
99    unsigned int  x;             // cluster x coordinate
100    unsigned int  y;             // cluster y coordinate
101    unsigned int  cxy;           // cluster identifier
102    unsigned int  lid;           // core local index
[434]103
[528]104    // get hardware config
105    get_config( &x_size , &y_size , &ncores );
106
107    // INIT displays processes and threads in all clusters
108    for( x = 0 ; x < x_size ; x++ )
[440]109    {
[574]110        cxy = HAL_CXY_FROM_XY( x , y );
111        display_cluster_processes( cxy );
112        for( lid = 0 ; lid < ncores ; lid++ )
113        { 
[528]114            display_cluster_processes( cxy );
[440]115        }
116    }
[457]117}
[440]118#endif
119
[435]120    // This loop detects the termination of the KSH[i] processes,
[436]121    // and recreate a new KSH[i] process when required.
[427]122    while( 1 )
123    {
[435]124        // block on child processes termination
[436]125        rcv_pid = wait( &status );
[427]126
[435]127        if( WIFSTOPPED( status ) )                         // stopped => unblock it
128        {
129            // display string to report unexpected KSH process block
[440]130            snprintf( string , 64 , "[INIT] KSH process %x stopped => unblock it" , rcv_pid );
[435]131            display_string( string ); 
132
[440]133            // TODO : unblock KSH [AG]
[435]134
[436]135        }  // end KSH stopped handling
136
[435]137        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
138        {
[437]139            // display string to report KSH process termination
[440]140            snprintf( string , 64 , "[INIT] KSH process %x terminated => recreate", rcv_pid );
[435]141            display_string( string ); 
[436]142
143            // INIT process fork a new CHILD process
144            ret_fork = fork();
145
146            if( ret_fork < 0 )                          // error in fork
147            {
[437]148                // INIT display error message
[440]149                snprintf( string , 64 , "[INIT ERROR] cannot fork child => suicide");
[436]150                display_string( string );
151
152                // INIT suicide
153                exit( 0 );
154            }
155            else if( ret_fork == 0 )                    // we are in CHILD process
156            {
157                // CHILD process exec process KSH
[444]158                ret_exec = execve( "/bin/user/ksh.elf" , NULL , NULL ); 
[436]159
160                if ( ret_exec )   // error in exec             
161                {
162                    // CHILD display error message on TXT0 terminal
[440]163                    snprintf( string , 64 , "[INIT ERROR] CHILD cannot exec KSH" );
[436]164                    display_string( string );
165                }
166            }
167            else                                       // we are in INIT process
168            {
[442]169                // INIT display new KSH process PID
170                snprintf( string , 64 , "[INIT] re-created KSH / pid = %x", ret_fork ); 
[436]171                display_string( string );
172            }
[438]173        } // end KSH kill handling
[427]174
[442]175#if DEBUG_PROCESS_INIT
[438]176
[440]177        // INIT displays processes and threads in all clusters
178        for( x = 0 ; x < x_size ; x++ )
179        {
180            for( y = 0 ; y < y_size ; y++ )
181            {
[574]182                cxy = HAL_CXY_FROM_XY( x , y );
[440]183                display_cluster_processes( cxy );
184                for( lid = 0 ; lid < ncores ; lid++ )
185                { 
186                    display_sched( cxy , lid );
187                }
188            }
189        }
[438]190
[440]191#endif
192
[438]193    }  // end while waiting KSH[i] termination
194
[427]195} // end main()
196
Note: See TracBrowser for help on using the repository browser.