我正在嘗試通過兩個線程(主線程和第二個線程)之間的管道發送一些數據,但我得到關於文件描述符的errno 9。 我認爲文件描述符在涉及線程時被複制,但在這裏似乎不是這種情況。 它通常會返回「從bf嗨讀」,但它不會。 你能幫我解決這個問題嗎? 謝謝。 下面是代碼管道不通過pthread_t
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
pthread_mutex_t mutex;
int tube[2];
void * fonction(){
if(close(tube[0])==-1){
perror("close error \n");
exit(EXIT_FAILURE);
}
printf("son \n");
if(write(tube[1],"hi",2)<0){
perror("write error \n");
exit(EXIT_FAILURE);
}
printf("errno %d \n",errno);
pthread_exit(NULL);
}
int main(){
pthread_t a;
if(pipe(tube)==-1){
perror("pipe error \n");
exit(EXIT_FAILURE);
}
char buffer[2];
pthread_mutex_init(&mutex,NULL);
close(tube[1]);
pthread_create(&a,NULL,fonction,&tube[1]);
pthread_join(a,NULL);
read(tube[0],buffer,2);
printf("read from bf %s \n",buffer);
return EXIT_SUCCESS;
}
PS:作爲timrau這裏建議(Pthread_t not starting)我用在pthread_join,但我想這不是做
我想這是因爲我使用close(),但我不知道它 – kevin556