LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2004, 12:17 PM   #1
41JokerNAM
LQ Newbie
 
Registered: Apr 2003
Location: Longview, TX
Distribution: Mandriva
Posts: 5

Rep: Reputation: 0
pthreads and segmentation fault


I'm trying to learn about pthreads for a class I'm taking. I found a nice little threaded program online, at hpcf.nersc.gov/vendor_docs/ibm/aixprggd/genprogc/create_threads.htm and tried it out:

--------------------------------------------------------------------------------
//thread.cpp
#include <pthread.h>
#include <iostream>
#include <fstream>
#include <unistd.h>

void *Thread(void *string)
{
while(1)
printf("%s\n", (char *)string);
pthread_exit(NULL);
}

int main()
{
char *e_str = "Hello";
char *f_str = "Bonjour";

pthread_t e_th;
pthread_t f_th;

int rc;

rc = pthread_create(&e_th, NULL, Thread, (void *)e_str);
if(rc)
exit(-1);
rc = pthread_create(&f_th, NULL, Thread, (void *)f_str);
if(rc)
exit(-1);
sleep(5);

exit(0);
}
---------------------------------------------------------------------------------

My Makefile:
----------------------------------------------------------------------------------
LDFLAGS=-lstdc++

all: main

clean:
-rm -f main thread.o

main: thread.o
thread.o: thread.cpp
--------------------------------------------------------------------------------

I run Make, and it works fine. But when I try to run the program all it give me is:
"Segmentation Fault"

I'm running this thing on a Knoppix 3.3 install, if it helps any.

Assistance appreciated. I'm a pretty Linux programmer, and barely know what I'm doing. A friend wrote the Makefile for another program, and I adapted it for this one.

I saw some very old stuff online that said some libraries don't seem to like pthreads, but nothing in recent years.
 
Old 03-27-2004, 12:46 PM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
i dont know bout knoppix ...
but you seem to be mixing c++ and c up (not that you can't, but in this instance there is nothing in the code that requires any of the C++ libs)
just copy whatever code is on that webpage u referred to, save as thread.c and run a 'gcc thread.c -Wall -lpthread'. (i think you need to link it with the pthread lib - that might be one of the reasons for the segfault?)

also, maybe you could run an trace on the executable, and see where in the exec it throws the segfault?
 
Old 03-27-2004, 03:18 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Modify the code to link with -lpthread, that should make it run.
Mixing C and C++ code is OK.

One thing here: using printf in threads is not recommended when you don't use mutexes/semaphores to make sure only one thread's is sending to screen.
 
Old 03-27-2004, 03:35 PM   #4
41JokerNAM
LQ Newbie
 
Registered: Apr 2003
Location: Longview, TX
Distribution: Mandriva
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the help. Added this line to Makefile:

LDFLAGS += -lpthread

Also, removed the line about main: Wasn't needed for this program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault satimis Linux From Scratch 4 07-11-2005 08:01 PM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
C Segmentation Fault fatman Programming 20 04-02-2003 05:16 PM
segmentation fault sqn Slackware 3 03-21-2003 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration