Hi All,

I have two programs, one writes to fifo (writefifo.c) and another reads from
fifo (readfifo.c) - sources at bottom of the mail).



The fifo has been created as myfifo

                vi...@myhost:~/prg> ls -l myfifo

                prw-r--r-- 1 vikas users 0 Feb 26 04:09 myfifo

Host is a 64 bit SLES (Suse) 10.



When I run writefifo on one terminal & readfifo on another, readfifo shows
me messages like:

vi...@myhost:~/prg> ./readfifo

Inside main

fd of fifo=3:

read 300 bytes --->message number #0<---

read 300 bytes ---><---

read 300 bytes --->message number #3<---

read 300 bytes ---><---

read 300 bytes --->message number #6<---

read 300 bytes ---><---

read 300 bytes --->message number #9<---

…

…

…

read 300 bytes --->message number #111<---

read 300 bytes ---><---

read 300 bytes --->message number #114<---

read 300 bytes ---><---

read 300 bytes --->message number #117<---

read 300 bytes ---><---

read 300 bytes --->message number #120<---

read 300 bytes ---><---

read 0 bytes ---><---

This happens all the time & happens on Solaris 10 too.

If I enable the *sleep(1)* in writefifo.c, I see the messages (yes, with a 1
second delay):

vi...@myhost:~/prg> ./readfifo

Inside main

fd of fifo=3:

read 200 bytes --->message number #0<---

read 200 bytes --->message number #1<---

read 200 bytes --->message number #2<---

read 200 bytes --->message number #3<---

read 200 bytes --->message number #4<---

read 200 bytes --->message number #5<---

read 200 bytes --->message number #6<---

read 200 bytes --->message number #7<---

read 200 bytes --->message number #8<---

read 200 bytes --->message number #9<---

…

…

…

read 200 bytes --->message number #118<---

read 200 bytes --->message number #119<---

read 200 bytes --->message number #120<---

read 200 bytes --->message number #121<---

read 200 bytes --->message number #122<---

read 0 bytes ---><---



I understand this is a problem with synchronization.

I can’t wait for 1 second as in the real program (a huge application) 1
second doesn’t work & I have to put 3 seconds which woirks but translates to
15 seconds (because the same function is called many times).

Please comment & suggest a better alternative.



*writefifo.c*



#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>



#define MYFIFO "myfifo"

int main()

{

    int fd=0,num=0,i=0;

    char buffer[250];

    printf("In writefifo \n");

    fd=open(MYFIFO,O_WRONLY);

    printf("Inside main, opened fd=%d\n",fd);

    memset(buffer,'\0',250);

while (num<123)

{

    //*sleep(1);*

    i=sprintf(buffer,"message number #%d",num);

    buffer[i+1]='\0';

    //printf("buffer (sent) --->%s<---\n",buffer);

    write(fd,buffer,200) ;

    num++;

 }

    return 0;

}





*readfifo.c*



#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>



#define FIFO_NAME "myfifo"

int main()

{



    char s[300];

    int num, fd;

    printf("Inside main \n");

    fd = open(FIFO_NAME, O_RDONLY);

    printf("fd of fifo=%d:\n",fd);

    do {

        num=read(fd,s,300);

        s[num] = '\0';

        printf("read %d bytes --->%s<---\n",num,s);

    } while (num > 0);

    return 0;

}



Thanks in anticipation.

Regards

Vikas
_______________________________________________
Ilugd mailing list
Ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd

Reply via email to