使用管道和进程的 Unix C 程序问题

发布于 2024-09-30 02:44:53 字数 2147 浏览 6 评论 0原文

故事是这样的:

用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

a√萤火虫的光℡ 2024-10-07 02:44:53

假设孩子已将管道读入缓冲区,则如下所示:

for (int i = 0; i < bytes_read; ++i)
{
    if (buffer[i] == 'a')
        buffer[i] = 'o';
}

Assuming the child has read the pipe into a buffer then something like:

for (int i = 0; i < bytes_read; ++i)
{
    if (buffer[i] == 'a')
        buffer[i] = 'o';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文