Results 1 to 8 of 8
hi,
I've got a problem in using pthread_create(). I'm using Linux OS, version is 2.6.18. I wrote a C program:
#include <pthread.h>
int main()
{
int g_nThOutRet;
int g_nThInRet;
pthread_t ...
- 09-04-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 11
pthread_create - segmentation fault
hi,
I've got a problem in using pthread_create(). I'm using Linux OS, version is 2.6.18. I wrote a C program:
#include <pthread.h>
int main()
{
int g_nThOutRet;
int g_nThInRet;
pthread_t thBuckIn,thBuckOut;
if(g_nThInRet = pthread_create(&thBuckIn,NULL,BucketIn,NULL))
perror("Thread Creation err of IN: ");
printf("\n after creat of IN \n");
pthread_join(thBuckIn,NULL);
printf("\n after join of IN in main \n");
usleep(1);
printf("\n b4 creat of OUT \n");
if(g_nThOutRet =
pthread_create(&thBuckOut,NULL,BucketOut,NULL)) // line # 19
perror("Thread Creation err of OUT: ");
printf("\n afr creat of OUT \n");
pthread_join(thBuckOut,NULL);
printf("\n after join of OUT in main \n");
return 0;
}
void * BucketOut()
{
printf("__BUCKET OUT__ \n");
}
void * BucketIn()
{
printf("__BUCKET IN__ \n");
}
I compiled using GCC and lpthread library. But I've a segmentation fault in call to pthread_create() of thBuckOut. The execution is as follows:
(gdb) run
Starting program: /home/viji/b
[Thread debugging using libthread_db enabled]
[New Thread -1208432960 (LWP 21834)]
[New Thread -1210537072 (LWP 21837)]
after creat of IN
__BUCKET IN___
after join of IN in main
b4 creat of OUT
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208432960 (LWP 21834)]
0x48ee4517 in memset () from /lib/libc.so.6
(gdb) bt
#0 0x48ee4517 in memset () from /lib/libc.so.6
#1 0x48fea7e7 in pthread_create@@GLIBC_2.1 () from /lib/libpthread.so.0
#2 0x080487f7 in main () at b.c:19
What could be the problem for this segmentation fault? Pls help me...Last edited by vijiambav; 09-04-2007 at 12:22 PM. Reason: missed few declarations
- 09-04-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
Declare the the variables thBuckOut and thBuckIn in main() as a pthread_t struct.
BTW place your code within code brackets!
Regards
- 09-04-2007 #3Just Joined!
- Join Date
- Sep 2007
- Posts
- 11
I missed that declaration while posting. Where to include brackets? I didn't get any compilation errors.
- 09-04-2007 #4
He means when you post code on this board, put it in code brackets. It makes it formatted properly, so it's not so offensive to the eyes.
- 09-04-2007 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
The 3th argument of pthread_create() must be a pointer to the thread function.
Try this:
Code:#include <stdio.h> #include <pthread.h> void * BucketOut() { printf("__BUCKET OUT__ \n"); } void * BucketIn() { printf("__BUCKET IN__ \n"); } int main() { int g_nThOutRet; int g_nThInRet; pthread_t thBuckIn,thBuckOut; if(g_nThInRet = pthread_create(&thBuckIn,NULL,&BucketIn,NULL)) perror("Thread Creation err of IN: "); printf("\n after create of IN \n"); pthread_join(thBuckIn,NULL); printf("\n after join of IN in main \n"); usleep(1); printf("\n b4 create of OUT \n"); if(g_nThOutRet = pthread_create(&thBuckOut,NULL,&BucketOut,NULL)) // line # 19 perror("Thread Creation err of OUT: "); printf("\n after create of OUT \n"); pthread_join(thBuckOut,NULL); printf("\n after join of OUT in main \n"); return 0; }
Regards
- 09-05-2007 #6Just Joined!
- Join Date
- Sep 2007
- Posts
- 11
I tried passing pointer to the thread function as 3rd arg to pthread_create(). But that didn't solve the problem, segmentation fault is occuring.
- 09-05-2007 #7Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
On my system it works well, copy and paste the code in your box and add -lpthread to the command line e.g.
RegardsCode:cc progname.c -o progname -lpthread
- 09-06-2007 #8Just Joined!
- Join Date
- Sep 2007
- Posts
- 11
its working fine 4 me too. thanks a bunch


Reply With Quote