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

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

Introduce idbg application.

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