Index: trunk/kernel/syscalls/sys_exit.c
===================================================================
--- trunk/kernel/syscalls/sys_exit.c	(revision 445)
+++ trunk/kernel/syscalls/sys_exit.c	(revision 446)
@@ -36,5 +36,17 @@
 int sys_exit( uint32_t status )
 {
-    reg_t       save_sr;       // required to enable IRQs
+    reg_t       save_sr;           // required to enable IRQs
+
+    xptr_t      owner_xp;          // extended pointer on owner process
+    cxy_t       owner_cxy;         // owner process cluster
+    process_t * owner_ptr;         // local pointer on owner process
+    thread_t  * main_ptr;          // local pointer on process main thread
+    xptr_t      parent_xp;         // extended pointer on parent process
+    cxy_t       parent_cxy;        // parent process cluster
+    process_t * parent_ptr;        // local pointer on parent process
+    xptr_t      children_lock_xp;  // extended pointer on children locki
+    thread_t  * parent_main_ptr;   // local pointer on parent main thread 
+    xptr_t      parent_main_xp;    // extended pointer on parent main thread
+    uint32_t    term_state;        // termination status for owner process
 
     thread_t  * this    = CURRENT_THREAD;
@@ -52,38 +64,34 @@
 
     // get owner process descriptor pointers 
-    xptr_t      owner_xp  = process->owner_xp;
-    cxy_t       owner_cxy = GET_CXY( owner_xp );
-    process_t * owner_ptr = GET_PTR( owner_xp );
+    owner_xp  = process->owner_xp;
+    owner_cxy = GET_CXY( owner_xp );
+    owner_ptr = GET_PTR( owner_xp );
 
-    // get pointers on the process main thread
-    thread_t * main    = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
-
-    // enable IRQs
-    hal_enable_irq( &save_sr );
-
-    // mark for delete all process threads in all clusters, 
-    // but the main thread and this calling thread 
-    process_sigaction( pid , DELETE_ALL_THREADS );
-
-    // disable IRQs
-    hal_restore_irq( save_sr );
-
-#if( DEBUG_SYS_EXIT & 1)
-if( tm_start > DEBUG_SYS_EXIT )
-printk("\n[DBG] %s : thread %x deleted threads / process %x\n",
-__FUNCTION__ , this, pid );
+#if (DEBUG_SYS_EXIT & 1)
+if( DEBUG_SYS_EXIT < tm_start )
+printk("\n[DBG] %s : thread %x get owner process %x in cluster %x\n",
+__FUNCTION__ , this, owner_ptr, owner_cxy );
 #endif
 
-    // mark for delete this calling thread when it is not the main
-    if( (owner_cxy != local_cxy) || (main != this) )
-    {
+    // get pointer on the process main thread
+    main_ptr  = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
 
-#if( DEBUG_SYS_EXIT & 1)
-if( tm_start > DEBUG_SYS_EXIT )
-printk("\n[DBG] %s : calling thread %x marked iself for delete / process %x\n",
-__FUNCTION__ , this, pid );
+    // get parent process descriptor pointers
+    parent_xp  = process->parent_xp;
+    parent_cxy = GET_CXY( parent_xp );
+    parent_ptr = GET_PTR( parent_xp );
+
+#if (DEBUG_SYS_EXIT & 1)
+if( DEBUG_SYS_EXIT < tm_start )
+printk("\n[DBG] %s : thread %x get parent process %x in cluster %x\n",
+__FUNCTION__ , this, parent_ptr, parent_cxy );
 #endif
-        thread_delete( XPTR( local_cxy , this ) , pid , true );
-    }
+
+    // get extended pointer on lock protecting children list in parent process
+    children_lock_xp = XPTR( parent_cxy , &parent_ptr->children_lock ); 
+
+    // get pointers on the parent process main thread
+    parent_main_ptr = hal_remote_lpt( XPTR( parent_cxy , &parent_ptr->th_tbl[0] ) ); 
+    parent_main_xp  = XPTR( parent_cxy , parent_main_ptr );
 
     // remove process from TXT list
@@ -96,6 +104,30 @@
 #endif
 
-    // block the main thread 
-    thread_block( XPTR( owner_cxy , main ) , THREAD_BLOCKED_GLOBAL );
+    // mark for delete all process threads in all clusters, 
+    // but the main thread and this calling thread 
+    hal_enable_irq( &save_sr );
+    process_sigaction( pid , DELETE_ALL_THREADS );
+    hal_restore_irq( save_sr );
+
+#if( DEBUG_SYS_EXIT & 1)
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x deleted threads for process %x\n",
+__FUNCTION__ , this, pid );
+#endif
+
+    // mark for delete this calling thread when it is not the main
+    if( (owner_cxy != local_cxy) || (main_ptr != this) )
+    {
+
+#if( DEBUG_SYS_EXIT & 1)
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : calling thread %x marked iself for delete in process %x\n",
+__FUNCTION__ , this, pid );
+#endif
+        thread_delete( XPTR( local_cxy , this ) , pid , true );
+    }
+
+    // block this main thread 
+    thread_block( XPTR( owner_cxy , main_ptr ) , THREAD_BLOCKED_GLOBAL );
 
 #if( DEBUG_SYS_EXIT & 1)
@@ -107,11 +139,22 @@
     // atomically update owner process descriptor term_state to ask
     // the parent process sys_wait() function to delete the main thread 
-    hal_remote_atomic_or( XPTR( owner_cxy , &process->term_state ) ,
-                          PROCESS_TERM_EXIT | (status & 0xFF) );
+    term_state = (status & 0xFF) | PROCESS_TERM_EXIT;
+    hal_remote_atomic_or( XPTR( owner_cxy , &owner_ptr->term_state ) , term_state );
 
 #if( DEBUG_SYS_EXIT & 1)
 if( tm_start > DEBUG_SYS_EXIT )
-printk("\n[DBG] %s : thread %x set exit status in process %x term_state\n",
-__FUNCTION__ , this, pid );
+printk("\n[DBG] %s : thread %x set exit status in process %x / term_state %x\n",
+__FUNCTION__ , this, pid, term_state );
+#endif
+
+    // take the children lock and unblock the parent process main thread 
+    remote_spinlock_lock( children_lock_xp );
+    thread_unblock( parent_main_xp , THREAD_BLOCKED_WAIT );
+    remote_spinlock_unlock( children_lock_xp );
+
+#if( DEBUG_SYS_EXIT & 1)
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x in cluster %x unblock parent main thread %x in cluster %x\n",
+__FUNCTION__ , this, local_cxy, parent_main_ptr, parent_cxy );
 #endif
 
