Pthread_create causing segmentation fault (C++, Kubutnu 15)

111 Views Asked by At

I am in shock and confusion, be honestly. After reading several dozen topics on "pthread_create causing segmentation fault" I still have this problem. I did everything that was ordered, and the result is zero: at the stage of debug, program gives segmentation fault on the line creation process.

I already do not understand anything. I would be grateful for your help. Thanks in advance.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <unistd.h>
#include <pthread.h>
#include <interfacemanager.h>
using namespace std;

InterfaceManager IFACE;



void* watch_for_interfaces(void*)
{
    IFACE.monitoring();
}



int main()
{

        sigset_t   *set;
        sigemptyset(&(*set));
        sigaddset(&(*set), SIGINT);
        sigaddset(&(*set), SIGTERM);

        pthread_t th1, th2;
        int i=1;
        if (pthread_create(&th2, NULL, watch_for_interfaces, (void*)&i) != 0) {
          perror("th2 error\n");
          exit(1);
        }




        printf ("Terminating\nPress eny key\n");
        getchar();

        exit(EXIT_SUCCESS);

        return 0;
    }
1

There are 1 best solutions below

1
On BEST ANSWER

The problem is not with pthread_create but with your signal functions.

    sigset_t   *set;
    sigemptyset(&(*set));
    sigaddset(&(*set), SIGINT);
    sigaddset(&(*set), SIGTERM);

Here, you are using an uninitialized variable set and passing it to signal functions which is undefined. You could malloc it but it's necessary you can write it as

    sigset_t   set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGTERM);