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

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

blap

File size: 4.0 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
[434]22#define DELAY_BETWEEN_FORK 100000
[427]23
24//////////
25int main()
26{
27    int     i;
[434]28    int     ret_fork;      // fork return value 
29    int     ret_exec;      // fork return value 
30    int     received_pid;  // pid received from the wait syscall
31    int     status;        // used by the wait syscall
[427]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)
[434]38    for( i = 1 ; i <  NB_TXT_CHANNELS ; i++ )
[427]39    {
40
[434]41snprintf( string , 64 , "@@@ before fork / iter = %d\n" , i );
42display_string( string );
[427]43
[434]44        // INIT process fork process CHILD[i]
45        ret_fork = fork();
46
47snprintf( string , 64 , "@@@ after fork / iter = %d / ret_fork = %d\n" , i , ret_fork );
48display_string( string );
49
50        if( ret_fork< 0 )   // error in fork
[427]51        {
[434]52            // INIT display error message on TXT0 terminal
53            snprintf( string , 64 , "init cannot fork child[%d]\n" , i );
[427]54            display_string( string );
55
[434]56            // INIT exit
57            exit( 0 );
[427]58        }
[434]59        else if( ret_fork == 0 )                    // we are in CHILD[i] process
[427]60        {
[434]61            // CHILD[i] process exec process KSH[i]
62            ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); 
63
64            if ( ret_exec )   // error in exec             
[427]65            {
66                // display error message on TXT0 terminal
[434]67                snprintf( string , 64 , "child[%d] cannot exec ksh[%d]\n" , i , i );
68                display_string( string );
[427]69
[434]70                // CHILD[i] exit
71                // exit( 0 );
[427]72            }
73        }
[434]74        else                                      // we are in INIT process
75        {
76             // INIT display CHILD[i] process PID
77             snprintf( string , 64 , "INIT forked CHILD[%d] / pid = %x\n", i , ret_fork ); 
78             display_string( string );
[427]79
[434]80             // INIT delay
81             int     x;
82             for( x=0 ; x<DELAY_BETWEEN_FORK ; x++) asm volatile ("nop");
83
84/*
85             // INIT wait exec completion by CHILD[i]
86             while( 1 )
87             {
88                 // get terminating process PID
89                 received_pid = wait( &status );
90
91                 if( received_pid == ret_fork ) break;
92                 else
93                 {
94                     snprintf( string , 64 ,
95                     "expected_pid %d / received_pid %d" , ret_fork , received_pid  );
96                     display_string( string );
97                 }
98             }
99
100*/
101             // INIT display string on kernel TXT0
102             snprintf( string , 64 , "INIT created KSH[%d]\n", i );
103             display_string( string );
[427]104// @@@
105display_process( 0 );
106display_sched( 0 , 0 );
107// @@@
[434]108        }
[427]109
[434]110    } 
111
[427]112    // This blocking loop is only for debug, because KSH[i] processes
[434]113    // should never be killed, and INIT should never return from the wait() function.
[427]114    while( 1 )
115    {
116        // block on child process termination
[434]117        received_pid = wait( &status ); 
[427]118
[434]119        // display string to report unexpected KSH process termination
120        snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid );
[427]121        display_string( string ); 
122    }
123
124} // end main()
125
Note: See TracBrowser for help on using the repository browser.