Click to See Complete Forum and Search --> : segmentation fault


killbill689
December 3rd, 2011, 09:33 AM
# 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

thanostlotr
December 5th, 2011, 09:11 AM
hey i got the same excercise... where r u from?