使用管道和进程的 Unix C 程序问题
故事是这样的:
用 C 语言编写一个程序,创建一个子进程和一个孙进程(子进程的子进程!)。 父进程应该读取文件的内容,当您运行程序时,该文件的名称将作为参数。 父亲应该将读到的文本发送给孩子,这需要将“o”中找到的字符“a”转换。 更改后的文本会将子项发送给孙子,孙子将计算文本中的行数和“o”字符数,并将结果打印在屏幕上(以及可选的文件 output.txt )。
因此,我创建了一个包含一些 'a' 字符的文件,但我无法真正将这些字符转换为 'o' 。
问题是我应该使用什么样的命令来进行字符转换?
非常感谢任何帮助。
这是我到目前为止所做的:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd_1[2],fd_2[2],pointer,pointer2,pid1,pid2,i,n;
char filename[200];
char buffer[500000];
printf("Input filename:");
fflush(stdout);
fflush(stdin);
scanf("%s", filename);
pointer = open(filename, O_RDONLY);
if (pointer == -1) {
printf("file does not exist\n");
}
pipe(fd_1);
if ( pipe(fd_1) == -1) {
perror("pipe");
exit(1);
}
pid1=fork();
switch(pid1){
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //child process
pipe(fd_2);
if ( pipe(fd_2) == -1) {
perror("pipe");
exit(1);
}
pid2=fork();
switch(pid2) {
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //grandchild process
close (fd_2[1]);
break;
}
default: { //child process
close (fd_1[1]);
dup2(fd_1[0],1);
pointer2 = open(buffer, O_WRONLY);
for (i=0; i< sizeof(buffer) ; i++)
if (buffer[i] == 'a' ) {
buffer[i] = 'o';
}
write(fd_1[1], buffer, sizeof(buffer));
wait(&pid2);
break;
}
}
}
default: { //parent process
close (fd_1[0]);
dup2(fd_1[1],0);
n = read(pointer, buffer, 200);
write(fd_1[1], buffer, n);
wait(&pid1);
break;
}
}
return 0;
}
Here's the story:
Write a program in C that creates a child process and a granchild process (child of a child process!).
The father process should read the contents of a file which name will take as an argument when you run the program.
The father should send the text read to the child, which takes to convert those characters 'a' found in 'o'.
The changed text will send the child to grandchild that (the grandchild) will take to count the number of rows and number of 'o' characters in the text and print the result on the screen (and optionally a file output.txt ).
So i create a file with some 'a' characters inside and i cant really convert those characters to 'o' .
The Question is what kind of command should i use for character converting ?
Any help much appreciated.
Here's what i have done so far :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main() {
int fd_1[2],fd_2[2],pointer,pointer2,pid1,pid2,i,n;
char filename[200];
char buffer[500000];
printf("Input filename:");
fflush(stdout);
fflush(stdin);
scanf("%s", filename);
pointer = open(filename, O_RDONLY);
if (pointer == -1) {
printf("file does not exist\n");
}
pipe(fd_1);
if ( pipe(fd_1) == -1) {
perror("pipe");
exit(1);
}
pid1=fork();
switch(pid1){
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //child process
pipe(fd_2);
if ( pipe(fd_2) == -1) {
perror("pipe");
exit(1);
}
pid2=fork();
switch(pid2) {
case -1: {
perror("fork");
exit(10);
break;
}
case 0: { //grandchild process
close (fd_2[1]);
break;
}
default: { //child process
close (fd_1[1]);
dup2(fd_1[0],1);
pointer2 = open(buffer, O_WRONLY);
for (i=0; i< sizeof(buffer) ; i++)
if (buffer[i] == 'a' ) {
buffer[i] = 'o';
}
write(fd_1[1], buffer, sizeof(buffer));
wait(&pid2);
break;
}
}
}
default: { //parent process
close (fd_1[0]);
dup2(fd_1[1],0);
n = read(pointer, buffer, 200);
write(fd_1[1], buffer, n);
wait(&pid1);
break;
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设孩子已将管道读入缓冲区,则如下所示:
Assuming the child has read the pipe into a buffer then something like: