CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    16

    segmentation fault

    Code:
    # include <pthread.h>
    
    # include <stdio.h>
    
    # include <stdlib.h>
    
    # define NUM_THREADS 10
    
    
    
    void *print(void*tid)
    
    {tid; 
    
     printf("hello from thread  \n");
    
      int x=32768;
    
    
    
    while(x>0)
    
    {	x=x-1;
    
           
    
           }
    
    
    
    int result;
    
    int check1,check2; 
    
    
    
    check1=pthread_attr_getscope(tid,&result);
    
      printf("scope %d \n",result);
    
    
    
    check2=pthread_attr_getstacksize(tid,&result);
    
      printf("stack size %d \n",result);
    
         
    
    if((check1!=0)||(check2!=0))
    
      printf("error getting attributes");
    
    
    
    pthread_exit(NULL);   }
    
    
    
    
    
    void *destruct(void* tth)
    
    {int status;
    
     status=pthread_attr_destroy(tth);
    
    
    
    if(status=0)
    
       printf("attribute object destroyed sucessfully \n");
    
    else
    
       printf("error %d destroying attribute object \n",status);
    
    
    
     pthread_t d=(pthread_t)tth;
    
    status=pthread_cancel(d);
    
    
    
    if(status=0)
    
       printf("thread destroyed sucessfully");
    
    else
    
       printf("error %d destroying thread ",status);
    
    
    
    
    
    }
    
    
    
    
    
    int main()
    
    {
    
       pthread_t threads[NUM_THREADS],destr,vict;
    
    int status,i;
    
    
    
    for(i=0;i<NUM_THREADS;i++){
    
    	printf("creation of thread %d \n",i);
    
    	status=pthread_create(&threads[i],NULL,print,(void*)threads[i]);
    
    	
    
            if(status!=0){
    
    	  printf("error code %d \n",status);
    
              exit(-1);
    
               }}
    
    status=pthread_create(&vict,NULL,NULL,NULL);
    
     if(status!=0){
    
    	 printf("error creating victim thread %d \n",status);
    
        
    
        }
    
    
    
    status=pthread_create(&destr,NULL,destruct,(void*)vict);
    
    	
    
          if(status!=0){
    
    	 printf("error creating destroyer thread %d \n",status);
    
        
    
        }
    
        
    
    
    
    
    
    }
    i have the following problem
    when i execute it i get sometimes segmentation fault and other times the program finishes without an error but it does not execute the three last lines

  2. #2
    Join Date
    Dec 2011
    Posts
    1

    Re: segmentation fault

    hey i got the same excercise... where r u from?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured