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

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

Restructure the mini_libc.

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