我想獲得一個工作在C代碼而不是調用命令行查找的套接字。C:unix套接字 - >破管
傳遞給child2函數'cut'的孩子3'sort'函數工作正常,並且當執行時包含所有3個子函數時,程序在父進程waitpid()中卡住。
我試圖隔離參與套接字的唯一孩子,當我在gdb上運行可執行文件時,我收到消息「find:'standard output':Broken pipe」和「find:write error」
這裏的兩個子功能與套接字交互的例子: 兒童1:
void child1()
{
int sock;
struct sockaddr_un remote;
sock = socket(AF_UNIX, SOCK_STREAM, 0)
memset(&remote, 0, sizeof(struct sockaddr_un));
remote.sun_family = AF_UNIX;
strncpy(remote.sun_path, "socket", sizeof(remote.sun_path) - 1);
while((connect(sock, (struct sockaddr *)&remote, (socklen_t)sizeof(remote))) == -1)
{
if(errno != ENOENT && errno != ECONNREFUSED)
erro("child 2 failed to connect to socket");
}
dup2(sock, 1);
close(sock);
execlp("find", "find", ".", "-type" , "f", "-ls", NULL);
}
而且CHILD2:
void child2(int *pipe_fd)
{
int sock;
struct sockaddr_un local, remote;
socklen_t sock_size = (socklen_t)sizeof(remote);
sock= socket(AF_UNIX, SOCK_STREAM, 0);
memset(&local, 0, sizeof(struct sockaddr_un));
memset(&remote, 0, sizeof(struct sockaddr_un));
local.sun_family = AF_UNIX;
strncpy(local.sun_path, "socket", sizeof(local.sun_path) - 1);
unlink("socket");
bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_un));
listen(sock, 1);
sock = accept(sock,(struct sockaddr *)&remote, &sock_size));
dup2(sock, STDOUT_FILENO);
close(sock);
close(pipe_fd[0]);
dup2(pipe_fd[1],1);
close(pipe_fd[1]);
execlp("cut", "cut", "-d", " ", "-f", "3-", NULL);
}
沒有什麼東東d在具體解決這個問題時,我只是想了解我在創建過程中做了什麼錯誤,所以我不會在將來再做這件事。 我預先提供任何幫助。
兩個孩子都在寫同一個插座。那真的是你想要的嗎? – Barmar
'child2'永遠不會執行任何操作。 – Barmar
爲什麼'child2'將stdout連接到套接字?它不應該是讀者嗎? – Barmar