linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* makecontext(3)
@ 2003-07-28 19:06 kkonaka
  2003-07-29 11:39 ` makecontext(3) Luciano Miguel Ferreira Rocha
  0 siblings, 1 reply; 3+ messages in thread
From: kkonaka @ 2003-07-28 19:06 UTC (permalink / raw)
  To: linux-c-programming

on the surface(?) calls to makecontext() appear to have to be 
preceded by getcontext() to completely initialize its arg: ucontext_t
struct (that is, if I comment out getcontext() calls from a
program like the below, it causes SEGV). why is it the case?
kenji

---
#include <ucontext.h>
#include <unistd.h>
#include <err.h>

static ucontext_t u0;
static ucontext_t u1;
static ucontext_t u2;

#define STACK_SIZE (1024 * 8)

#define NLOOP 7

static int i;
static int j;

static void f()
{
  for (i = 0; i < NLOOP; i++) {
    printf("f(): i = %d\n", i);
    if (swapcontext(&u1, &u2) < 0)
      err(1, NULL);
  }
}

static void g()
{
  for (j = 0; j < NLOOP; j++) {
    printf("g(): j = %d\n", j);
    if (swapcontext(&u2, &u0) < 0)
      err(1, NULL);
  }
}

main()
{
  static unsigned char _u1_stack[STACK_SIZE];
  static unsigned char _u2_stack[STACK_SIZE];

  getcontext(&u1);
  u1.uc_stack.ss_sp = _u1_stack;
  u1.uc_stack.ss_size = sizeof _u1_stack;
  u1.uc_stack.ss_flags = 0;
  u1.uc_link = NULL;
  makecontext(&u1, f, 0);

  getcontext(&u2);
  u2.uc_stack.ss_sp = _u2_stack;
  u2.uc_stack.ss_size = sizeof _u2_stack;
  u2.uc_stack.ss_flags = 0;
  u2.uc_link = NULL;
  makecontext(&u2, g, 0);

  while (1) {
    if (swapcontext(&u0, &u1) < 0)
      err(1, NULL);
    printf("main: %d %d\n", i, j);
    if (i == NLOOP && j == NLOOP)
      exit(0);
  }
}
--

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: makecontext(3)
  2003-07-28 19:06 makecontext(3) kkonaka
@ 2003-07-29 11:39 ` Luciano Miguel Ferreira Rocha
  2003-07-30 13:49   ` makecontext(3) kkonaka
  0 siblings, 1 reply; 3+ messages in thread
From: Luciano Miguel Ferreira Rocha @ 2003-07-29 11:39 UTC (permalink / raw)
  To: kkonaka; +Cc: linux-c-programming

On Mon, Jul 28, 2003 at 03:06:30PM -0400, kkonaka@mac.com wrote:
> on the surface(?) calls to makecontext() appear to have to be 
> preceded by getcontext() to completely initialize its arg: ucontext_t
> struct (that is, if I comment out getcontext() calls from a
> program like the below, it causes SEGV). why is it the case?
> kenji

Hello,

From the manual page:
       The makecontext() function modifies  the  context  pointed  to  by  ucp
       (which was obtained from a call to getcontext()).  Before calling make-
       context(), one should allocate a new stack for this context,  assigning
       to   ucp->uc_stack,  and  define  a  successor  context,  assigning  to
       ucp->uc_link. 

I assume that the getcontext() is needed to get values for registers
(cs, ds, es, gs, ...).

Regards,
Luciano Rocha

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: makecontext(3)
  2003-07-29 11:39 ` makecontext(3) Luciano Miguel Ferreira Rocha
@ 2003-07-30 13:49   ` kkonaka
  0 siblings, 0 replies; 3+ messages in thread
From: kkonaka @ 2003-07-30 13:49 UTC (permalink / raw)
  To: luciano, linux-c-programming

howdy thanks,

> > makecontext() appear to have to be 
> > preceded by getcontext() to completely initialize its arg
> I assume that the getcontext() is needed to get values for registers
> (cs, ds, es, gs, ...).

hmm., that certainly is part of the answer :) but, why makecontext()
doesn't/cannot fill up some appropriate(??) values automatically into
such fields? -- it looks to me the machine context makecontext() have
to fabricate is simply the one to start calling the given function as
one of its argument (= its second arg: "void *func()"). and it appears
to me that this new machine context doesn't have anything to do with
that of the caller. this make me think getcontext() may not be
necessary. -- ??  (well, obviously I'm missing something)

kenji

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-07-30 13:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-07-28 19:06 makecontext(3) kkonaka
2003-07-29 11:39 ` makecontext(3) Luciano Miguel Ferreira Rocha
2003-07-30 13:49   ` makecontext(3) kkonaka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).