Help With FIFO problem...


 
Thread Tools Search this Thread
Top Forums Programming Help With FIFO problem...
# 1  
Old 09-26-2010
Question Help With FIFO problem...

=server.c=
Code:
#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include <errno.h>

#include <fcntl.h>

#include <string.h>

#include <signal.h>
#define MSGSIZE 50


char *fifo1="fifo1";
char *fifo2="fifo2";



main()

{

    	char msgbuf[MSGSIZE];

    	pid_t pid;
	int fd1,fd2;



	system("clear");

	printf("************** SERVER SIDE ***************\n");

     	if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST))

	{

            	perror("Error creating the named pipe");

            	exit (1);

        }

      

        if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST)) 

	{

            	perror("Error creating the named pipe");

            	exit (1);

        }

	if((fd2=open(fifo2, O_RDONLY|O_NONBLOCK))<0)
		perror("fifo2 open failed");

	if((fd1=open(fifo1, O_WRONLY|O_NONBLOCK))<0)
		perror("fifo1 open failed");

 	pid = fork();
	

//loop:

    	if (pid < 0) 

	{

		//failure in creating a child

        	perror ("fork");

    	  	exit(2);

    	}



    	else if (pid == 0) 

	{
		if(read(fd2, msgbuf, MSGSIZE+1)<0)
			perror("Message read failed");
		printf("Received message: %s", msgbuf);

       	}



    	else

	{	if(fgets(msgbuf,MSGSIZE, stdin) != NULL) 

		{
			if(write(fd1, msgbuf, MSGSIZE+1)<0)
				perror("Message write failed");
		}
	}

    	

//	goto loop;

=client.c=
Code:
#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

#include <errno.h>

#include <fcntl.h>

#include <string.h>

#include <signal.h>
#define MSGSIZE 50

char *fifo1="fifo1";
char *fifo2="fifo2";

 

main()

{

       	char msgbuf[MSGSIZE];
	int fd1,fd2;

    	pid_t pid;



	system("clear");

	printf("************** CLIENT SIDE ***************\n");

        if ((mkfifo(fifo1, 0666) == -1) && (errno != EEXIST)) 

	{

            	perror("Error creating the named pipe");

            	exit (1);

        }



        if ((mkfifo(fifo2, 0666) == -1) && (errno != EEXIST)) 

	{

            perror("Error creating the named pipe");

            exit (1);

        }

	if((fd2=open(fifo1, O_WRONLY|O_NONBLOCK))<0)
		perror("fifo1 open failed");

	if((fd1=open(fifo2, O_RDONLY|O_NONBLOCK))<0)
		perror("fifo2 open failed");


	pid = fork(); 

//loop:

    	if (pid < 0) 

	{

        	//failure in creating a child

        	perror ("fork");

        	exit(2);

    	}



    	else if (pid == 0) 

	{

 		if(read(fd1, msgbuf, MSGSIZE+1)<0)
			perror("message read failed");
		printf("Received message: %s", msgbuf);

    	}

  

  	else

	{
		if(fgets(msgbuf,MSGSIZE, stdin) != NULL) 

		{
			if(write(fd2, msgbuf, MSGSIZE+1)<0)
				perror("Message write failed");
		}

        	

    	}

 //   goto loop;
}

The output is weird and i cant figure out why.
Can someone show me what is the problem?

Last edited by Scott; 09-26-2010 at 07:03 AM.. Reason: Please use code tags
# 2  
Old 09-26-2010
Admitted homework. Thread closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

FIFO

Hello , I am working on unix FIFO IPC. i have a doubt regarding that. If the fifo is updated(write()) through one process....can we able to send any signal that fifo is updated and ready to get read...to other process.?? (0 Replies)
Discussion started by: Harry443
0 Replies

2. Programming

FIFO problem[solved]

I wrote a simple program to have cat pipe its output into less through a FIFO. The write works fine, but the child won't exit after writing to the FIFO. Another program needs to read from the FIFO for it to exit, but the parent (less) must wait for the data to become available to read the FIFO. ... (0 Replies)
Discussion started by: Ultrix
0 Replies

3. Programming

fifo

Dear friends i'm want to implement a program which one file is split into fragments by the server (by some random size) and sent to some processes, so these processes get randomly the fragments of the original file from the server, then the downloader randomly connects to some of these processes... (0 Replies)
Discussion started by: saman_glorious
0 Replies

4. Programming

How to see a FIFO from all the threads?

Hello C programming fellows!!! I'm doing a program with multiple threads in gnu/linux, ubuntu for beeing precise... This program consist in multiple threads, as logical each thread do different things and communicate to each other using IPC. The problem is that in "process 1" I have a... (1 Reply)
Discussion started by: Sandia_man
1 Replies

5. Programming

FIFO's and asynchronousity?

Hello, I have a FIFO which i would like to open O_WDWR | O_NONBLOCK | O_ASYNC and have a SIGIO signal generated when there is input on the FIFO, but there a re a bunch of bugs surrounding this (like for instance i read somewhere that you have to set O_ASYNC with fcntl etc. is this even... (0 Replies)
Discussion started by: davo666
0 Replies

6. Filesystems, Disks and Memory

fifo deletion problem..

I have unix sco server. I have created one application for client server communication. On this I have creted some fifos/pipes. The reader.123 fifo is used by one process for reading and writing. I haven't deleted that fifo. But ls or find command doesn't show it. It is giving error as file or... (1 Reply)
Discussion started by: yogeshdimble
1 Replies

7. Programming

how to use fifo

hi, I have a problem. I've done a lil program which gets from the server the given persons username a personal folder. I made it with a pipe calling popen with a command, but how can i make the same thing using fifo. I make the fifo with mkfifo() func. and than what. How do I tell the sertver using... (3 Replies)
Discussion started by: atticus
3 Replies

8. UNIX for Advanced & Expert Users

FIFO Pipes

Hi...Can anyone please guide me on FIFO Pipes in UNIX.I have lerant things like creating fifo pipes,using them for reads and writes etc.I want to know what is the maximum amount of memory that such a pipe may have? Also can anyone guide me on where to get info on this topic from? (4 Replies)
Discussion started by: tej.buch
4 Replies

9. Programming

FIFO issue

hello i am making a client server program that communicates via FIFOS and i cannot get it to not hang i also am forking exec-ing the client from the server my logic (i will spare you the code ) in the server i make two fifos in my server my file permissions are 0644 i then open both... (1 Reply)
Discussion started by: runawayNinja
1 Replies

10. UNIX for Advanced & Expert Users

FIFO over NFS

Hi All L2000-44 HP-UX 11.00 I am attempting to setup a FIFO over NFS. Directing and reading from the FIFO works fine on the local machine - however when I direct to the FIFO from a server which has the directory NFS mounted it does not work. The a process to read from the FIFO on the local... (2 Replies)
Discussion started by: saabir
2 Replies
Login or Register to Ask a Question