This is the mail archive of the frysk@sources.redhat.com mailing list for the frysk project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: half exited task testing


New simplified version. It sets up one thread and a handler so that the thread can be explicitly killed.


#define _GNU_SOURCE

#include <sys/types.h>
#include <linux/unistd.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "util.h"

void
usage ()
{
  printf ("\
Usage:\n\
Where:\n\
    <pid> <signal>   Manager process to signal, and signal to use, once\n\
                     an operation has completed (e.g., running, thread\n\
                     created, thread exited).\n\
");
  _exit (1);
}

static int sig;
static int pid;
static int sec;
static pthread_t thread_id;

static void
handler (int sig)
{
  trace ("received signal %d (%s)\n", sig, strsignal (sig));
  trace ("kill %d\n", (int) thread_id);
  tkill (thread_id, SIGKILL);
  kill (pid, sig); // ack.
}

void *
op_thread (void *np)
{
  thread_id = gettid ();
  trace ("PID=%d TTID=%d\n", (int)*((pthread_t*)np), gettid ());
  kill (pid, sig); // ack.
  sleep (sec);
  return NULL;
}

int
main (int argc, char *argv[])
{
  pthread_t p;
  pthread_t threads;
  pthread_attr_t pthread_attr;

  if (argc < 4)
    usage ();
  pid = atol (argv[1]);
  sig = atol (argv[2]);
  sec = atol (argv[3]);

  pthread_attr_init (&pthread_attr);
  pthread_attr_setstacksize (&pthread_attr, PTHREAD_STACK_MIN);

  signal (SIGUSR2, handler);  	// Corresponds to delClone method.

  threads = gettid();
  if (pthread_create (&p, &pthread_attr, op_thread, &threads)) {
    perror ("pthread_create");
    _exit (1);
  }
  trace ("%d:%d\n", (int) getpid (), (int) thread_id);

  OK (pthread_join, (p, NULL));
  trace ("%d:%d", (int) getpid (), (int) thread_id);

  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]