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 | #define DELAY_BETWEEN_FORK 100000 |
---|
23 | |
---|
24 | ////////// |
---|
25 | int main() |
---|
26 | { |
---|
27 | int i; |
---|
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 |
---|
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 = 1 ; i < NB_TXT_CHANNELS ; i++ ) |
---|
39 | { |
---|
40 | // INIT process fork process CHILD[i] |
---|
41 | ret_fork = fork(); |
---|
42 | |
---|
43 | if( ret_fork< 0 ) // error in fork |
---|
44 | { |
---|
45 | // INIT display error message on TXT0 terminal |
---|
46 | snprintf( string , 64 , |
---|
47 | "INIT cannot fork child[%d]\n" , i ); |
---|
48 | display_string( string ); |
---|
49 | |
---|
50 | // INIT exit |
---|
51 | exit( 0 ); |
---|
52 | } |
---|
53 | else if( ret_fork == 0 ) // we are in CHILD[i] process |
---|
54 | { |
---|
55 | // CHILD[i] process exec process KSH[i] |
---|
56 | ret_exec = exec( "/bin/user/ksh.elf" , NULL , NULL ); |
---|
57 | |
---|
58 | if ( ret_exec ) // error in exec |
---|
59 | { |
---|
60 | // display error message on TXT0 terminal |
---|
61 | snprintf( string , 64 , |
---|
62 | "CHILD[%d] cannot exec KSH[%d] / ret_exec = %d\n" , i , i , ret_exec ); |
---|
63 | display_string( string ); |
---|
64 | } |
---|
65 | } |
---|
66 | else // we are in INIT process |
---|
67 | { |
---|
68 | // INIT display CHILD[i] process PID |
---|
69 | snprintf( string , 64 , |
---|
70 | "INIT forked CHILD[%d] / pid = %x\n", i , ret_fork ); |
---|
71 | display_string( string ); |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | // This loop detects the termination of the KSH[i] processes, |
---|
77 | // to recreate these process when required. |
---|
78 | while( 1 ) |
---|
79 | { |
---|
80 | // block on child processes termination |
---|
81 | received_pid = wait( &status ); |
---|
82 | |
---|
83 | if( WIFSTOPPED( status ) ) // stopped => unblock it |
---|
84 | { |
---|
85 | // display string to report unexpected KSH process block |
---|
86 | snprintf( string , 64 , "KSH process %x unexpectedly stopped" , received_pid ); |
---|
87 | display_string( string ); |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | if( WIFSIGNALED( status ) || WIFEXITED( status ) ) // killed => recreate it |
---|
92 | { |
---|
93 | // display string to report unexpected KSH process termination |
---|
94 | snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid ); |
---|
95 | display_string( string ); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | } // end main() |
---|
100 | |
---|