首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

linuxC程序提示段异常,求解

2012-05-24 
linuxC程序提示段错误,求解!#include stdio.h#include unistd.h#include stdlib.h#include string.

linuxC程序提示段错误,求解!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

pthread_mutex_t work_mutex; 
#define WORK_SIZE 1024
char work_area[WORK_SIZE];
char threadlog[WORK_SIZE];

void thread(void *threadlog)
{
int i;
  FILE *fp;
  time_t t;
  
  pthread_mutex_lock(&work_mutex);
sleep(1);
if((fp=fopen((char*)threadlog,"a")) >=0)
{
t=time(0);
fprintf(fp,"I'm here at %s\n",asctime(localtime(&t)) );
fprintf(fp,"Thread id is %d\n",pthread_self());

fclose(fp);
pthread_mutex_unlock(&work_mutex);
}
else
printf("parameter :arg error!\n");

pthread_exit(NULL);
}


main(void)
{
pthread_t id1,id2,id3;
int ret;


ret = pthread_mutex_init(&work_mutex, NULL);
if (ret != 0) 
{
perror("Mutex initialization failed");
exit(EXIT_FAILURE);
}

ret=pthread_create(&id1,NULL,(void*)thread,(void*)threadlog);
if(ret!=0)
{
printf("Create pthread error!\n");
exit(1);
}

ret=pthread_create(&id2,NULL,(void*)thread,(void*)threadlog);
if(ret!=0)
{
printf("Create pthread error!\n");
exit(1);
}

ret=pthread_create(&id3,NULL,(void*)thread,(void*)threadlog);
if(ret!=0)
{
printf("Create pthread error!\n");
exit(1);
}

pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
printf("Finished!\n");

return(0);
}

[解决办法]

C/C++ code
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <semaphore.h>pthread_mutex_t work_mutex;  #define WORK_SIZE 1024char work_area[WORK_SIZE];char threadlog[WORK_SIZE];void thread(void *threadlog){        int i;          FILE *fp;            time_t t;                                     pthread_mutex_lock(&work_mutex);                  sleep(1);                  if((fp=fopen((char*)threadlog,"a")) != NULL) //                  {                          t=time(NULL); //                          fprintf(fp,"I'm here at %s\n",asctime(localtime(&t)) );                          fprintf(fp,"Thread id is %u\n",pthread_self());                          fclose(fp);                          pthread_mutex_unlock(&work_mutex);                  }                  else {                          printf("parameter :arg error!\n");                          pthread_mutex_unlock(&work_mutex); //                  }                  pthread_exit(NULL);}int main(void){        pthread_t id1,id2,id3;        int ret;        strncpy(threadlog, "test.txt", sizeof(threadlog)); //        ret = pthread_mutex_init(&work_mutex, NULL);        if (ret != 0)          {                perror("Mutex initialization failed");                exit(EXIT_FAILURE);        }        ret=pthread_create(&id1,NULL,(void*)thread,(void*)threadlog);        if(ret!=0)        {                printf("Create pthread error!\n");                exit(1);        }        ret=pthread_create(&id2,NULL,(void*)thread,(void*)threadlog);        if(ret!=0)        {                printf("Create pthread error!\n");                exit(1);        }        ret=pthread_create(&id3,NULL,(void*)thread,(void*)threadlog);        if(ret!=0)        {                printf("Create pthread error!\n");                exit(1);        }        pthread_join(id1,NULL);        pthread_join(id2,NULL);        pthread_join(id3,NULL);        printf("Finished!\n");        return(0);} 

热点排行