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

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

Fix a bug in scheduler related to RPC blocking.

File size: 5.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// It includes the hard_config.h file to get th NB_TXT_CHANNELS parameter.
10//
11// TODO : Register the PIDs for all KSH[i] in a ksh_pid[] array.
12// Then calls the wait() function to block, and reactivate any child KSH process
13// that has been deleted, using a new fork/exec.
14///////////////////////////////////////////////////////////////////////////////////////
15
16#include <hard_config.h>
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <pthread.h>
21
22//////////
23int main()
24{
25    int           i;
26    int           delay;
27    int           ret_fork;      // fork return value 
28    int           ret_exec;      // exec return value 
29    int           rcv_pid;       // pid received from the wait syscall
30    int           status;        // used by the wait syscall
31    char          string[64];    // log messages on kernel TXT0
32
33    // check number of TXT channels
34    assert( NB_TXT_CHANNELS > 1 );
35
36    // create the KSH processes (one per user terminal)
37    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
38    {
39        // INIT process fork process CHILD[i]
40        ret_fork = fork();
41
42        if( ret_fork < 0 )   // error in fork
43        {
44            // INIT display error message 
45            snprintf( string , 64 , "INIT cannot fork child[%d] => suicide" , i );
46            display_string( string );
47
48            // INIT suicide
49            exit( 0 );
50        }
51        else if( ret_fork == 0 )                    // we are in CHILD[i] process
52        {
53            // CHILD[i] process exec process KSH[i]
54            ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
55
56            if ( ret_exec )   // error in exec             
57            {
58                // CHILD[i] display error message
59                snprintf( string , 64 , 
60                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
61                display_string( string );
62            }
63        }
64        else                                      // we are in INIT process
65        {
66             // INIT display CHILD[i] process PID
67             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork ); 
68             display_string( string );
69
70             // INIT wait a fixed delay between two forks
71             for( delay = 0 ; delay < 50000 ; delay++ ) asm volatile( "nop" );
72        }
73    } 
74
75// INIT display processes and threads in clusters 0 & 1
76display_cluster_processes( 0 );
77display_sched( 0 , 0 );
78display_cluster_processes( 1 );
79display_sched( 1 , 0 );
80
81    // This loop detects the termination of the KSH[i] processes,
82    // and recreate a new KSH[i] process when required.
83    while( 1 )
84    {
85        // block on child processes termination
86        rcv_pid = wait( &status );
87
88        if( WIFSTOPPED( status ) )                         // stopped => unblock it
89        {
90            // display string to report unexpected KSH process block
91            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
92            display_string( string ); 
93
94            // TODO : unblock KSH
95
96        }  // end KSH stopped handling
97
98        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
99        {
100            // display string to report KSH process termination
101            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
102            display_string( string ); 
103
104            // INIT process fork a new CHILD process
105            ret_fork = fork();
106
107            if( ret_fork < 0 )                          // error in fork
108            {
109                // INIT display error message
110                snprintf( string , 64 , "INIT cannot fork child => suicide");
111                display_string( string );
112
113                // INIT suicide
114                exit( 0 );
115            }
116            else if( ret_fork == 0 )                    // we are in CHILD process
117            {
118                // CHILD process exec process KSH
119                ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
120
121                if ( ret_exec )   // error in exec             
122                {
123                    // CHILD display error message on TXT0 terminal
124                    snprintf( string , 64 , "CHILD cannot exec KSH" );
125                    display_string( string );
126                }
127            }
128            else                                       // we are in INIT process
129            {
130                // INIT display new CHILD process PID
131                snprintf( string , 64 , "INIT forked CHILD / pid = %x", ret_fork ); 
132                display_string( string );
133            }
134        } // end KSH kill handling
135
136// INIT wait a fixed delay
137for( delay = 0 ; delay < 50000 ; delay++ ) asm volatile( "nop" );
138
139// INIT display processes and threads in clusters 0 & 1
140display_cluster_processes( 0 );
141display_sched( 0 , 0 );
142display_cluster_processes( 1 );
143display_sched( 1 , 0 );
144
145    }  // end while waiting KSH[i] termination
146
147} // end main()
148
Note: See TracBrowser for help on using the repository browser.