Jump to content

[Resolvido] Named pipes


jpedro20

Recommended Posts

Estava a treinar comunicação entre processos e surgiu-me umas dúvidas. Eu tenho o seguinte servidor:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define MAX_BUF_SIZE 1024

int main(void)
{
int fd = -1, fifo = -1;
int nbytes = 0;
char buf[MAX_BUF_SIZE];

fd = open("log.txt", O_WRONLY | O_CREAT, 0644);
if(fd == -1) return 1;

mkfifo("myfifo", 0644);

fifo = open("myfifo", O_RDONLY, 0644);
if(fifo == -1) return 1;

for(;; )
{
	nbytes = read(fifo, buf, MAX_BUF_SIZE);
	write(fd, buf, nbytes);
}

close(fd);
close(fifo);

unlink("myfifo");
return 0;
}

e o seguinte cliente:

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>


#define MAX_BUF_SIZE 1024


int main(void)
{
int fifo = -1;
char buf[MAX_BUF_SIZE];

fifo = open("myfifo", O_WRONLY, 0644);
if(fifo == -1) return 1;

while(fgets(buf, MAX_BUF_SIZE, stdin) != NULL)
{
	write(fifo, buf, strlen(buf));
}

close(fifo);
return 0;
}

Quando inicio o servidor ele fica parado na chamada ao sistema read à espera que um cliente escreva para o pipe. Quando deixo de enviar input através do cliente, o servidor entra em ciclo infinito indicando que leu 0 bytes em cada iteração (ate iniciar um novo cliente). Porque é que quando o cliente termina, o servidor não fica parado no read à espera de um novo cliente, tal como acontece quando iniciamos o servidor? Como posso garantir que o servidor não entra em ciclo infinito enquanto está à espera que clientes se "conectem" a este?

Obrigado

Edited by jpedro20
Link to comment
Share on other sites

Ja leste o manual respeitante ao funcionamento da funcao (POSIX) read, especificamente no que acontece depois dum EOF?

No data transfer shall occur past the current end-of-file. If the starting position is at or after the end-of-file, 0 shall be returned

if (nbytes == 0) /* client closed the connection */;

What have you tried?

Não respondo a dúvidas por PM

A minha bola de cristal está para compor; deve ficar pronta para a semana.

Torna os teus tópicos mais atractivos e legíveis usando a tag CODE para colorir o código!

Link to comment
Share on other sites

Então significa que se tiver dois clientes ligados a comunicar pelo mesmo pipe e fechar o canal de saída de um deles (através da função close()), o servidor vai encontrar EOF e devemos terminá-lo?

Então como posso codificar um encadeamento de pipes? ex: ls -la | cat | cut -c 40-50.

Edited by jpedro20
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.