2014-09-09 2 views
0

우분투 및 임베디드 리눅스 (우리 프로젝트 칩)에서 아래 코드를 실행합니다. 그러나 결과는 다릅니다. 임베디드 리눅스에서 실행될 때 스레드 errno가 0 인 이유는 무엇입니까? 어떻게 동일한 출력을 얻을 수 있습니까? pthread_create() 함수가 errno를 설정할 수 있습니까?pthread_create() 함수에 의해 errno 설정 (문제)

게다가 나는 pthread_create() 함수 대신에 fork() 함수를 사용했습니다. 이 예에서는 우분투와 동일한 결과물을 얻었습니다. 예 :

if (fork()) 
    print_message_function("foo"); 

우분투 :

>>main errno: 2 
>>thread errno: 2 

임베디드 Linux :

>>main errno: 2 
>>thread errno: 0 
포크() 함수를 사용

임베디드 Linux :

>>main errno: 2 
>>thread errno: 2 

#include <unistd.h>  /* Symbolic Constants */ 
#include <sys/types.h> /* Primitive System Data Types */ 
#include <errno.h>  /* Errors */ 
#include <stdio.h>  /* Input/Output */ 
#include <stdlib.h>  /* General Utilities */ 
#include <pthread.h> /* POSIX Threads */ 
#include <string.h>  /* String handling */ 
#include <time.h> 

typedef struct str_thdata 
{ 
    int thread_no; 
    char message[100]; 
} thdata; 

int createError(char *str) 
{ 
    int retVal = 0; 

    if (NULL == fopen("YOK", "r")) 
    { 
     printf(">>%s errno: %d\n",str, errno); 
     retVal = -errno; 
    } 
    return retVal; 
} 

void print_message_function (void *ptr) 
{ 
    int retVal; 
    thdata *data; 
    data = (thdata *) ptr; 

    retVal = createError("thread"); 

    while(1) 
     ; 

    pthread_exit(0); 
} 

int main() 
{ 
int retVal = 0; 
    pthread_t thread1; 
    thdata data1; 

    data1.thread_no = 1; 
    strcpy(data1.message, "Hello!"); 


    /*if (fork()) 
     print_message_function("foo"); 
    */ 
    pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1); 

    retVal = createError("main"); 
    while(1) 
    { 

    } 

    pthread_join(thread1, NULL); 

    exit(0); 
} 
+0

당신의'print_message_function'가 잘못된 반환 형식을 가지고, 그것은''무효 * 반환해야하고,''무효 *에 pthread_create''에 당신이 전달하는 기능을 캐스팅하지 않습니다, 'void * (void *)'가 아닌'void *'여야하므로 반환 형식을 고치면 캐스트 할 필요가 없습니다. –

답변

2

P 스레드 함수는를 반환 오류 발 ue, 그러나 errno을 설정하지 마십시오. 이는 errno이 pthread가 설계되었을 때 스레드 특정 적이 아니었기 때문일 수 있습니다.

사용 예 :

int err = pthread_create(...); 
if(err) { 
    fprintf(stderr, "pthread_create: (%d)%s\n", err, strerror(err)); 
    exit(EXIT_FAILURE); 
} 
+0

답변 해 주셔서 감사합니다. 하지만 thread_create에 의해 호출 된 print_message_function에 errno를 설정하고 싶습니다. –

+0

@ zafer-victory _ print_message_function_에 errno를 설정하고 싶습니다. –