Thread: A segmentation fault occurs when thread_kill is executed.

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    9

    A segmentation fault occurs when thread_kill is executed.

    I am developing a simple program in C on linux in which there is a thread that checks the time elapsed since the beginning of the program and when they have passed 10 seconds, sends an end signal (SIGINT or SIGTERM, for example) to the main program, what should make it end in a clean way.

    The C program is attached at the end.

    When the call to thread_kill is executed, a Segmentation fault occurs.

    The result of running this program with gdb is as follows:

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread 0x7ffff77f2700 (LWP 25472)]
    __pthread_kill (threadid=25466, signo=2) at ../nptl/sysdeps/unix/sysv/linux/pthread_kill.c:42
    42    ../nptl/sysdeps/unix/sysv/linux/pthread_kill.c: No such file or directory.
    (gdb) bt
    #0  __pthread_kill (threadid=25466, signo=2) at ../nptl/sysdeps/unix/sysv/linux/pthread_kill.c:42
    #1  0x0000000000400a1e in ThreadRoutine (number=1) at program_16b.c:46
    #2  0x00007ffff7bc4184 in start_thread (arg=0x7ffff77f2700) at pthread_create.c:312
    #3  0x00007ffff78f103d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
    (gdb)

    What is wrong on my code?




    program.c :

    Code:
    #include <pthread.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/time.h>
    #include <signal.h>
    
    
    #define _REENTRANT
    
    
    struct timeval begin, end;
    
    
    void siginthandler(int param)
    {
      printf("User has pressed Ctrl + C\n");
    
    }
    
    
    
    
    void *ThreadRoutine(int number)
    {
      long microseconds;
    
    
      printf ("[thread %d] pthread_self = %d, getpid = %d, getppid = %d\n",
               number, pthread_self(), getpid(), getppid());
    
    
      while(1) // loop forever
      {
        printf("thread type 1 (%d) running\n",number);
        if (number == 1)
        {
          gettimeofday(&end, NULL);
          microseconds = (end.tv_sec - begin.tv_sec) * 1000000 + (end.tv_usec - begin.tv_usec);
          printf ("Microseconds from starting time = %ld\n", microseconds);
          if (microseconds >= 10000000)
          {
            printf("Killing parent process after 10 seconds from starting.\n");
            pthread_kill(getppid(), SIGINT);
          }    
        }
        sleep(number); // sleep for number seconds, value passed as a parameter.
      }
    }
    
    
    int main(void)
    {
      int t;
      pthread_t tid[13]; // an array to keep track of the threads
    
      // To create a detached thread.
      pthread_attr_t attr; // thread attribute
    
      // To call to pthread_attr_get...
      int rc, val;
      struct sched_param sp;
      size_t v;
      void *stkaddr;
    
      signal(SIGINT, siginthandler);
    
      printf ("[parent] pid = %d\n", getpid());
    
      gettimeofday(&begin, NULL);
    
      for (t=1; t<3; t++)
        {
        pthread_create(&tid[t],NULL,(void *)ThreadRoutine,(int *)t);
        printf ("Created thread %d with id=%d\n", t, (int)tid[t]);
        }
    
    sleep(30);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    Solved: If I use pthread_self() instead of getppid() the programs works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Getting a segmentation fault.
    By mgracecar in forum C Programming
    Replies: 13
    Last Post: 02-22-2012, 09:23 PM
  3. Segmentation Fault Help
    By Sergio Lopez in forum C Programming
    Replies: 6
    Last Post: 02-21-2012, 05:34 PM
  4. Error occurs when adding #include?
    By Dest in forum C Programming
    Replies: 6
    Last Post: 04-11-2010, 08:40 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread