Unix Programming - writing into FIFO using writev system call

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2006 > writing into FIFO using writev system call





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author writing into FIFO using writev system call
param

2006-01-20, 2:50 am

Hi,

I am using FIFO as IPC. Server creates a FIFO and opens it in read only
mode(RDONLY).
Client opens it in write only mode(WRONLY). In infinite loop, i am
sending data (using writev system call) from client, & server (reads
using readv system call)displays it. During execution, i am suspending
the server process (KILL -STOP <serverpid> ). After this, writev call
in client is still passing.
Questions:
1) After server is stopped, How is it still passing (writev call in
client side)?

As per my knowledge, writev should through some error message to
indicate that condition.

Please help me to understand that behavior.

Thanks in advance

Param

Program Used:
fifo_client.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

void sigpipe_handler(int signum)
{
write(STDOUT_FILENO,"sigpipe caught",strlen("sigpipe caught"));
}

int main(int argc,char *argv[])
{
int fd;
char *buf="Parameswaran";
struct iovec data;
data.iov_base=buf;
data.iov_len=12;

/* signal(SIGPIPE, sigpipe_handler); */
if((fd=open("./pfifo", O_WRONLY )) == -1)
{
perror("Error");
exit(1);
}

while(1)
{
if(writev(fd,&data,1) == 12)
{
printf("writev successful\n");
sleep(1);
}
else {
perror("Error");
exit(1);
}
}
exit(0);
}

----------------------

fifo_server.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
void exit_handler(void);
int fd;
char *buf=(char *)malloc(12);
int i;
struct iovec data;
data.iov_base=buf;
data.iov_len=12;

/* register exit handler */
if(atexit(exit_handler) != 0 )
{
perror("register");
exit(1);
}
if(mkfifo("./pfifo",0644)<0)
{
perror("Error");
exit(1);
}

if((fd=open("./pfifo", O_RDONLY)) == -1)
{
perror("open");
exit(1);
}
i=0;
while(i=3)
{
if(read(fd,buf,12) == data.iov_len)
{
printf("Request: %s\n",buf);
sleep(3);
}
else {
perror("read");
exit(1);
}
i++;
}
exit(0);
}

void exit_handler(void)
{
unlink("./pfifo");
}

Kurtis D. Rader

2006-01-21, 2:49 am

On Thu, 19 Jan 2006 22:51:48 -0800, param wrote:

> I am using FIFO as IPC. Server creates a FIFO and opens it in read only
> mode(RDONLY). Client opens it in write only mode(WRONLY). In infinite
> loop, i am sending data (using writev system call) from client, & server
> (reads using readv system call)displays it. During execution, i am
> suspending the server process (KILL -STOP <serverpid> ). After this,
> writev call in client is still passing.
>
> Questions:
> 1) After server is stopped, How is it still passing (writev call in
> client side)?


Pipes and fifos buffer data between the sender and receiver. That is, the
data is copied from the sender into a kernel buffer pending a read. The
size of the buffer varies but is normally at least 8 KiB in length.

> As per my knowledge, writev should through some error message to
> indicate that condition.


How did you get that idea? Even if the kernel did not buffer the data your
writev() call would simply sleep until a read() or readv() was issued. On
the other hand, if you put the file descriptor in non-blocking I/O mode
(see fcntl(2)) then the writev() will return -1 with errno == EAGAIN once
the buffer is full.

I suspect you meant this line in fifo_server.c

while(i=3)

to read

while (i < 3)

If you make that change your server will exit after executing three
read()'s. That will result in the fifo having no file descriptors open
for reading. The next writev() by your client will generate a SIGPIPE
that will kill your client process. If you uncomment the signal() call the
writev() will return -1 and errno == EPIPE.

param

2006-01-22, 6:11 pm

> Pipes and fifos buffer data between the sender and receiver. That is, the
> data is copied from the sender into a kernel buffer pending a read. The
> size of the buffer varies but is normally at least 8 KiB in length.


Yes. You are right. Initially i did not realise the buffer(& buffer
size). So I expected error (writev or write) while other process in
suspended state.


Thank You
Param

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com