A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: To fork() or not to fork()...That is the question

  1. #1
    Member
    Join Date
    Mar 2000
    Posts
    91
    I am developing a Flash chat application via sockets. However, I am wondering...What is better:

    A. one process that manages all socket connections OR
    B. a separate process for each socket connection

    Option A is slightly simpler, but doesn't seem like it will scale well with lots of users. With 1000 people connected one socket error could cause problems for everyone.

    Option B seems optimal in the reagrd that each connection has it's own process so one will not effect another. However, each process will require a certain amount of overhead and I don't want to max out server resources (thereby possibly effecting other processes).

    Which is best? And also on Linux is there anyway to measure the amount of RAM and resources a process is utilizing? The 'ps' command, maybe? I believe the 'ps' man page says that all processes use at least 12k.

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Posts
    475
    Hi

    IMHO i wouldn't fork anything - multithreading is better. Cause forking has a lot overhead (starting/stopping a new process, needs more memory,...) What programming language are you going to use? Java and C/C++ have quite good threading capabilities. With java it's very easy to do this - look at http://www.moock.org, there's a simple client - server socket app written in java using threads.
    Using C (better is C++) will maybe result in faster code and using less memory than a java app, but it's quite complicated to write apps crash safe (buffer overflow, ...). If you know C++ well I suggest to look for a well threading lib and get going. But before starting with C++ I'd figure out how many users are needed to bog down a java chat-server, because java being slower than a well written C++ program doesn't mean java is slow at all.
    a nifty tool that comes with most linux-distros is 'top'. it lists all processes with cpu usage, memory, user and more...
    Yours
    HTD

  3. #3
    Moderator
    Join Date
    Aug 2000
    Posts
    1,455
    I'd have to agree with HTD here. While forking is certianly an easy way to handle separate chat clients, it does make sharing resources more complicated than it needs to be.

    In my opinion, the best solution to the problem is to create a separate thread for each connection. Actually, it would be better to create a pool of an appropriate number of threads (depending on how busy you expect your server to be) upon starting your application so that you don't have the overheads of creating/destroying threads during the run of your application unless the thread pool is used up (and if this is happening frequently then you might want to look at creating a larger initial thread pool).

    Since you're creating the server on a Unix based system, I'd recommend getting hold of a copy of Unix Network Programming - Networking APIs: Sockets and XTI by W. Richard Stevens. This is pretty much the standard tome on Unix network programming, and although the examples are presented in C, the material covered is equally applicable to C++.

    I hope this helps!

    Regards,

    Steve

  4. #4
    Member
    Join Date
    Mar 2000
    Posts
    91

    Hmmm...

    I am trying to use C++. I've seen the Moock example, but I don't know Java.

    Thanx guys, I'll look into it all and get back to this thread if I have any questions...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center