title: Implementing a user-level thread library but code keep seg-faulting, any ideas why?
body: <p>So as the title says, my code keeps seg-faulting unfortunately. I'm pretty sure I malloc'd everything correctly and I believe that my functions are correct for the test case I have but it still seg-faults. I just wanted to see if anyone could maybe spot why it may be seg-faulting? Here is my code that I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ucontext.h&gt;
  #include "t_lib.h"
  #include &lt;sys/mman.h&gt;
  
  struct ready_queue *ready;
  struct tcb *running;
  
  void t_yield()
  {
      tcb *tmp;
      tmp-&gt;thread_context = running-&gt;thread_context;
  
      running-&gt;thread_context = ready-&gt;head-&gt;thread_context;
      ready-&gt;head-&gt;thread_context = tmp-&gt;thread_context;
      swapcontext(ready-&gt;head-&gt;thread_context, running-&gt;thread_context);
  }
  
  void t_init()
  {
      tcb *tmp;
      tmp = (tcb *) malloc(sizeof(tcb));
  
      running = NULL;
      ready-&gt;head = ready-&gt;tail = NULL;
  }
  
  void t_create(void (*fct)(int), int id, int pri)
  {
      size_t sz = 0x10000;
  
      tcb *tmp;
      tmp = (tcb *) malloc(sizeof(tcb));
  
      ucontext_t *uc;
      uc = (ucontext_t *) malloc(sizeof(ucontext_t));
  
      tmp-&gt;thread_context = uc;
      tmp-&gt;thread_priority = pri;
      tmp-&gt;thread_id = id;
      getcontext(uc);
      uc-&gt;uc_stack.ss_sp = mmap(0, sz,
          PROT_READ | PROT_WRITE | PROT_EXEC,
          MAP_PRIVATE | MAP_ANON, -1, 0);
      uc-&gt;uc_stack.ss_size = sz;
      uc-&gt;uc_stack.ss_flags = 0;
      uc-&gt;uc_link = running-&gt;thread_context;
  
      makecontext(uc, fct, 1, id);
  
      if(running == NULL)
          running = tmp;
      else
      {
          if(ready-&gt;head == NULL)
          {
               ready-&gt;head = tmp;
               ready-&gt;tail = tmp;
          }
          else
          {
               ready-&gt;tail-&gt;next = tmp;
               ready-&gt;tail = tmp;
          }
      }
   }
  </code></pre>
  
  <p>TEST CASE</p>
  
  <pre><code>void assign(int pri)
  {
    int i;
  
    for (i = 0; i &lt; 3; i++)
      printf("in assign(1): %d\n", i);
  
    t_yield();
  
    for (i = 10; i &lt; 13; i++)
      printf("in assign(2): %d\n", i);
  
    t_yield();
  
    for (i = 20; i &lt; 23; i++)
      printf("in assign(3): %d\n", i);
  }
  
  int main(int argc, char **argv)
  {
    t_init();
    t_create(assign, 1, 1);
  
    printf("in main(): 0\n");
  
    t_yield();
  
    printf("in main(): 1\n");
    t_yield();
  
    printf("in main(): 2\n");
    t_yield();
  
    printf("done...\n");
  
    return (0);
  
  }
  </code></pre>
  
  <p>VALGRIND RESULTS:</p>
  
  <pre><code>==4596== Memcheck, a memory error detector
  ==4596== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
  ==4596== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
  ==4596== Command: ./test00
  ==4596==
  ==4596== Invalid write of size 8
  ==4596==    at 0x400810: t_init (t_lib.c:24)
  ==4596==    by 0x40072C: main (test00.c:24)
  ==4596==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
  ==4596==
  ==4596==
  ==4596== Process terminating with default action of signal 11 (SIGSEGV)
  ==4596==  Access not within mapped region at address 0x8
  ==4596==    at 0x400810: t_init (t_lib.c:24)
  ==4596==    by 0x40072C: main (test00.c:24)
  ==4596==  If you believe this happened as a result of a stack
  ==4596==  overflow in your program's main thread (unlikely but
  ==4596==  possible), you can try to increase the size of the
  ==4596==  main thread stack using the --main-stacksize= flag.
  ==4596==  The main thread stack size used in this run was 10485760.
  ==4596==
  ==4596== HEAP SUMMARY:
  ==4596==     in use at exit: 0 bytes in 0 blocks
  ==4596==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
  ==4596==
  ==4596== All heap blocks were freed -- no leaks are possible
  ==4596==
  ==4596== For counts of detected and suppressed errors, rerun with: -v
  ==4596== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
  </code></pre>