title: Implementing thread scheduler round robin and thread cancel
body: <p>I'm very new in C language and linux and English is not my mother language. Sorry for those in advance.</p>
  
  <p>I'm working on school project which is to implement a round robin scheduler on linux and I have some problems on implementing scheduler and thread_self.</p>
  
  <p>Scheduler checks if ready queue is empty first, if yes set time slice and alarm(timeslice). Otherwise, look up a new thread from ready list, dispatch TCB of new thread, set timeslice, context switch to new thread and set alarm(timeslice). But I keep getting error at some point and I couldn't find where to fix.</p>
  
  <p>Another thing is about thread_cancel. int thread_cancel(thread_t tid) function removes target tcb and I need to find the tcb using tid. I tried like</p>
  
  <pre><code>Thread* temp = getpid();
  kill(pid, SIGKILL);
  </code></pre>
  
  <p>but I couldn't figure out how to remove tcb from queue.
  Please give me some better idea!</p>
  
  <p>Thread.h</p>
  
  <pre><code>typedef struct _Thread{
      ThreadStatus status;
      pid_t pid;
      Thread* pPrev;
      Thread* pNext;
  }Thread;
  Thread* ReadyQHead;
  Thread* ReadyQTail;
  </code></pre>
  
  <p>-Queue</p>
  
  <pre><code>typedef struct _queue{
      Thread* head;
      Thread* tail;
      unsigned int num;
  } queue;
  
  queue* runList;
  queue* readyList;
  queue* waitList; 
  </code></pre>
  
  <p>-Queue.c</p>
  
  <pre><code>void enqueue(queue * q, Thread* tcb)
  {
      if(q-&gt;num == 0) {
          q-&gt;head = tcb;
          q-&gt;tail = tcb;
      } else {
          q-&gt;tail-&gt;pNext = tcb;
          q-&gt;tail = tcb;
      }
      q-&gt;num ++;
  }
  
  Thread * dequeue(queue * q)
  {
      Thread * tmp;
      if(q-&gt;num == 0) return NULL;
      else if(q-&gt;num == 1) {
          tmp = q-&gt;head;
          q-&gt;head = NULL;
          q-&gt;tail = NULL;
      } else {
          tmp = q-&gt;head;
          q-&gt;head = q-&gt;head-&gt;pNext;
      }
      q-&gt;num --;
      return tmp;
  }
  </code></pre>
  
  <p>-Scheduler</p>
  
  <pre><code>void alarmHandler(int signal)
  {
      printf("Scheduler awake!!");
      /*Do schedule*/
  }
  
  int RunScheduler( void )
  {
      //check if ready queue is empty
      if(is_empty(readyList) != 0)
      {
          printf("this is weird");
          signal(SIGALRM, alarmHandler);
      }
      else {
          /*Look up a new thread from ready list*/
          Thread* tmp = ReadyQHead;
  
          /*send sigcont*/
          kill(tmp-&gt;pid, SIGCONT);
  
          //dispatch TCB of new thread
          if(is_empty(runList) != 0 &amp;&amp; runList-&gt;head-&gt;status == 0)
              enqueue(readyList, tmp);
  
          //pick thread at head of ready list as first thread to dispatch
          tmp-&gt;status = 0; // tmp == runningTcb
          printf("alive tcb : %d\n", tmp-&gt;pid);
  
          ReadyQHead = dequeue(readyList);
          //set timeslice
          signal(SIGALRM, alarmHandler);
          //context switch to new thread
          _ContextSwitch(tmp-&gt;pid, ReadyQHead-&gt;pid);
  
      }
  
      while(1){
          alarm(TIMESLICE);
      }
  
      return 0;
  }
  
  
  void _ContextSwitch(int curpid, int tpid)
  {
      kill(curpid, SIGSTOP);
      kill(tpid, SIGCONT);
  }
  </code></pre>