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

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

1) improve the threads and process destruction mechanism.
2) introduce FIFOs in the soclib_tty driver.

File size: 4.6 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        {
[434]43            // INIT display error message on TXT0 terminal
[436]44            snprintf( string , 64 , "INIT cannot fork child[%d]" , 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            {
[436]57                // CHILD[i] display error message on TXT0 terminal
[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
[435]71    // This loop detects the termination of the KSH[i] processes,
[436]72    // and recreate a new KSH[i] process when required.
[427]73    while( 1 )
74    {
[435]75        // block on child processes termination
[436]76        rcv_pid = wait( &status );
[427]77
[435]78        if( WIFSTOPPED( status ) )                         // stopped => unblock it
79        {
80            // display string to report unexpected KSH process block
[436]81            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
[435]82            display_string( string ); 
83
[436]84            // TODO : unblock KSH
[435]85
[436]86        }  // end KSH stopped handling
87
[435]88        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
89        {
90            // display string to report unexpected KSH process termination
[436]91            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
[435]92            display_string( string ); 
[436]93
94            // INIT process fork a new CHILD process
95            ret_fork = fork();
96
97            if( ret_fork < 0 )                          // error in fork
98            {
99                // INIT display error message on TXT0 terminal
100                snprintf( string , 64 , "INIT cannot fork child");
101                display_string( string );
102
103                // INIT suicide
104                exit( 0 );
105            }
106            else if( ret_fork == 0 )                    // we are in CHILD process
107            {
108                // CHILD process exec process KSH
109                ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
110
111                if ( ret_exec )   // error in exec             
112                {
113                    // CHILD display error message on TXT0 terminal
114                    snprintf( string , 64 , "CHILD cannot exec KSH" );
115                    display_string( string );
116                }
117            }
118            else                                       // we are in INIT process
119            {
120                // INIT display new CHILD process PID
121                snprintf( string , 64 , "INIT forked CHILD / pid = %x", ret_fork ); 
122                display_string( string );
123            }
124        }  // end KSH kill handling
[427]125    }
126
127} // end main()
128
Note: See TracBrowser for help on using the repository browser.