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
RevLine 
[427]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.
[434]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.
[427]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{
[438]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
[427]32
33    // check number of TXT channels
34    assert( NB_TXT_CHANNELS > 1 );
35
36    // create the KSH processes (one per user terminal)
[434]37    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
[427]38    {
[434]39        // INIT process fork process CHILD[i]
40        ret_fork = fork();
41
[436]42        if( ret_fork < 0 )   // error in fork
[427]43        {
[437]44            // INIT display error message 
45            snprintf( string , 64 , "INIT cannot fork child[%d] => suicide" , i );
[427]46            display_string( string );
47
[436]48            // INIT suicide
[434]49            exit( 0 );
[427]50        }
[434]51        else if( ret_fork == 0 )                    // we are in CHILD[i] process
[427]52        {
[434]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             
[427]57            {
[437]58                // CHILD[i] display error message
[435]59                snprintf( string , 64 , 
[436]60                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
[434]61                display_string( string );
[427]62            }
63        }
[434]64        else                                      // we are in INIT process
65        {
66             // INIT display CHILD[i] process PID
[436]67             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork ); 
[434]68             display_string( string );
[438]69
70             // INIT wait a fixed delay between two forks
71             for( delay = 0 ; delay < 50000 ; delay++ ) asm volatile( "nop" );
[434]72        }
73    } 
74
[438]75// INIT display processes and threads in clusters 0 & 1
[437]76display_cluster_processes( 0 );
77display_sched( 0 , 0 );
78display_cluster_processes( 1 );
79display_sched( 1 , 0 );
80
[435]81    // This loop detects the termination of the KSH[i] processes,
[436]82    // and recreate a new KSH[i] process when required.
[427]83    while( 1 )
84    {
[435]85        // block on child processes termination
[436]86        rcv_pid = wait( &status );
[427]87
[435]88        if( WIFSTOPPED( status ) )                         // stopped => unblock it
89        {
90            // display string to report unexpected KSH process block
[436]91            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
[435]92            display_string( string ); 
93
[436]94            // TODO : unblock KSH
[435]95
[436]96        }  // end KSH stopped handling
97
[435]98        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
99        {
[437]100            // display string to report KSH process termination
[436]101            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
[435]102            display_string( string ); 
[436]103
104            // INIT process fork a new CHILD process
105            ret_fork = fork();
106
107            if( ret_fork < 0 )                          // error in fork
108            {
[437]109                // INIT display error message
110                snprintf( string , 64 , "INIT cannot fork child => suicide");
[436]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            }
[438]134        } // end KSH kill handling
[427]135
[438]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
[427]147} // end main()
148
Note: See TracBrowser for help on using the repository browser.