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

Last change on this file since 432 was 427, checked in by alain, 7 years ago

Several processes KSH[i] can be launched by the INIT process.

File size: 3.3 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), and register the corresponding PIDs
9//   in the ksh_pid[] array. Then it calls the wait() function to block.
10// - It is reactivated when any child KSH process is terminated by a SIGKILL signal,
11//   or stopped by a SIGSTOP signal. In case of SIGKILL, it scan all registered KSH
12//   processes and re-creates each killed KSH process, using a new fork/exec.
13// It includes the hard_config.h file to get the NB_TXT_CHANNELS parameter.
14///////////////////////////////////////////////////////////////////////////////////////
15
16#include <hard_config.h>
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <pthread.h>
21
22#define NB_KSH  (NB_TXT_CHANNELS - 1)
23
24
25//////////
26int main()
27{
28    int     i;
29    int     ret;           // fork return value 
30    int     child_pid;
31    int     status;        // used by the wait function
32    char    string[64];
33
34    // check number of TXT channels
35    assert( NB_TXT_CHANNELS > 1 );
36
37    // create the KSH processes (one per user terminal)
38    for( i = 0 ; i <  NB_KSH ; i++ )
39    {
40        // INIT process fork process CHILD
41        ret = fork();
42
43        if( ret < 0 )                            // error in fork
44        {
45            // display error message on TXT0 terminal
46            display_string( "init process cannot fork\n" );
47
48            // INIT process exit
49            exit( 1 );
50        }
51        else if( ret > 0 )                     // we are in INIT process
52        {
53            // INIT display string on kernel TXT0
54            snprintf( string , 64 , "INIT created KSH[%d]\n" , i );
55            display_string( string );
56
57            // INIT process deschedule
58            pthread_yield();
59        }
60        else                                   // we are in CHILD process
61        {
62            // CHILD process exec process KSH
63            if ( exec( "/bin/user/ksh.elf" , NULL , NULL ) )  // CHILD failure
64            {
65                // display error message on TXT0 terminal
66                display_string( "child process cannot exec process ksh\n" );
67
68                // CHILD process exit
69                exit( 1 );
70            }
71            else                                             // child success
72            {
73                // CHILD process deschedule
74                pthread_yield();
75            }
76        }
77    } 
78
79// @@@
80display_string( "@@@  INIT process created all KSH processes\n" );
81display_process( 0 );
82display_sched( 0 , 0 );
83// @@@
84
85    // This blocking loop is only for debug, because KSH[i] processes
86    // should never be killed, and INIT should never exit the wait() function.
87    while( 1 )
88    {
89        // block on child process termination
90        child_pid = wait( &status ); 
91
92        // build a string to report unexpected KSH process termination
93        snprintf( string , 64 , "KSH process %x unexpectedly terminated" , child_pid );
94
95        // display string on kernel TXT0 (INIT has no TXT terminal)
96        display_string( string ); 
97    }
98
99} // end main()
100
Note: See TracBrowser for help on using the repository browser.