所以这应该做的是fork,让子进程获取文件的文本,然后让父进程修改该文本并将其写入新文件。我有各种奇怪的东西。整个代码大致是这个。
#include <iostream>
#include <termios.h>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
int parentPID;
int main(int argc, char* argv[]){
parentPID = getpid();
int pipey[2];
int worked = pipe(pipey);
if( worked == - 1){
cout << "Oops. Didn't make a pipe.";
}
//cout << "About to fork!!!";
fork();
if(getpid() != parentPID){//Only run in the child process
char* argvec1[3] = {"cat", "colorfile.txt", (char*)0};
dup2(pipey[1], 1);
execv("/bin/cat", argvec1);
}
else{//Only run in the parent process.
int someInt;
cout << "In the parent process";
pid_t status = wait(&someInt);
dup2(pipey[0], 0);
creat("newfile.txt", 0777);
chmod("newfile.txt", 0777);
int targetFile = open("newfile.txt", O_WRONLY);
if(targetFile == -1){
cout << "\nOops, couldn't open targetFile, ";
perror("because ");
}
else{
cout << "\nOpened target file.";
}
dup2(targetFile, 1);
//char* argvec2[] = {"sed", "-e", "s/color/colour/g", (char*)0};
//execv("/bin/sed", argvec2);
cout << "something went terribly wrong";
}
}
特别麻烦的是三件事,第一件事,这段代码......
creat("newfile.txt", 0777);
chmod("newfile.txt", 0777);
int targetFile = open("newfile.txt", O_WRONLY);
if(targetFile == -1){
cout << "\nOops, couldn't open targetFile, ";
perror("because ");
}
else{
cout << "\nOpened target file.";
}
dup2(targetFile, 1);
...不会将“打开的目标文件”写入标准输出。相反,它把它放在newfile.txt中,所以dup2正在改变输出命令之前出现的输出?...如果我注释掉dup2,那么它就不会发生,它肯定是特定的dup2打电话让它发生。
第二,这段代码片段......
creat("newfile.txt", 0777);
chmod("newfile.txt", 0777);
int targetFile = open("newfile.txt", O_WRONLY);
if(targetFile == -1){
cout << "\nOops, couldn't open targetFile, ";
perror("because ");
}
else{
cout << "\nOpened target file.";
}
//dup2(targetFile, 1);
char* argvec2[] = {"sed", "-e", "s/color/colour/g", (char*)0};
execv("/bin/sed", argvec2);
cout << "something went terribly wrong";
...根本没有输出关于打开文件的任何成功/失败。它打印出原始文件的内容,经过适当修改,但不会终止。它只是一种存在,直到我使用ctrl-C来杀死当前进程。最后的cout不会显示。
最后,这......
creat("newfile.txt", 0777);
chmod("newfile.txt", 0777);
int targetFile = open("newfile.txt", O_WRONLY);
if(targetFile == -1){
cout << "\nOops, couldn't open targetFile, ";
perror("because ");
}
else{
cout << "\nOpened target file.";
}
dup2(targetFile, 1);
char* argvec2[] = {"sed", "-e", "s/color/colour/g", (char*)0};
execv("/bin/sed", argvec2);
cout << "something went terribly wrong";
...不会给我输出标准输出或newfile.txt。
感觉这些系统调用中的一些只是以他们想要的顺序执行,半独立于我编写它们的顺序,因此很难用它们做任何事情。
答案 0 :(得分:0)
使用名为'colorfile.txt'的输入文件,如:
color
color color
color color color
color color color color
我更新了您的代码,以不同方式执行重复和调试消息。最重要的更改(正如我在您对您的问题的评论中所提到的)是关闭父进程中管道的写入端,以避免该进程挂起。同样重要的(用于调试目的,并处理有关事件顺序的混淆)是使用'endl'来刷新输出。否则,当仍然保持缓冲数据时,会发生dup2调用,并且可以在写入之后更改该数据的目标,因为在基础库刷新输出时(例如,在进程退出时)会发生写入。
请注意,对于管道,我使用术语“写入侧”和“读取侧”来分别引用由 pipe(2)创建的小型文件描述符数组的索引1和0 :管道的用户写入并从中读取的边。
这些更新针对Linux 3.10.17内核上的g ++(GCC)4.8.2。据推测,其他人的包括等等会有所不同。
#include <iostream>
#include <cstdio>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
int main() {
int pipey[2];
if (pipe(pipey) == -1) {
perror("pipe failed");
return 1;
}
cout << "About to fork!!!" << endl;
pid_t pid = fork();
if (pid == -1) {
perror("fork failed");
return 1;
}
if (pid == 0) { // child process
const char *av[] = { "cat", "colorfile.txt", 0 };
cout << "In the child process" << endl;
cout << "colorful child message before dup2" << endl;
dup2(pipey[1], 1);
close(pipey[1]);
cout << "colorful child message after dup2" << endl;
close(pipey[0]);
execvp(av[0], (char * const *) av);
perror("failed to exec cat");
return 1;
} else { // parent process.
const char *av[] = { "sed", "-e", "s/color/colour/g", 0 };
cout << "In the parent process" << endl;
int targetFd = open("newfile.txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
if (targetFd == -1){
perror("failed to open newfile.txt");
return 1;
} else {
cout << "Opened target file." << endl;
}
cout << "colorful parent message before dup2s" << endl;
dup2(pipey[0], 0);
close(pipey[0]);
dup2(targetFd, 1);
close(targetFd);
cout << "colorful parent message after dup2s" << endl;
// The soon-to-be exec'd cat process will have the read side of
// the pipe duped to its stdin, and the child process created
// above *will* write data that cat will see, but unless the
// write side is closed by *all* its writers prior to exec (and
// this parent process is one of the writers), cat will see no
// end-of-file on its stdin.
//
// Note how easily this deadlock can be created, within a single
// process.
close(pipey[1]);
execvp(av[0], (char * const *) av);
perror("failed to exec sed");
return 1;
}
}
当我运行时,我看到:
About to fork!!!
In the parent process
In the child process
colorful child message before dup2
Opened target file.
colorful parent message before dup2s
输出文件newfile.txt的内容是:
colorful parent message after dup2s
colourful child message after dup2
colour
colour colour
colour colour colour
colour colour colour colour
如果你理解为什么一条消息是彩色的而另一条消息是丰富多彩的,那么你就会抓住它。