segmentation fault怎么解决 求解
#include <stdio.h>#include <semaphore.h>
#include <pthread.h>
#include <fcntl.h>
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
sem_t* t=NULL;
void* thread(void* arg)
{
//sleep(10);
int status=pthread_mutex_lock(&mutex);
if(status)
{
printf("can't lock mutex\n");
exit(1);
}
sem_wait(t);
int value;
printf("pthread id is %d get resource\n",pthread_self());
sem_getvalue(t,&value);
printf("now the semaphore value is %d\n",value);
status=pthread_mutex_unlock(&mutex);
if(status)
{
printf("can't not unlock mutex\n");
exit(1);
}
}
int main()
{
pthread_t pid;
int status=pthread_mutex_lock(&mutex);
if(status)
{
printf("can't lock mutex\n");
exit(1);
}
sem_init(t,0,3);
status=pthread_mutex_unlock(&mutex);
if(status)
{
printf("can't unlock mutex\n");
exit(1);
}
int i=0;
while(i++<3)
if((pthread_create(&pid,NULL,thread,NULL))!=0)
{
printf("pthread can't create.\n");
exit(0);
}
pthread_join(pid,NULL);
sem_destroy(t);
return 0;
}