gpt4 book ai didi

进程与管道之间的通信

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:52 25 4
gpt4 key购买 nike

我正在尝试实现一个程序,该程序在输入中采用一系列参数,并依赖于它创建由管道实现的相同数量的进程,其中每个进程写入管道,然后将其传递给父亲。

这是我的代码,它没有做我需要的。

谢谢你的帮助。

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



int main(int argc ,char *argv[])
{
int i,pid;
int fd[2];//crea i descriptor
char phrase[30][30];//crea il buffer
pipe(fd); /* crea la pipe */

// Genera i 10 processi
for(i=0;i<argc-1;i++)
{
if((pid=fork())==0)
{
strcpy(phrase[i], argv[i+1]);
printf("ho scritoo :'%s'\n",*phrase);
close(fd[0]); /* chiude in lettura */
write(fd[1],phrase,strlen(*phrase)+1); /* invia anche 0x00 */
close (fd[1]); // chiude in scrittura
// pid=0 -> figlio
usleep(50000*(1+i)); // Ritardo iniziale
printf("Figlio: %d\n",i+1); // Stampa messaggio del figlio
usleep(500000*(1+i)); // Ritardo finale
return(101+i); // Termina con codice di ritorno
}
else {
printf("Ho generato il figlio %d con pid %d\n",i+1,pid);
char message[100]; //creare il buffer
memset(message,0,100);
int bytesread;
close(fd[1]); /* chiude in scrittura */
bytesread = read(fd[0],message,sizeof(message));
printf("ho letto dalla pipe %d bytes: '%s' \n",bytesread,message);
close(fd[0]);
}
}

// Attende che dieci processi terminino
for(i=0;i<argc-1;i++)
{
int status;
wait(&status); // Attende termine di un figlio (uno qualunque)
printf("Terminato processo %d\n",WEXITSTATUS(status));
}
}

输入:./pipes cmd1 cmd2 cmd3

 I created the first child with pid 21552
I wrote: 'cmd1'
I read from the pipe 5 bytes: 'cmd1'
I created the second child with pid 21553
I read from the pipe -1 bytes:''
I wrote: ') ML?'
I created the third child with pid 21554
I read from the pipe -1 bytes:''
I write: ') ML?'
Son: 1
Son: 2
Son: 3
Ended process 101
Ended process 102
Ended process 103

最佳答案

你有两个问题:

首先,您没有向管道写入正确的短语。你一直在写 phrase。对于第一个 child ,这是可以的,对于其他 child ,它将是空字符串。

其次,您将在创建第一个 child 后关闭 fd[0]。您永远不会收到来自其他进程的数据。

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

int main(int argc ,char *argv[])
{
int i,pid;
int fd[2];//crea i descriptor
char phrase[30][30];//crea il buffer
pipe(fd); /* crea la pipe */

for(i = 0; i < argc - 1 ; i++)
{
if((pid=fork())==0)
{
strcpy(phrase[i], argv[i+1]);
printf("ho scritoo :'%s'\n",phrase);
close(fd[0]); /* chiude in lettura */
write(fd[1],phrase[i],strlen(phrase[i])+1); /* invia anche 0x00 */
close (fd[1]); // chiude in scrittura
// pid=0 -> figlio
usleep(50000*(1+i)); // Ritardo iniziale
printf("Figlio: %d\n",i+1); // Stampa messaggio del figlio
usleep(500000*(1+i)); // Ritardo finale
return(101+i); // Termina con codice di ritorno
} else {
printf("Ho generato il figlio %d con pid %d\n",i+1,pid);
char message[100]; //creare il buffer
memset(message,0,100);
int bytesread;

bytesread = read(fd[0],message,sizeof(message));
printf("ho letto dalla pipe %d bytes: '%s' \n",bytesread,message);
// close(fd[0]);
}
}
close(fd[0]); /* chiude in scrittura */
close(fd[1]); /* chiude in scrittura */
// Attende che dieci processi terminino
for(i=0;i<argc-1;i++)
{
int status;
wait(&status); // Attende termine di un figlio (uno qualunque)
printf("Terminato processo %d\n",WEXITSTATUS(status));
}
return 0;
}

关于进程与管道之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com