Threading Segmentation fault


 
Thread Tools Search this Thread
Top Forums Programming Threading Segmentation fault
# 8  
Old 01-14-2010
It means you didn't compile it with the -ggdb flag.
# 9  
Old 01-14-2010
Hello,

I didn't performed a deep analysis of your code, but I might shed some lights on the problem you're facing.

Quote:
Originally Posted by Corona688
I also don't see that main() is waiting for the receiving thread to finish... It should. You can't create a thread and quit the same way you can fork a process and quit because when main's thread quits, the entire process is gone.
Yes, generally speaking your are correct. But notice here that main() calls pthread_exit(). In this case, the program will happily continue to perform until the thread Recv_Handle ceases to exit (assuming that the process only have these two threads). Detailed explanations about the main thread semantic can be found here: The main thread.

However I see a fundamental flaw: the way the receivedSocket is passed to the Recv_Handle thread. In the original posted code, you have a race condition. Indeed receivedSocket is a local variable stored on the main's stack. However, when the main() executes the pthread_exit(), receivedSocket becomes then undefined (indeed, the stack is usually located on a memory that is freed upon pthread_exit). When Recv_Handle thread accesses this variable, possibly main has already executed pthread_exit(). This can happen, because of the asynchronous nature of pthread_create(). At this point, all bets are off and you get a SIGSEGV.

The following article explains you how to pass (correctly) arguments between threads: Pthreads arguments passing.

Quote:
Originally Posted by jim mcnamara
Try pthread_join() to wait for the thread to end.
This is may provide a help, but nevertheless the program remains incorrect because of the way the argument is passed, as explained here: Joinable and Detached Threads.

Besides that, in the original code, you are closing the recvSocket... recvfrom() in Recv_Handle thread should fail. If I find some times tomorrow, I'll review your code thoroughly.

HTH,
Loïc.

Last edited by Loic Domaigne; 01-15-2010 at 01:31 PM.. Reason: grammar
# 10  
Old 01-15-2010
Hello,

FYI- The posted program seems to work correctly if you fix the argument passing and don't close the recvSocket.

Cheers,
Loïc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

3. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

4. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

5. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

6. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

7. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

8. Linux

Segmentation fault

Hi, on a linux Red HAT(with Oracle DB 9.2.0.7) I have following error : RMAN> delete obsolete; RMAN retention policy will be applied to the command RMAN retention policy is set to redundancy 2 using channel ORA_DISK_1 Segmentation fault What does it mean ? And the solution ? Many thanks. (0 Replies)
Discussion started by: big123456
0 Replies

9. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies
Login or Register to Ask a Question