Segmentation fault
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#define WORK_SIZE 256
void* thread_func(void *arg);
sem_t bin_sem;
char work_area[WORK_SIZE];
int main()
{
int ret = -1;
pthread_t thread_id;
void* thread_ret = NULL;
ret = sem_init(&bin_sem, 0, 0);
if (ret != 0)
{
perror("semaphore failed \n");
exit(EXIT_FAILURE);
}
ret = pthread_create(&thread_id, NULL, thread_func, NULL);
if (ret != 0)
{
perror("create thread failed\n");
exit(EXIT_FAILURE);
}
printf("input some text:");
while (strncpy("end", work_area, 3) != 0)
{
fgets(work_area, WORK_SIZE, stdin);
sem_post(&bin_sem);
}
printf("\nwaiting for thread to finish...");
ret = pthread_join(thread_id, &thread_ret);
if (ret != 0)
{
perror("thread join failed\n");
exit(EXIT_FAILURE);
}
printf("thread joined\n");
sem_destroy(&bin_sem);
return 0;
}
void* thread_func(void *arg)
{
sem_wait(&bin_sem);
while (strncpy("end", work_area, 3) != 0)
{
printf("you input %d characters\n", strlen(work_area)-1);
sem_wait(&bin_sem);
}
pthread_exit(NULL);
}
这段程序在LINUX下出现Segmentation fault,我想了半天也没想出来,请教各位