Index: trunk/kernel/syscalls/sys_exit.c
===================================================================
--- trunk/kernel/syscalls/sys_exit.c	(revision 438)
+++ trunk/kernel/syscalls/sys_exit.c	(revision 440)
@@ -2,5 +2,5 @@
  * sys_exit.c - Kernel function implementing the "exit" system call.
  * 
- * Author    Alain Greiner (2016,2017)
+ * Author    Alain Greiner (2016,2017,2018)
  *
  * Copyright (c)  UPMC Sorbonne Universites
@@ -41,5 +41,4 @@
     process_t * process = this->process;
     pid_t       pid     = process->pid;
-    trdid_t     trdid   = this->trdid;
 
 #if DEBUG_SYS_EXIT
@@ -52,60 +51,67 @@
 #endif
 
-    // get owner cluster
-    cxy_t  owner_cxy = CXY_FROM_PID( pid );
+    // get owner process descriptor pointers an cluster
+    xptr_t      owner_xp  = cluster_get_owner_process_from_pid( pid );
+    cxy_t       owner_cxy = GET_CXY( owner_xp );
+    process_t * owner_ptr = GET_PTR( owner_xp );
 
-    // exit must be called by the main thread
-    if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID( trdid ) != 0) )
-    {
-
-#if DEBUG_SYSCALLS_ERROR
-printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n",
-__FUNCTION__, trdid, owner_cxy );
-#endif
-         this->errno = EINVAL;
-         return -1;
-    }
+    // 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 );
 
-    // register exit_status in owner process descriptor
-    process->term_state = status;
+    // 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)
-printk("\n[DBG] %s : set exit status in process term_state\n", __FUNCTION__);
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x deleted threads / process %x\n",
+__FUNCTION__ , this, pid );
 #endif
 
-    // remove process from TXT list
-    process_txt_detach( XPTR( local_cxy , process ) );
+    // mark for delete this calling thread when it is not the main
+    if( (owner_cxy != local_cxy) || (main != this) )
+    {
 
 #if( DEBUG_SYS_EXIT & 1)
-printk("\n[DBG] %s : removed from TXT list\n", __FUNCTION__);
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : calling thread %x deleted itself / process %x\n",
+__FUNCTION__ , this, pid );
+#endif
+        thread_delete( XPTR( local_cxy , this ) , pid , true );
+    }
+         
+    // remove process from TXT list
+    process_txt_detach( owner_xp );
+
+#if( DEBUG_SYS_EXIT & 1)
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x removed process %x from TXT list\n",
+__FUNCTION__ , this, pid );
 #endif
 
-    // mark for delete all process threads in all clusters (but the main) 
-    process_sigaction( pid , DELETE_ALL_THREADS );
+    // block the main thread 
+    thread_block( XPTR( owner_cxy , main ) , THREAD_BLOCKED_GLOBAL );
 
 #if( DEBUG_SYS_EXIT & 1)
-printk("\n[DBG] %s : deleted all other threads than main\n", __FUNCTION__);
-#endif
-
-    // restore IRQs
-    hal_restore_irq( save_sr );
-
-    // block the main thread itself 
-    thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_GLOBAL );
-
-#if( DEBUG_SYS_EXIT & 1)
-printk("\n[DBG] %s : blocked the main thread\n", __FUNCTION__);
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x blocked main thread for process %x\n",
+__FUNCTION__, this , pid );
 #endif
 
     // atomically update owner process descriptor term_state to ask
-    // the parent process sys_wait() function to delete this main thread 
-    hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
-                          PROCESS_TERM_EXIT );
+    // 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) );
 
 #if( DEBUG_SYS_EXIT & 1)
-printk("\n[DBG] %s : set EXIT flag in process term_state\n", __FUNCTION__);
+if( tm_start > DEBUG_SYS_EXIT )
+printk("\n[DBG] %s : thread %x set exit status in process %x term_state\n",
+__FUNCTION__ , this, pid );
 #endif
 
@@ -119,5 +125,5 @@
 #endif
 
-    // main thread deschedule
+    // this thread deschedule
     sched_yield( "process exit" );
 
