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
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     ret_fork;      // fork return value 
27    int     ret_exec;      // fork return value 
28    int     rcv_pid;       // pid received from the wait syscall
29    int     status;        // used by the wait syscall
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)
36    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
37    {
38        // INIT process fork process CHILD[i]
39        ret_fork = fork();
40
41        if( ret_fork < 0 )   // error in fork
42        {
43            // INIT display error message on TXT0 terminal
44            snprintf( string , 64 , "INIT cannot fork child[%d]" , i );
45            display_string( string );
46
47            // INIT suicide
48            exit( 0 );
49        }
50        else if( ret_fork == 0 )                    // we are in CHILD[i] process
51        {
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             
56            {
57                // CHILD[i] display error message on TXT0 terminal
58                snprintf( string , 64 , 
59                "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d" , i , i , ret_exec );
60                display_string( string );
61            }
62        }
63        else                                      // we are in INIT process
64        {
65             // INIT display CHILD[i] process PID
66             snprintf( string , 64 , "INIT created KSH[%d] / pid = %x", i , ret_fork ); 
67             display_string( string );
68        }
69    } 
70
71    // This loop detects the termination of the KSH[i] processes,
72    // and recreate a new KSH[i] process when required.
73    while( 1 )
74    {
75        // block on child processes termination
76        rcv_pid = wait( &status );
77
78        if( WIFSTOPPED( status ) )                         // stopped => unblock it
79        {
80            // display string to report unexpected KSH process block
81            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
82            display_string( string ); 
83
84            // TODO : unblock KSH
85
86        }  // end KSH stopped handling
87
88        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
89        {
90            // display string to report unexpected KSH process termination
91            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
92            display_string( string ); 
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
125    }
126
127} // end main()
128
Note: See TracBrowser for help on using the repository browser.