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

Last change on this file since 527 was 475, checked in by viala@…, 6 years ago

[userland] Add void type to function prototypes with no parameter

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