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
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 
44            snprintf( string , 64 , "INIT cannot fork child[%d] => suicide" , 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
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// 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
77    // This loop detects the termination of the KSH[i] processes,
78    // and recreate a new KSH[i] process when required.
79    while( 1 )
80    {
81        // block on child processes termination
82        rcv_pid = wait( &status );
83
84        if( WIFSTOPPED( status ) )                         // stopped => unblock it
85        {
86            // display string to report unexpected KSH process block
87            snprintf( string , 64 , "KSH process %x stopped => unblock it" , rcv_pid );
88            display_string( string ); 
89
90            // TODO : unblock KSH
91
92        }  // end KSH stopped handling
93
94        if( WIFSIGNALED( status ) || WIFEXITED( status ) )  // killed => recreate it
95        {
96            // display string to report KSH process termination
97            snprintf( string , 64 , "KSH process %x terminated => recreate KSH", rcv_pid );
98            display_string( string ); 
99
100            // INIT process fork a new CHILD process
101            ret_fork = fork();
102
103            if( ret_fork < 0 )                          // error in fork
104            {
105                // INIT display error message
106                snprintf( string , 64 , "INIT cannot fork child => suicide");
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
131    }
132
133} // end main()
134
Note: See TracBrowser for help on using the repository browser.