segmentation fault while using setjmp

From: naveen (joga2052_at_yahoo.com)
Date: 06/29/04


Date: 28 Jun 2004 21:14:54 -0700

Hello *,

Here I'm trying to switch control between three threads in a cycle -
parent, child1, child2 in that order.
But after the first cycle of execution i get a segmentation fault. I
guess the problem is with the stack allocated for each thread. I tried
increasing the stack size and i still get the segmentation fault, but
this time after completing a few more cycles.
Can anybody explain this:

#include <stdio.h>
#include <setjmp.h>
#include <unistd.h>
#include <signal.h>

sigjmp_buf context[3];
int crt = 0;

void parent()
{
      while(1)
        fprintf(stderr, "Parent process\n");
}

void child(int *i)
{
      while(1)
        fprintf(stderr, "Child process %d\n", *i);
}

/* create child context and return to parent */
void create(int i)
{
      if (sigsetjmp(context[i], 1)) {
        child(&crt);
      } else {
        char stack[1024];
        siglongjmp(context[0], -1);
      }
}

/* context switch */
void run_thread()
{
      int next = (crt + 1) % 3;

      if (!sigsetjmp(context[crt], 1)) {
        crt = next;
        siglongjmp(context[crt], -1);
      }
}

void handler(int sig)
{
      signal(SIGALRM, handler);
      alarm(1);

      run_thread();
}

main()
{
      signal(SIGALRM, handler);
      alarm(1);

      if (!sigsetjmp(context[0], 1)) {
        create(1);
      }
      if (!sigsetjmp(context[0], 1)) {
        create(2);
      }
      parent();
}



Relevant Pages

  • segmentation fault while using setjmp
    ... parent, child1, child2 in that order. ... But after the first cycle of execution i get a segmentation fault. ... void child ...
    (comp.os.linux.development.system)
  • segmentation fault while using setjmp
    ... parent, child1, child2 in that order. ... But after the first cycle of execution i get a segmentation fault. ... void child ...
    (comp.os.linux)
  • Re: Use a DC in a worker thread
    ... opportunities to update the progress bar, one for each little piece you did. ... typedef void ... CWnd * parent = NULL); ... static void SetRange; ...
    (microsoft.public.vc.mfc)
  • Re: Cant copy the background of my simple control
    ... I made a simple control that should simply paint a dragable circle on ... correct background of the parent. ... void CSimpleCircleDrawer::OnPaint ... //Work out the new position (in parent client coordinates) ...
    (microsoft.public.vc.mfc)
  • Re: Threading Faux Pa
    ... the Parent class data from the database and loads all the objects. ... a TCPClient that is wrapped in a 'communicator' class. ... public class ObjectModelController ... private void LoadObjectModel() ...
    (microsoft.public.dotnet.languages.csharp)