Thread: Problem with named FIFO

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    Problem with named FIFO

    Hi there,

    i have to write simple tcp messanger on linux. It has to have some basic functionality - when connecting via telnet, it must react to basic commands as put, get and exit and so on and when connecting by my own client every message must be delivered to everyone talking without any commands (real time).

    So what do i need to write it? The server must surely have some shared memory for it's subproccesses to communicate - but what would be the best - PIPE or unnamed PIPE? I know that all these processess are "family" so unnamed PIPE would be normally best. But how can i reach for previous messages (telnet issue) when using unnamed PIPE? Is it possible at all?

    One more thing: named PIPE stops everything when opened. So this subprocesses must be divided to 2 more - one for reading and one for writing?


    Thanks guys!
    Greg

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kbzium View Post
    The server must surely have some shared memory for it's subproccesses to communicate - but what would be the best - PIPE or unnamed PIPE?
    It must use some form of IPC -- eg, shared memory OR pipes. What are the subprocesses for? Are you going to fork every client?

    That may not be necessary (unless it is specifically part of the assignment); you could also use non-blocking sockets and queue/buffer data. Even with dozens and dozens of connections, the effect can be made seamless; tcp/ip is not really a steady stream anyway (plus think where forking dozens of clients is going to take you...).

    But, since you have to use sockets for the tcp connections, if you do use multiple processes, why not use tcp/ip sockets with them too? They are somewhat less hassle and more flexible than pipes. Linux has "unix local" sockets for stuff like this, they are probably the most common form of IPC.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Thanks for your reply.

    Yes, i use subprocesses to handle each client individually. That's the way they did it in classes so i did it to and i'd rather do it that way even if that may be not the best way to do it. But there is something about that this program should be able to handle any number of clients simultaneously.

    I've typed some code but i'm afraid that it would be hard for you to get through it especially it has a lot of polish comments and names of variables are in polish too... not so smart i guess :-). The thing is that as for communication stuff it works (the server accepts clients and talk with them), but i can't manage to make the server talk to everyone at once (if a client posts new message).

    I'd rather not change my entire code... Shared memory is something that is also called unnamed pipe? Which one should I use then? Named or unnamed? How to reach to its previous entries (go back more than one "read")?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kbzium View Post
    Yes, i use subprocesses to handle each client individually. That's the way they did it in classes so i did it to and i'd rather do it that way even if that may be not the best way to do it.
    It's not unusual; most mainstream http servers use a variation on this theme. But there are hard limits on how many connections they will really maintain simultaneously, and limits on how long they will maintain them (generally, <20 seconds).

    Presuming your connections are more persistent, you should check how many you have to maintain; while in theory forking does not have a limit, in practice it uses a lot of memory, is the issue. But I imagine for this it is not such a serious one.

    The thing is that as for communication stuff it works (the server accepts clients and talk with them), but i can't manage to make the server talk to everyone at once (if a client posts new message).
    If each client is in it's own fork, you have to pass the message from that process to the parent-server process, and then the parent passes it on to all it's children. To do that you need to use some form of IPC (Inter Process Communication):

    Inter-process communication - Wikipedia, the free encyclopedia

    Shared memory is something that is also called unnamed pipe?
    Pipes and shared mem are two different means of accomplishing IPC.

    I wouldn't use shared mem unless you've used it before because it will require locking, which is a lot of new stuff to cram into one assignment. Like I said, I'd just use sockets for the whole thing, since you have to use sockets for the client connections anyway. So each fork would have two sockets to handle, one to the client and one to the server. Sounds redundant at first, but if the fork mostly handles the client on it's own, this frees up the parent-server, and in any case the parent<->child connections will be local, so the child forks will be a kind of buffer between the parent-server and the clients. If one of the client connections is slow, it won't affect the server-parent as much as it will affect the child fork.

    I'd rather not change my entire code...
    I don't know if you have to, but if you do, sooner is better than later.

    Posting your code always helps. There is no guarantee anyone will read it all, of course, but as long as it is indented, don't worry about polish.
    Last edited by MK27; 12-28-2011 at 09:20 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    I'd like to post it here but i'm worrying that someone would get it too and i would be accussed of stealing not my work (which would be very unpleasant). I've tried to send it to you on private, but this forum doesnt seem to let me do that. Or... I'll post it here and i've delete it after you read it, ok?

    http://dl.dropbox.com/u/54401741/czatserver.c

    Locking means semaphores? I think they'd like me to use them...

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A fifo file and select() problem
    By itsdafoetime in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 02:23 AM
  2. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  3. How to flush a named pipe (FIFO) after reading from it?
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 01-10-2007, 06:05 PM
  4. named pipe problem
    By fnoyan in forum Linux Programming
    Replies: 0
    Last Post: 05-28-2006, 05:54 AM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM