Index: /soft/giet_vm/giet_kernel/sys_handler.c
===================================================================
--- /soft/giet_vm/giet_kernel/sys_handler.c	(revision 697)
+++ /soft/giet_vm/giet_kernel/sys_handler.c	(revision 698)
@@ -90,5 +90,5 @@
 
 __attribute__((section(".kdata")))
-unsigned int _tty_channel_allocator    = 1;
+unsigned int _tty_channel[NB_TTY_CHANNELS]  = {1};
 
 __attribute__((section(".kdata")))
@@ -744,11 +744,24 @@
 int _sys_tty_alloc( unsigned int shared )
 {
-    // get a new TTY terminal index
-    unsigned int channel = _atomic_increment( &_tty_channel_allocator, 1 );
-
+    unsigned int channel;
+
+    if ( _get_context_slot( CTX_TTY_ID ) < NB_TTY_CHANNELS )
+    {
+        _printf("\n[GIET_ERROR] in _sys_tty_alloc() : TTY channel already allocated\n");
+        return 0;
+    }
+
+    // get a new TTY channel
+    for ( channel = 0 ; channel < NB_TTY_CHANNELS ; channel++ )
+    {
+        if ( !_tty_channel[channel] )
+        {
+            _tty_channel[channel] = 1;
+            break;
+        }
+    }
     if ( channel >= NB_TTY_CHANNELS )
     {
-        _atomic_increment( &_tty_channel_allocator , 0xFFFFFFFF );
-        _printf("\n[GIET_ERROR] in _sys_tty_alloc() : not enough TTY channels\n");
+        _printf("\n[GIET_ERROR] in _sys_tty_alloc() : no TTY channel available\n");
         return -1;
     }
@@ -782,5 +795,10 @@
             unsigned int ltid          = task[task_id].ltid;
             static_scheduler_t* psched = (static_scheduler_t*)_schedulers[x][y][p];
-            psched->context[ltid][CTX_TTY_ID] = channel;
+
+            // don't overwrite TTY_ID
+            if ( psched->context[ltid][CTX_TTY_ID] >= NB_TTY_CHANNELS )
+            {
+                psched->context[ltid][CTX_TTY_ID] = channel;
+            }
         }
     }
@@ -797,9 +815,43 @@
 int _sys_tty_release()
 {
+    unsigned int channel = _get_context_slot( CTX_TTY_ID );
+
+    if ( channel == -1 )
+    {
+        _printf("\n[GIET_ERROR] in _sys_tty_alloc() : TTY channel already released\n");
+        return -1;
+    }
+
     // release WTI mailbox
-    if ( USE_PIC ) _ext_irq_release( ISR_TTY_RX , _get_context_slot( CTX_TTY_ID ) );
-
-    // release one TTY terminal
-    _atomic_increment( &_tty_channel_allocator , 0xFFFFFFFF );
+    if ( USE_PIC ) _ext_irq_release( ISR_TTY_RX , channel );
+
+    // reset CTX_TTY_ID for all tasks in vspace
+    unsigned int      vspace_id = _get_context_slot( CTX_VSID_ID );
+    mapping_header_t  *header   = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
+    mapping_vspace_t  *vspace   = _get_vspace_base(header);
+    mapping_task_t    *task     = _get_task_base(header);
+
+    unsigned int task_id;
+    for (task_id = vspace[vspace_id].task_offset;
+         task_id < (vspace[vspace_id].task_offset + vspace[vspace_id].tasks);
+         task_id++)
+    {
+        unsigned int y_size        = header->y_size;
+        unsigned int cid           = task[task_id].clusterid;
+        unsigned int x             = cid / y_size;
+        unsigned int y             = cid % y_size;
+        unsigned int p             = task[task_id].proclocid;
+        unsigned int ltid          = task[task_id].ltid;
+        static_scheduler_t* psched = (static_scheduler_t*)_schedulers[x][y][p];
+
+        // only clear matching TTY_ID
+        if ( psched->context[ltid][CTX_TTY_ID] == channel )
+        {
+            psched->context[ltid][CTX_TTY_ID] = -1;
+        }
+    }
+
+    // release TTY channel
+    _tty_channel[channel] = 0;
 
     return 0;
