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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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