Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 具有管道的多个子进程_C_String_Pointers_Pipe_Child Process - Fatal编程技术网

C 具有管道的多个子进程

C 具有管道的多个子进程,c,string,pointers,pipe,child-process,C,String,Pointers,Pipe,Child Process,我编写了一个C程序,该程序应该创建一定数量的子进程,每个子进程必须更改字符串中的一个字母。从键盘读取字符串和子进程数 例如: 输入: 3 Apples 输出: Applex Appldx Apqldx 我的问题是:只有家长会改一个字母,而孩子们不会。 不确定我做错了什么。非常感谢您的帮助,提前谢谢 编辑: 我觉得我不够清楚。我想用管子做。 它应该是这样工作的:父级更改一个字母,然后第一个子级接受父级修改的字符串并再更改一个字母。 第二个子项接受由第一个子项修改的字符串(已经更改了两个字母

我编写了一个C程序,该程序应该创建一定数量的子进程,每个子进程必须更改字符串中的一个字母。从键盘读取字符串和子进程数

例如:

输入:

3 Apples  
输出:

Applex Appldx Apqldx
我的问题是:只有家长会改一个字母,而孩子们不会。 不确定我做错了什么。非常感谢您的帮助,提前谢谢

编辑: 我觉得我不够清楚。我想用管子做。 它应该是这样工作的:父级更改一个字母,然后第一个子级接受父级修改的字符串并再更改一个字母。 第二个子项接受由第一个子项修改的字符串(已经更改了两个字母),然后再更改一个,以此类推。我是C新手,不太清楚它是如何工作的,尤其是管道。 也可以通过管道将子级链接到它们之间,也可以将唯一的子级链接到父级,它必须是这样的:第一个子级更改字母,将字符串返回给父级,然后第二个子级从父级读取,修改字母并返回。 如果是这样的话,有没有办法确保这种情况不会发生:苹果变成AppleD,然后变成AppleX,然后变成AppleQ

这是我的密码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include <sys/wait.h>


void error(char* msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}

static void modify(char msg[]) {
    srand(time(NULL));
    int pos1=rand()%((int)strlen(msg));
    srand(time(NULL));
    int pos2=rand()%26;
    srand(time(NULL));
    int big=rand()%2;
    if(big==1) {
        msg[pos1]=(char)(((int)'A')+pos2);
    }
    else {
        msg[pos1]=(char)(((int)'a')+pos2);
    }
    return;
}

int main(int argc, char *argv[])
{
if(argc!=3) {
    error("Wrong number of arguments\n");
}
int nrch;
nrch=atoi(argv[1]);
char* msg=argv[2];

printf("Parent: erhalten: %s\n", msg);
int i=0;
modify(argv[2]);
printf("Parent: weiter: %s\n", msg);
srand(time(NULL));
pid_t pids[10];
int fd[2];

/* Start children. */
for (i = 0; i < nrch; ++i) {
    if (pipe(fd) == -1) {
        error("Can’t create the pipe");
    }
    if ((pids[i] = fork()) < 0) {
            error("Can't fork process");
    } 
    else if (pids[i] == 0) {
        //dup2(fd[1], 1);
        //close(fd[0]);
        printf("child%d: erhalten: %s\n", (i+1), msg);
            modify(msg);
        printf("child%d: weiter: %s\n", (i+1), msg);
            exit(0);
    }
}

/* Wait for children to exit. */
int status;
pid_t pid;
while (nrch > 0) {
    pid = wait(&status);
    printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
    --nrch; 
}   

}
#包括
#包括
#包括
#包括
#包括
#包括
无效错误(字符*消息)
{
fprintf(stderr,“%s\n”,msg);
出口(1);
}
静态无效修改(字符消息[]){
srand(时间(空));
int pos1=rand()%((int)strlen(msg));
srand(时间(空));
int pos2=rand()%26;
srand(时间(空));
int big=rand()%2;
如果(大==1){
msg[pos1]=(char)((int)'A')+pos2;
}
否则{
msg[pos1]=(char)((int)'a')+pos2;
}
返回;
}
int main(int argc,char*argv[])
{
如果(argc!=3){
错误(“错误的参数数量\n”);
}
int-nrch;
nrch=atoi(argv[1]);
char*msg=argv[2];
printf(“父项:erhalten:%s\n”,msg);
int i=0;
修改(argv[2]);
printf(“父项:weiter:%s\n”,msg);
srand(时间(空));
pid_t pid[10];
int-fd[2];
/*生孩子*/
对于(i=0;i0){
pid=等待(&状态);
printf(“PID为%ld的子项已退出,状态为0x%x。\n”,(长)PID,状态);
--nrch;
}   
}

@user3386109,这一严格标准将适用于90%的SO帖子。当孩子们发帖时,他们也会复制消息。所以他们只是在修改自己的副本。您需要将其结果发送回父级。另外,为什么pids[]需要是一个数组?每个孩子都有自己的副本。@Jim我觉得我不够清楚。我想用管子做。它应该是这样工作的:父级更改一个字母,然后第一个子级接受父级修改的字符串并再更改一个字母。第二个子项接受由第一个子项修改的字符串(已经更改了两个字母),然后再更改一个,以此类推。我是C新手,不太清楚它是如何工作的,尤其是管道。@Jim也可以通过管道将子对象链接到它们之间,或者唯一的子对象可以链接到父对象,它必须是这样的:第一个子对象更改一个字母,将字符串返回给父对象,然后第二个子对象从那里读取,修改字母并返回。如果是这样的话,有没有办法确保这种情况不会发生:苹果变成AppleD,然后变成AppleX,然后变成AppleQ?