Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pthread_join crash

882 views
Skip to first unread message

somenath

unread,
May 14, 2012, 11:52:15 PM5/14/12
to somena...@emc.com
Hi All,
I have the following program crashing.

#include <pthread.h>
#include <stdio.h>
/*
* Thread start routine.
*/
void *thread_routine (void *arg)
{
int i =0;
while (i ++ <5 ) {
printf("i = %d\n",i);
sleep(3);
}
return arg;
}

int main (int argc, char *argv[])
{
pthread_t thread_id[2];
void *thread_result;
int status;
int i =0;
while ( i ++ <2) {
status = pthread_create (&thread_id[i], NULL, thread_routine,
NULL);
if (status != 0)
puts ( "Create thread");
}
i = 0;
while ( i ++ <2) {
status = pthread_join (thread_id[i], &thread_result);
if (status != 0)
puts ( "Join thread");
}
if (thread_result == NULL)
return 0;
else
return 1;
}

When I run the program it yields the following

./a.out
i = 1
i = 1
i = 2
i = 2
i = 3
i = 3
i = 4
i = 4
i = 5
i = 5
Segmentation fault (core dumped)

When I load the core file in GDB it says the following

Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0 0x47a57c6e in pthread_join () from /lib/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install
glibc-2.13.90-9.i686


Now if i change pthread_join (thread_id[i], &thread_result); as
follows
status = pthread_join (thread_id[i], NULL);

Then the program does not crash.
Where I am going wrong? Why the earlier code is crashing?
Please provide some inputs.

Also I failed to understand the printf result .I have just added one
printf after the while before join and the rest of the code remains
unchanged.

int main (int argc, char *argv[])
{
pthread_t thread_id[2];
void *thread_result;
int status;
int i =0;
while ( i ++ <2) {
status = pthread_create (&thread_id[i], NULL, thread_routine,
NULL);
if (status != 0)
puts ( "Create thread");
}
i = 0;
while ( i++ <2) {
printf("\n VAL of i = %d\n");
status = pthread_join (thread_id[i], NULL);
if (status != 0)
puts ( "Join thread");
}
if (thread_result == NULL)
return 0;
else
return 1;
}

The o/p of the program is
VAL of i = 0
i = 1
i = 1
i = 2
i = 2
i = 3
i = 3
i = 4
i = 4
i = 5
i = 5

VAL of i = 0
============================
Why it is printing as VAL of i = 0. Shouldn't it be 1 ?

Please help me to understand the behaviour.

I am using the following system

Linux gayan 2.6.38.6-26.rc1.fc15.i686.PAE #1 SMP Mon May 9 20:36:50
UTC 2011 i686 i686 i386 GNU/Linux

Thread model: posix
gcc version 4.6.0 20110428 (Red Hat 4.6.0-6) (GCC)

Regards,
Somenath

Philip Guenther

unread,
May 15, 2012, 1:56:33 AM5/15/12
to
On May 14, 8:52 pm, somenath <somenath...@gmail.com> wrote:
> I have the following program crashing.
>
>  #include <pthread.h>
> #include <stdio.h>
...
>  int main (int argc, char *argv[])
>  {
>      pthread_t thread_id[2];
>      void *thread_result;
>      int status;
>          int i =0;
>          while ( i ++ <2) {
>                  status = pthread_create (&thread_id[i], NULL, thread_routine,
> NULL);
>                  if (status != 0)
>                          puts ( "Create thread");
>          }

What values does 'i' have *inside* the loop? Where are the thread ids
stored?


Philip Guenther

somen...@gmail.com

unread,
May 15, 2012, 3:28:32 AM5/15/12
to
On Tuesday, May 15, 2012 11:26:33 AM UTC+5:30, Philip Guenther wrote:
> On May 14, 8:52 pm, somenath <somenath...@gmail.com> wrote:
> > I have the following program crashing.
> >
> >  #include
>
> > #include <stdio.h>
> ...
> >  int main (int argc, char *argv[])
> >  {
> >      pthread_t thread_id[2];
> >      void *thread_result;
> >      int status;
> >          int i =0;
> >          while ( i ++ <2) {
> >                  status = pthread_create (&thread_id[i], NULL, thread_routine,
> > NULL);
> >                  if (status != 0)
> >                          puts ( "Create thread");
> >          }
>
> What values does 'i' have *inside* the loop? Where are the thread ids
> stored?
>
I got the problem. I am going one more in thread_id array.

So for the other question

"VAL of i = 0
============================
Why it is printing as VAL of i = 0. Shouldn't it be 1 ? "
Is it because of undefined behavior caused by the array out bound access as well ?

Thanks for the help.


Arun Chandrasekaran

unread,
May 15, 2012, 4:51:02 AM5/15/12
to
> "VAL of i = 0
> ============================
> Why it is printing as VAL of i = 0. Shouldn't it be 1 ? "
> Is it because of undefined behavior caused by the array out bound access as well ?

i = 0;
while ( i++ <2) {
printf("\n VAL of i = %d\n");

Check the arguments for printf.

Richard Kettlewell

unread,
May 15, 2012, 6:16:15 AM5/15/12
to
somenath <somen...@gmail.com> writes:
> pthread_t thread_id[2];
> void *thread_result;
> int status;
> int i =0;
> while ( i ++ <2) {
> status = pthread_create (&thread_id[i], NULL, thread_routine,
> NULL);

You are writing past the end of thread_id[].

> printf("\n VAL of i = %d\n");

You aren't supplying i to printf. The compiler can spot this for you,
but you need to turn warnings on.

--
http://www.greenend.org.uk/rjk/

somen...@gmail.com

unread,
May 15, 2012, 7:03:54 AM5/15/12
to
On Tuesday, May 15, 2012 3:46:15 PM UTC+5:30, Richard Kettlewell wrote:
> somenath writes:
> > pthread_t thread_id[2];
> > void *thread_result;
> > int status;
> > int i =0;
> > while ( i ++ <2) {
> > status = pthread_create (&thread_id[i], NULL, thread_routine,
> > NULL);
>
> You are writing past the end of thread_id[].
>
> > printf("\n VAL of i = %d\n");
>
> You aren't supplying i to printf. The compiler can spot this for you,
> but you need to turn warnings on.
>

Many thanks for the help.
0 new messages