Solved

Simple message relay using Fifo IPC's & file locking

Posted on 2003-10-22
6
1,265 Views
Last Modified: 2008-03-17
Hi im new to C programming.

Im trying to create a simple program which relays messages between two users on a linux machine using IPC's.
At the moment a sent message may arrive at the source or destonation program. How could i fix this problem?

I want a message from 1st program to arrive at the 2nd program and vice versa.  


I assume 1 way would be to lock/unlock the fifo file, so that only one user may send, while the other recives and vice versa.
How could i do this, and are the other solutions?

Here is some of the code

pid = fork()

for(;;)
{
      if(pid == 0) // child
      {            
            mkfifo(fifo, 0666); // create fifo if it already doesnt exist
      
            if((fd = open(fifo, O_RDWR)) <0) // open fifo for reading and writing
              printf("fifo failed");

            for(;;)  // recive and print msg
            {
            read(fd, msgbuf, MAXLEN+1)
            printf("\n\n messgae rcvd: %s", msgbuf);
            //sleep(1);
            }
      }

       while(waitpid(pid, &status, WNOHANG) == 0)

                    {      // create and send msg
            fd = open(fifo, O_WRONLY | O_NONBLOCK)
                        
            printf("\n\n please enter a message :");
            load_string(msgbuf, MAXLEN);

            nwrite = write(fd, msgbuf, MAXLEN+1)
            //sleep(3);
            
      }
}

Thanx = )
0
Comment
Question by:FOXBAT
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9599964

From the looks of your code, the "while(waitpid(...))" block of code is executed by both the parent and child, but the upper block is execute only by the child.

The child task opens the fifo and waits for a message.  The parent falls through to the while() block, opens the fifo, prints a message and writes a string to the fifo.

The child tries to continually reopen the fifo.  First in O_RDWR, then in O_WRONLY, then looping on the sequence.  The parent only opens the fifo O_WRONLY.



Kent
0
 
LVL 6

Accepted Solution

by:
Ajar earned 150 total points
ID: 9604572
kdo ,
      The code is ok in regards of  upper block getting invoked only by child process and lower by the parent .This is due to the infinite loop in the upper block.

Coming back to the original problem ,
     Using two fifos/pipes instead of one  for establishing the duplex link will  be easier to implement
rather than using locks and  some technique to synchronize the two  process read and writes .


0
 

Author Comment

by:FOXBAT
ID: 9604689
ok thanx i thought about the two pipes b4, Im gonna try implement that now.

But getting back to the lock issue,  i added this code to sending program part

char * lck ="lck";
int lk;
extern int errno;

if((lk = open(lck, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0)
{
      if(errno == EEXIST)
            { printf("lck exists"); exit(1); }
      else
            { printf("WTF"); exit(127); }
}
which works fine, creating the lock.

however i cannot remove it. i tried using close(lk); But it didnt work.

as when the program was run again 'lck exists" was displayed and lck file was still in directory

How can i remove the lock?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9604766
For implementing two way messaging,

U can also think of Message Queues



0
 

Author Comment

by:FOXBAT
ID: 9605714
Thanx i got it 2 work, how ever im wondering if i could ask you  some easy, genral questions about linux.

1) how can i copy to floppy, i went mnt/floppy, but was unable to read/write to it.
The floppy drive works fine under windows.

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9605846

Many linux implementations have a complete library of tools to read DOS formatted devices.  You can get a pretty good list with:

man -k DOS

In particular, look at mtools(1) and mcopy(1).


Kent
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rhel6 C system() call to zip to archive has problems 25 245
Problem to ASCII 1 194
How to learn Linux? 10 66
Please explain C language error codes ? 5 25
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question