我已經編寫了用於在使用流的無關進程之間傳遞文件描述符的代碼。 服務器應等待客戶端發送文件描述符。 這裏是服務器代碼:爲什麼ioctl()不阻塞?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stropts.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
int pipefd[2];
pipe(pipefd);
close(pipefd[1]);
recvfd(pipefd[0]);
return 0;
}
void recvfd(int p)
{
struct strrecvfd rfdbuf;
struct stat statbuf;
int i;
i=ioctl(p, I_RECVFD, &rfdbuf);
printf("errno=%d\n",errno);
printf("recvfd=%d\n", rfdbuf.fd);
}
但我收到錯誤編號9 - 錯誤的文件描述符。
您使用的是什麼操作系統?在不知道操作系統的情況下很難回答有關操作系統調用的問題。 – Dipstick 2009-06-07 20:33:47
你爲什麼認爲它返回一個錯誤?該代碼甚至不檢查ioctl返回值。如果它不是-1,則errno不會更改,並且可能具有先前系統調用的值(例如,在動態加載器或啓動例程中)。 – mark4o 2009-06-07 21:42:17