우분투 및 임베디드 리눅스 (우리 프로젝트 칩)에서 아래 코드를 실행합니다. 그러나 결과는 다릅니다. 임베디드 리눅스에서 실행될 때 스레드 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);
}
당신의'print_message_function'가 잘못된 반환 형식을 가지고, 그것은''무효 * 반환해야하고,''무효 *에 pthread_create''에 당신이 전달하는 기능을 캐스팅하지 않습니다, 'void * (void *)'가 아닌'void *'여야하므로 반환 형식을 고치면 캐스트 할 필요가 없습니다. –