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

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

Fix various bugs

File size: 4.7 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{
25    int     i;
[434]26    int     ret_fork;      // fork return value 
27    int     ret_exec;      // fork return value 
[436]28    int     rcv_pid;       // pid received from the wait syscall
[434]29    int     status;        // used by the wait syscall
[427]30    char    string[64];
31
32    // check number of TXT channels
33    assert( NB_TXT_CHANNELS > 1 );
34
35    // create the KSH processes (one per user terminal)
[434]36    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
[427]37    {
[434]38        // INIT process fork process CHILD[i]
39        ret_fork = fork();
40
[436]41        if( ret_fork < 0 )   // error in fork
[427]42        {
[437]43            // INIT display error message 
44            snprintf( string , 64 , "INIT cannot fork child[%d] => suicide" , i );
[427]45            display_string( string );
46
[436]47            // INIT suicide
[434]48            exit( 0 );
[427]49        }
[434]50        else if( ret_fork == 0 )                    // we are in CHILD[i] process
[427]51        {
[434]52            // CHILD[i] process exec process KSH[i]
53            ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
54
55            if ( ret_exec )   // error in exec             
[427]56            {
[437]57                // CHILD[i] display error message
[435]58                snprintf( string , 64 , 
[436]59                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
[434]60                display_string( string );
[427]61            }
62        }
[434]63        else                                      // we are in INIT process
64        {
65             // INIT display CHILD[i] process PID
[436]66             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork ); 
[434]67             display_string( string );
68        }
69    } 
70
[437]71// display processes and threads in clusters 0 & 1
72display_cluster_processes( 0 );
73display_sched( 0 , 0 );
74display_cluster_processes( 1 );
75display_sched( 1 , 0 );
76
[435]77    // This loop detects the termination of the KSH[i] processes,
[436]78    // and recreate a new KSH[i] process when required.
[427]79    while( 1 )
80    {
[435]81        // block on child processes termination
[436]82        rcv_pid = wait( &status );
[427]83
[435]84        if( WIFSTOPPED( status ) )                         // stopped => unblock it
85        {
86            // display string to report unexpected KSH process block
[436]87            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
[435]88            display_string( string ); 
89
[436]90            // TODO : unblock KSH
[435]91
[436]92        }  // end KSH stopped handling
93
[435]94        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
95        {
[437]96            // display string to report KSH process termination
[436]97            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
[435]98            display_string( string ); 
[436]99
100            // INIT process fork a new CHILD process
101            ret_fork = fork();
102
103            if( ret_fork < 0 )                          // error in fork
104            {
[437]105                // INIT display error message
106                snprintf( string , 64 , "INIT cannot fork child => suicide");
[436]107                display_string( string );
108
109                // INIT suicide
110                exit( 0 );
111            }
112            else if( ret_fork == 0 )                    // we are in CHILD process
113            {
114                // CHILD process exec process KSH
115                ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
116
117                if ( ret_exec )   // error in exec             
118                {
119                    // CHILD display error message on TXT0 terminal
120                    snprintf( string , 64 , "CHILD cannot exec KSH" );
121                    display_string( string );
122                }
123            }
124            else                                       // we are in INIT process
125            {
126                // INIT display new CHILD process PID
127                snprintf( string , 64 , "INIT forked CHILD / pid = %x", ret_fork ); 
128                display_string( string );
129            }
130        }  // end KSH kill handling
[427]131    }
132
133} // end main()
134
Note: See TracBrowser for help on using the repository browser.