writing into FIFO using writev system call
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > writing into FIFO using writev system call




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    writing into FIFO using writev system call  
param


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-06 07: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");
}






[ Post a follow-up to this message ]



    Re: writing into FIFO using writev system call  
Kurtis D. Rader


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-21-06 07: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.






[ Post a follow-up to this message ]



    Re: writing into FIFO using writev system call  
param


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-22-06 11: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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:14 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register