For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2006 > FIFO synchronization problem









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 FIFO synchronization problem
erik.dassi@gmail.com

2006-05-12, 7:04 pm

Hi, I have a problem related to FIFOs under Linux. I'm developing a
simple application with a server that open his fifo(server_fifo) for
reading and then blocks waiting for writers. When I open a client the
server gets unblocked and reads the messages sent by clients... but the
problem is that the first fgets I do always returns "dirt" and so the
first reply by the server to the client is that the command is wrong.
Going on all the messages are one message after what they should be, so
as I send message 9 I get answer 8, when I send message 10, I get
answer 9 and so on.... someone has an idea on what it could be ? I
could just trash the first read but I wanted to understand what it is,
as I guess it's not right...

thanks to everyone, erik

THIS IS CLIENT CODE

int main(int argc, char *argv[])
{
/* stores informations about logged user and tells if a user is
logged.*/
char logUsername[50];
char logPassword[50];
int isLogged = 0;

/* program variables storing string commands and pointing to
FIFOs.*/
char menuChoice;
char command[512];
char *buffer;
FILE *fpRead;
FILE *fpWrite;

/* opens server fifo for sending commands to server.*/
if((fpWrite = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen");
exit(1);
}

/* get client PID.*/
char pid[20];
int intpid = getpid();
sprintf(pid,"%d", intpid);

//makes client fifo for server replies.
char clientFifoPath[50] = "/tmp/fifo_";
strcat(clientFifoPath, pid);
mknod(clientFifoPath, S_IFIFO|0666, 0);

/* main program loop for commands sending/reply.*/
while(1) {

sprintf(command,"%s;UREG;%s;%s;\n", pid, username, password);

SendMessage(fpWrite, command);
fpRead = fopen(clientFifoPath, "r");
buffer = GetMessage(fpRead);
if (strcmp(buffer,"UREGSUCC;")) {
printf("\nAll right\n");
}
else {
printf("\nServer replied: %s\n", buffer);
}
fclose(fpRead);
}
}
}

void SendMessage(FILE *fifo, char *message) {

if (fprintf(fifo, "%s", message) < 0) {
printf("Error while writing message!");
}
fflush(fifo);
}

char *GetMessage(FILE *fifo) {
char buffer[512];
return fgets(buffer, sizeof(buffer), fifo);
}

THIS IS SERVER CODE

int main(void) {
FILE *fp;
char *readbuf;

/* Create the FIFO if it does not exist */
umask(0);
mknod(FIFO_FILE, S_IFIFO|0666, 0);

while(1)
{
/* open reception FIFO. */
int fd = open(FIFO_FILE, O_RDONLY);
fp = fdopen(fd, "r");

readbuf = GetMessage(fp);
/*if a message was retrieved, process it.*/
if (readbuf != NULL) {
/*elaborate return string*/
SendMessage(CLIENT_FIFO, message);
}

fclose(fp);
}

return(0);
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com