gpt4 book ai didi

c - 子进程不读取 PIPE

转载 作者:行者123 更新时间:2023-11-30 15:41:18 25 4
gpt4 key购买 nike

#include    <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <sys/select.h>
#include <errno.h>

typedef struct rot* Rotacao;
typedef struct rot{
int idProcess;
int rotacao;
char url[50];
} roda;

struct stat statbuf;

void listen();
void * do_rotation();
void write_pixel();
void rotacao90();
void rotacao180();
void rotacao270();
void sig_handler();
struct pixel *get_pixel();
void trata();

char *src, buf[50];
int size, fileids90[2], fileids180[2], fileids270[2], idp;


int main(){
int id[N], i = 0;

int p, idPai = getpid();

for(p = 0; p<3; p++){
if(getpid() == idPai){
if(fork() == 0){
if(p == 0) rotacao90();
else if(p == 1) rotacao180();
else if(p == 2) rotacao270();
}
}
}

if(pipe(fileids90) != 0) printf("Não criou pipe 90!");
if(pipe(fileids180) != 0) printf("Não criou pipe 180!");
if(pipe(fileids270) != 0) printf("Não criou pipe 270!");

if(getpid() == idPai){
while(1){
listen();
}
}


void rotacao90(){
printf("1 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());
char str[20];
while(1){
read(fileids90[0], str, strlen(str)+1);
printf("%s", str);

}
}

void rotacao180(){
printf("2 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());
}

void rotacao270(){
printf("3 Processo chegou! ID = %d ; IDPai = %d\n", getpid(), getppid());
}

void listen(){
int pipe, rot, fdmax, n;
char str[20];


Rotacao new = (Rotacao) malloc(sizeof(roda));
Rotacao final = (Rotacao) malloc(sizeof(roda));

//OPEN PIPE WITH READ ONLY
if ((pipe = open ("FIFO_PIPE", O_RDONLY))<0){
perror("Could not open named pipe for reading.");
exit(-1);
}


while(1){
fd_set master;
FD_ZERO(&master);

FD_SET(pipe, &master);
if(pipe > fdmax) fdmax = pipe;
FD_SET(fileids90[0], &master);
if(fileids90[0] > fdmax) fdmax = fileids90[0];
FD_SET(fileids180[0], &master);
if(fileids180[0] > fdmax) fdmax = fileids180[0];
FD_SET(fileids270[0], &master);
if(fileids270[0] > fdmax) fdmax = fileids270[0];

n = select(fdmax+1, &master, NULL, NULL, NULL);
if (n == -1) {
perror("select");
}

if (FD_ISSET(pipe, &master)){


//READ FROM PIPE
if (read(pipe,new,sizeof(roda)) < 0 ){
perror("Error reading pipe.");
exit(-1);
}

final->idProcess = new->idProcess;
final->rotacao = new->rotacao;
strcpy(final->url, new->url);

if(final->rotacao == 90){
printf("\ndsadasdas1");
strcpy(str, "ola");
write(fileids90[1], str, strlen(str)+1);
}
else if(final->rotacao == 180){
printf("\ndsadasdas2");
}
else{
printf("\ndsadasdas3");
}

trata(final);



//CLOSING PIPE
if (close(pipe)<0){
perror("Error closing FIFO.");
exit(-1);
}

//OPEN PIPE WITH READ ONLY
if ((pipe = open ("FIFO_PIPE", O_RDONLY))<0){
perror("Could not open named pipe for reading.");
exit(-1);
}


}
if (FD_ISSET(fileids90[0], &master) == 1) printf("\nPipe90 com conteudo!");
if (FD_ISSET(fileids180[0], &master) == 1) printf("\nPipe180 com conteudo!");
if (FD_ISSET(fileids270[0], &master) == 1) printf("\nPipe270 com conteudo!");

}

//CLOSING PIPE
if (close(pipe)<0){
perror("Error closing FIFO.");
exit(-1);
}
}

大家好。我有一个我不明白的问题。我有一个客户端和一个服务器。这是服务器代码。我从客户端收到一个结构,我想将该结构发送到在函数 rotacao90() 中运行的进程子进程。与此同时,我正在做一些测试,并通过管道向父进程发送消息。选择功能未以正确的方式运行,我不明白为什么。以其他方式,我已经尝试从父进程向子进程发送消息,但他没有阅读它。我需要进行从服务器到 child 以及从 child 到服务器的通信。

有人可以解释我或帮助我吗?这是我大学的一个项目。

谢谢。

最佳答案

您可以在子进程生成后创建管道(使用pipe(fileids90)等),因此子进程不会拥有父进程所拥有的文件描述符。

关于c - 子进程不读取 PIPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554713/

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