0

i am creating a thread that will captures packets and will store some information in a structure "flow" for each packet. i am using static array of "flow" type structures. but when i run the program it retuns SIGSEGV error. here's the structure "flow":

typedef struct flow
{
    unsigned int s_port;
    unsigned int d_port;
    char s_addr[20];
    char d_addr[20];
    int spi;
    short total; 
    short data[10000];
    struct timeval prev_t;
    double ipt[10000]; 
    flowParam info;
    char status[100];

}flow;

note that flowParam is another structure whose object info is included in "flow". i also run program by commenting it but got same result...

and here's the main program:

int main()
{
    pthread_t tid;

    int err = pthread_create(&tid, NULL, Capture, NULL);
        if (err != 0){
            perror("\ncan't create capturing thread");
            exit(-1);
        }
        else
            printf("\nCapturing thread created!\n");

    pthread_join(tid, NULL);
    printf("Finished!!");
    return 0;
}

void* Capture()
{
    flow Register[5000]; /* flow Register */
    //counter Counter[5000]; 
    pthread_exit(NULL);

}

interestingly, when i use another structure "counter" and make its array in thread, it does not give such error.

typedef struct counter
{
    char s_addr[20];
    char d_addr[20];    
}counter;

i tried to my best to solve this issue but could not find any clue.any help???

User_890
  • 169
  • 1
  • 9
  • 2
    Local variables are usually stored on the *stack* which is a limited resource. On Linux the default stack size is 8MB. Your array of `sizeof(flow) * 5000` is most definitely larger than that (in fact it will be way over 500MB!) The `ipt` member by itself is 80000 bytes, multiply that with 5000 and you have 400000000 bytes of storage. – Some programmer dude Jun 16 '17 at 12:19
  • it means i have to make flow register[5000] global??? or any better alternative??? i actually have to pass this Register to another thread2 after every 5 seconds using pipes so as thread1 continuously write to this Register without any locking mechanism... – User_890 Jun 16 '17 at 12:34

2 Answers2

0

Your struct flow is very large, over 100KB. An array of 5000 of those is roughly 500MB. By making Register a local variable, which most likely lives on the stack, it is way too large and overflows the stack. This causes the segfault.

You should instead allocate memory for it dynamically. It's still a big structure, but there's a better chance of having memory for it on the heap.

void* Capture()
{
    flow *Register = malloc(5000 * sizeof(flow));
    ...
    free(Register);
    pthread_exit(NULL);

}
dbush
  • 205,898
  • 23
  • 218
  • 273
0

Your struct flow is a bit over 100kB. Then you drop 5000 of those on the stack. That's 500MB of stack needed. Your system has limits on how much you can put on the stack and 500MB is definitely too much. Threads impose additional limits on how much you can put on the stack so they are not helping here, but I'm pretty sure this would go bad without threads.

Art
  • 19,807
  • 1
  • 34
  • 60