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

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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