C 子进程不读取管道
大家好。我有一个我不明白的问题。我有一个客户端和一个服务器。这是服务器代码。我从客户端接收到一个结构,并希望将该结构发送到在函数rotacao90中操作的流程子级。同时,我正在做一些测试,并通过管道向父进程发送消息。选择功能运行不正常,我不明白原因。另一方面,我已经尝试从父进程向子进程发送消息,但他没有读。我需要做一个从服务器到子服务器以及从子服务器到服务器的通信 有人能给我解释一下或帮助我吗?这是我大学的一个项目C 子进程不读取管道,c,process,printf,pipe,fifo,C,Process,Printf,Pipe,Fifo,大家好。我有一个我不明白的问题。我有一个客户端和一个服务器。这是服务器代码。我从客户端接收到一个结构,并希望将该结构发送到在函数rotacao90中操作的流程子级。同时,我正在做一些测试,并通过管道向父进程发送消息。选择功能运行不正常,我不明白原因。另一方面,我已经尝试从父进程向子进程发送消息,但他没有读。我需要做一个从服务器到子服务器以及从子服务器到服务器的通信 有人能给我解释一下或帮助我吗?这是我大学的一个项目 谢谢。您在子进程已经使用pipefileids90等生成之后创建管道,这样子进程
谢谢。您在子进程已经使用pipefileids90等生成之后创建管道,这样子进程就不会像父进程那样具有文件描述符。尝试简化代码,看看是否可以获得更简单的版本来工作,以确保您了解自己在做什么-例如,只需要两个进程与简单消息通信,而不是4或5。您的主要错误是需要在fork之前创建管道与子进程通信。父级/子级不共享内存,但子级继承文件描述符。退出您的孩子,这样他们就不会执行父代码或像90一样永远运行。处理完成后,让父级等待子级。否,fork将只创建3个子级。如果有点非正统的话,那么多就可以了。
#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);
}
}