Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #859 > unrolled thread

pthread_join crash

Started bysomenath <somenathpal@gmail.com>
First post2012-05-14 20:52 -0700
Last post2012-05-15 04:03 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.programming.threads


Contents

  pthread_join  crash somenath <somenathpal@gmail.com> - 2012-05-14 20:52 -0700
    Re: pthread_join crash Philip Guenther <guenther@gmail.com> - 2012-05-14 22:56 -0700
      Re: pthread_join crash somenathpal@gmail.com - 2012-05-15 00:28 -0700
        Re: pthread_join crash Arun Chandrasekaran <visionofarun@gmail.com> - 2012-05-15 01:51 -0700
    Re: pthread_join  crash Richard Kettlewell <rjk@greenend.org.uk> - 2012-05-15 11:16 +0100
      Re: pthread_join  crash somenathpal@gmail.com - 2012-05-15 04:03 -0700

#859 — pthread_join crash

Fromsomenath <somenathpal@gmail.com>
Date2012-05-14 20:52 -0700
Subjectpthread_join crash
Message-ID<9a956a1e-3d3e-4a98-b24b-beae12f40a03@n9g2000pbi.googlegroups.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

[toc] | [next] | [standalone]


#860 — Re: pthread_join crash

FromPhilip Guenther <guenther@gmail.com>
Date2012-05-14 22:56 -0700
SubjectRe: pthread_join crash
Message-ID<81db5b42-ec9a-4274-a4da-faf8b54f8161@jx17g2000pbb.googlegroups.com>
In reply to#859
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

[toc] | [prev] | [next] | [standalone]


#861 — Re: pthread_join crash

Fromsomenathpal@gmail.com
Date2012-05-15 00:28 -0700
SubjectRe: pthread_join crash
Message-ID<1074960.584.1337066912671.JavaMail.geo-discussion-forums@ynjm4>
In reply to#860
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. 

[toc] | [prev] | [next] | [standalone]


#862 — Re: pthread_join crash

FromArun Chandrasekaran <visionofarun@gmail.com>
Date2012-05-15 01:51 -0700
SubjectRe: pthread_join crash
Message-ID<12957154.1299.1337071862228.JavaMail.geo-discussion-forums@pbcge2>
In reply to#861
> "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.

[toc] | [prev] | [next] | [standalone]


#863

FromRichard Kettlewell <rjk@greenend.org.uk>
Date2012-05-15 11:16 +0100
Message-ID<87k40d7nrk.fsf@araminta.anjou.terraraq.org.uk>
In reply to#859
somenath <somenathpal@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/

[toc] | [prev] | [next] | [standalone]


#864

Fromsomenathpal@gmail.com
Date2012-05-15 04:03 -0700
Message-ID<8599463.3435.1337079834269.JavaMail.geo-discussion-forums@ynja13>
In reply to#863
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming.threads


csiph-web