for*_*uce 1 c pipe exec execve
我正在为学校做一个项目,我不确定我试图解决它的方式是否可行。该项目涉及制作一个程序,分叉出 2 个孩子,然后必须用其他程序替换他们的 pid,并让 2 个孩子通过使用 read() 和 write() 通过管道交谈。
我的问题是使用 execve 并将管道传递给那个孩子。我现在所拥有的是:
父程序 - 分叉并让孩子调用 execve:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 16
/* 2 cmd line arguments
1. file already in system (to be copied)
2. file to be created (the copy)
create pipe (for communication) + 2 child processes
first child replace pid with reader
second child with writer
wait for both children to terminate before exiting
*/
int main(int argc, char* argv[]){
//making the pipe
int pfd[2];
pipe(pfd);
int num_dead;
//forking
pid_t reader_child_pid;
pid_t writer_child_pid;
pid_t child_pid;
//args for each fork
char *args_1[] = {argv[1], (char *) 0};
char *args_2[] = {argv[2], (char *) 0};
switch(writer_child_pid = fork()) {
case -1:
perror("fork failed");
return 1;
case 0:
close(pfd[1]);
dup2(pfd[0], 0);
execve("./writer", args_2, NULL);
perror("execve failed");
break;
default:
switch(reader_child_pid = fork()) {
case -1:
perror("2nd fork failed");
return 1;
case 0:
close(pfd[0]);
dup2(pfd[1], 1);
execve("./reader", args_1, NULL);
perror("execve failed");
break;
default:
//close(pfd[0]);
//close(pfd[1]);
break;
}
}
num_dead = 0;
for(;;) {
if((child_pid=wait(NULL)) == -1){
if(errno == ECHILD) {
printf("NO MORE CHILDREN");
exit(0);
} else {
perror("wait error");
exit(1);
}
}
++num_dead;
printf("wait() returned 1 child");
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用 dup2 重定向到标准输入和标准输出。然后在孩子们中,我尝试像这样读取和写入 stdout 和 stdin:
孩子 - 从标准输入读取数据
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
printf("\n\nWRITER\n");
/* ready to process info
read the file, write it to pipe */
int wri_inFile = open(argv[0], O_WRONLY | O_CREAT | O_TRUNC | S_IRUSR | S_IWUSR);
char buf[BUF_SIZE];
int read_test;
for(;;) {
read_test = read(0, buf, BUF_SIZE);
if(read_test == 0) //eof
break;
write(wri_inFile, buf, BUF_SIZE);
}
close(wri_inFile);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我正在按照您的建议使用 dup,但我不确定我是否像我应该的那样访问 stdin 和 stdout。
--这是另一个孩子仅供参考
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
printf("\n\nREADER\n");
/* ready to process info
read the file, write it to pipe */
printf("FILE::%s", argv[0]);
int inFile = open(argv[0], O_RDONLY);
char buf[BUF_SIZE];
int read_test;
for(;;) {
read_test = read(inFile, buf, BUF_SIZE);
if(read_test == 0) //eof
break;
write(1, buf, BUF_SIZE);
}
close(argv[0][1]);
close(inFile);
printf("\nDONE WITH READER\n");
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
--如果它有任何区别,似乎另一个孩子(写入管道的那个)正在成功写入它,但上面的孩子从未从管道中读取并且上面的 outFile 总是空的。
* *仍然发生同样的事情
我不是在找你为我解决问题,我只是真的被卡住了,不知道我是否做错了什么。再次感谢您的任何帮助。
我正在积极寻找可以使用的示例,但我找不到任何显示他们执行的孩子们的代码的代码,这就是我现在的问题所在。
使用管道时,必须确保进程没有保持管道打开(除非进程实际上正在使用管道)。特别是,在这种情况下,父进程必须关闭管道的两端,否则读取器将永远不会得到 EOF 指示。
在更仔细地阅读其他两个程序之后,我看到读取器程序读取它给定的文件并将该内容写入管道,而写入器程序将它从管道读取的数据写入给定的文件。
此代码是关于将起作用的最小更改:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
/* 2 cmd line arguments
1. file already in system (to be copied)
2. file to be created (the copy)
create pipe (for communication) + 2 child processes
first child replace pid with reader
second child with writer
wait for both children to terminate before exiting
*/
int main(int argc, char* argv[])
{
//making the pipe
int pfd[2];
pipe(pfd); // Error check omitted!
int num_dead;
//forking
pid_t reader_child_pid;
pid_t writer_child_pid;
pid_t child_pid;
if (argc != 3)
{
fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
return 1;
}
//args for each fork
char *args_1[] = { "reader", argv[1], (char *) 0 };
char *args_2[] = { "writer", argv[2], (char *) 0 };
switch (writer_child_pid = fork()) {
case -1:
perror("fork failed");
return 1;
case 0:
// writer reads from standard input (pipe) and writes to named file
dup2(pfd[0], 0); // Error check omitted
close(pfd[0]);
close(pfd[1]);
execve("./writer", args_2, NULL);
perror("execve failed");
return 1;
default:
switch (reader_child_pid = fork()) {
case -1:
perror("2nd fork failed");
return 1;
case 0:
//reader reads from the named file and writes to the pipe
dup2(pfd[1], 1);
close(pfd[0]);
close(pfd[1]);
execve("./reader", args_1, NULL);
perror("execve failed");
return 1;
default:
// The parent closes both ends of the pipe
close(pfd[0]);
close(pfd[1]);
break;
}
}
num_dead = 0;
for(;;) {
if ((child_pid = wait(NULL)) == -1){
if (errno == ECHILD) {
printf("NO MORE CHILDREN\n");
exit(0);
} else {
perror("wait error");
exit(1);
}
}
++num_dead;
printf("wait() returned 1 child (%d)\n", (int)child_pid);
}
return(0);
}
Run Code Online (Sandbox Code Playgroud)
请注意,每个子级和父级在完成工作之前都会关闭管道文件描述符。
我可能不会将嵌套开关用于控制流,我可能会使用函数来处理每个子级的处理:
if ((child1 = fork()) == -1)
...error...
else if (child1 == 0)
launch_reader(pfd, args_1);
else if ((child2 = fork()) == -1)
...error...
else if (child2 == 0)
launch_writer(pfd, args_2);
...parent...
Run Code Online (Sandbox Code Playgroud)
我还会有一个函数来封装“报告错误并退出”,即使它只有两行长。另外,printf()如果您确实希望消息及时出现,请确保消息以换行符结尾。
| 归档时间: |
|
| 查看次数: |
13359 次 |
| 最近记录: |