Changeset 434 for trunk/user
- Timestamp:
- Feb 14, 2018, 3:41:31 PM (7 years ago)
- Location:
- trunk/user
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/user/init/init.c
r427 r434 5 5 /////////////////////////////////////////////////////////////////////////////////////// 6 6 // This single thread application implement the "init" process for ALMOS-MKH. 7 // -It uses the fork/exec syscalls to create N KSH child processes8 // (one child process per user terminal), and register the corresponding PIDs9 // 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 KSH12 // 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.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 14 /////////////////////////////////////////////////////////////////////////////////////// 15 15 … … 20 20 #include <pthread.h> 21 21 22 #define NB_KSH (NB_TXT_CHANNELS - 1) 23 22 #define DELAY_BETWEEN_FORK 100000 24 23 25 24 ////////// … … 27 26 { 28 27 int i; 29 int ret; // fork return value 30 int child_pid; 31 int status; // used by the wait function 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 32 char string[64]; 33 33 … … 36 36 37 37 // create the KSH processes (one per user terminal) 38 for( i = 0 ; i < NB_KSH; i++ )38 for( i = 1 ; i < NB_TXT_CHANNELS ; i++ ) 39 39 { 40 // INIT process fork process CHILD41 ret = fork();42 40 43 if( ret < 0 ) // error in fork 41 snprintf( string , 64 , "@@@ before fork / iter = %d\n" , i ); 42 display_string( string ); 43 44 // INIT process fork process CHILD[i] 45 ret_fork = fork(); 46 47 snprintf( string , 64 , "@@@ after fork / iter = %d / ret_fork = %d\n" , i , ret_fork ); 48 display_string( string ); 49 50 if( ret_fork< 0 ) // error in fork 44 51 { 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 ); 52 // INIT display error message on TXT0 terminal 53 snprintf( string , 64 , "init cannot fork child[%d]\n" , i ); 55 54 display_string( string ); 56 55 57 // INIT process deschedule58 pthread_yield();56 // INIT exit 57 exit( 0 ); 59 58 } 60 else // we are in CHILDprocess59 else if( ret_fork == 0 ) // we are in CHILD[i] process 61 60 { 62 // CHILD process exec process KSH 63 if ( exec( "/bin/user/ksh.elf" , NULL , NULL ) ) // CHILD failure 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 64 65 { 65 66 // display error message on TXT0 terminal 66 display_string( "child process cannot exec process ksh\n" ); 67 snprintf( string , 64 , "child[%d] cannot exec ksh[%d]\n" , i , i ); 68 display_string( string ); 67 69 68 // CHILD process exit 69 exit( 1 ); 70 } 71 else // child success 72 { 73 // CHILD process deschedule 74 pthread_yield(); 70 // CHILD[i] exit 71 // exit( 0 ); 75 72 } 76 73 } 77 } 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 ); 78 79 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 ); 79 104 // @@@ 80 display_string( "@@@ INIT process created all KSH processes\n" );81 105 display_process( 0 ); 82 106 display_sched( 0 , 0 ); 83 107 // @@@ 108 } 109 110 } 84 111 85 112 // This blocking loop is only for debug, because KSH[i] processes 86 // should never be killed, and INIT should never exitthe wait() function.113 // should never be killed, and INIT should never return from the wait() function. 87 114 while( 1 ) 88 115 { 89 116 // block on child process termination 90 child_pid = wait( &status );117 received_pid = wait( &status ); 91 118 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) 119 // display string to report unexpected KSH process termination 120 snprintf( string , 64 , "KSH process %x unexpectedly terminated" , received_pid ); 96 121 display_string( string ); 97 122 } -
trunk/user/ksh/ksh.c
r427 r434 297 297 static void cmd_load( int argc , char **argv ) 298 298 { 299 unsigned int ret_pid;300 unsigned int new_pid;299 int ret; 300 unsigned int ksh_pid; 301 301 char * pathname; 302 302 unsigned int background; … … 304 304 if( (argc < 2) || (argc > 3) ) 305 305 { 306 printf(" usage: %s pathname [&] 306 printf(" usage: %s pathname [&]\n", argv[0] ); 307 307 return; 308 308 } … … 311 311 312 312 if( argc == 3 ) background = (argv[2][0] == '&'); 313 314 // fork system call 315 ret_pid = fork(); 316 317 if (ret_pid == 0) // it is the child process 318 { 319 // exec system call 320 if( exec( pathname , NULL , NULL ) ) 313 else background = 0; 314 315 // get KSH process PID 316 ksh_pid = getpid(); 317 318 // KSH process fork CHILD process 319 ret = fork(); 320 321 if ( ret < 0 ) // it is a failure reported to parent 322 { 323 printf(" error: ksh process unable to fork\n"); 324 } 325 else if (ret == 0) // it is the CHILD process 326 { 327 // give back to KSH process the terminal ownership 328 if( background ) fg( ksh_pid ); 329 330 // CHILD process exec NEW process 331 ret = exec( pathname , NULL , NULL ); 332 333 if( ret ) 321 334 { 322 335 printf(" error: new process unable to exec <%s>\n", pathname ); 323 336 exit(0); 324 337 } 325 326 // get new process pid327 new_pid = getpid();328 329 // give new process terminal ownership330 if( background == 0 ) fg( new_pid );331 338 } 332 else if ( ret_pid < 0 ) // it is a failure reported to parent333 {334 printf(" error: unable to fork\n");335 }336 339 } // end cmd_load 337 340 … … 624 627 if (!found) 625 628 { 626 printf(" \n undefined command %s\n", argv[0]);629 printf(" undefined command <%s>\n", argv[0]); 627 630 } 628 631 } -
trunk/user/pgcd/pgcd.c
r427 r434 16 16 int opx; 17 17 int opy; 18 char c;19 18 20 19 printf( "*** Starting interactive pgcd ***\n\n" ); -
trunk/user/sort/sort.c
r427 r434 240 240 { 241 241 printf("\n[SORT ERROR] number of cores must be power of 2\n"); 242 pthread_exit( NULL);242 exit( 0 ); 243 243 } 244 244 … … 247 247 { 248 248 printf("\n[SORT ERROR] array size must be multiple of number of threads\n"); 249 pthread_exit( NULL);249 exit( 0 ); 250 250 } 251 251 … … 261 261 { 262 262 printf("\n[SORT ERROR] cannot initialise barrier\n" ); 263 pthread_exit( NULL);263 exit( 0 ); 264 264 } 265 265 … … 309 309 { 310 310 printf("\n[SORT ERROR] creating thread %x\n", thread_uid ); 311 pthread_exit( NULL);311 exit( 0 ); 312 312 } 313 313 … … 350 350 { 351 351 printf("\n[SORT] success at cycle %d\n", (unsigned int)cycle ); 352 pthread_exit( NULL);352 exit( 0 ); 353 353 } 354 354 else 355 355 { 356 356 printf("\n[SORT] failure at cycle %d\n", (unsigned int)cycle ); 357 pthread_exit( NULL);357 exit( 0 ); 358 358 } 359 359
Note: See TracChangeset
for help on using the changeset viewer.