Advertisement

01.25.2008 at 09:51PM PST, ID: 23113051
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Child process (fork) sending message to other child processes
Tags: C, NA
Hello experts,

I am writing a program that uses sockets and forking.  What I'm having it do is receive a message on one of the child processes, and then that message should be distributed to the other child processes.  You can think of it like a chat server as that is the most similar concept.  

I'm at a point where I'm wondering "how should I implement" - specifically the piece where data comes into the read buffer for a child process, and how it is then transmitted from each of the child processes to their connected peers.

My pseudo code is this...

{
  setup socket parameters
  socket call
  bind call
  loop {
    accept call (blocking)
    fork
      child process
          listen for messages
          when i get a message, send to other child processes
      parent process
          close file descriptor
  }
}

There's the RIGHT way to do it (which seems like a lot of work, and I want to PROTOTYPE this first) which is to build a dispatch-type of service, which learns about FDs and other metainformation from the child processes when spawned.  This dispatch could act as a registrar, and add/remove child processes as they are born/die.

The dispatch could also become a messaging relay, i.e. a child process could say "send my peers this message", since it has this metadata.

As per the above, writing a dispatch is the right way but will take a painfully long time.  Anyone know of a simple way to do this using C?

Thanks!
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: jchristn123
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
Views: 121
Translate:
Loading Advertisement...
01.26.2008 at 02:34AM PST, ID: 20749111

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.26.2008 at 03:40AM PST, ID: 20749213

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.26.2008 at 02:34AM PST, ID: 20749111

Rank: Sage

I gather you're thinking of something similar to the IRC protocol ? If so, you'll find lots of interesting information starting from here :

        http://en.wikipedia.org/wiki/IRC

and more specifically, the RFC :

        http://www.irchelp.org/irchelp/rfc/rfc.html
        http://tools.ietf.org/rfc/rfc1459.txt


Now, keeping the IRC protocol in the back of our heads, the server (parent process if you want) would be the central component : it accepts new connections (new clients), and every client sends and receives messages from the server. Clients (childs) never communicate with each other directly - they send their message to the server (parent), which broadcasts it to all clients (childs).


>> Anyone know of a simple way to do this using C?

It's not very complicated. We already have all the information we need : the server knows all its children, and each child knows the parent. They can now use IPC (inter-process communication) to communicate. Several ways are possible, but the easiest is probably using pipes. Have one (bidirectional) pipe between each child and the parent (so there will be as many pipes as there are children).

Here's some example code :

        http://www.ee.ic.ac.uk/docs/Software/unix/programming/sys/transfer/related.html


Just a question : do you really want to fork off a process for every client ? That's potentially a lot of processes !! You could opt for threads instead (and use a shared message buffer for communication), or even a more monolithic design with just one server thread that does everything (it just keeps the socket descriptot for each client, and whenever a message arrives on one of the sockets, it is forwarded to all others).
Accepted Solution
 
01.26.2008 at 03:40AM PST, ID: 20749213

Rank: Guru

Picking up on what I8 has said, you seem to be making an awful amount of hard work for yourself here. I think it would be useful if you could actually state the objective of this exercise. Like I8, I am wondering why you need to be forking new processes. Unless you need each instance of a connection to be discrete so that it may, for example, out live it's parent process there is little point in doing this; you really are making life hard. Light weight threads would make you life so much simpler. Have you looked at pthreads or the Boost threads C++ library, which provides a x-platform abstract C++ interface for powerful threading concepts? With all the connections in the same process managing communication between them becomes so much simpler as they are all sharing the same process and therefore address space.

Both threads and processes are methods of parallelizing an application. However, processes are independent execution units that contain their own state information, use their own address spaces, and only interact with each other via interprocess communication mechanisms (generally managed by the operating system). Applications are typically divided into processes during the design phase, and a master process explicitly spawns sub-processes when it makes sense to logically separate significant application functionality. Processes, in other words, are an architectural construct. By contrast, a thread is a coding construct that doesn't affect the architecture of an application. A single process might contains multiple threads; all threads within a process share the same state and same memory space, and can communicate with each other directly, because they share the same variables.

-Rx.

 
 
01.26.2008 at 06:11PM PST, ID: 20752029
I got around the whole thing by using a couple of simple global variables.  Thanks for everyone's input!

BTW - I switched to a multi-threaded model as opposed to a forked model - you both were right, there wasn't enough to justify using fork.
 
 
01.26.2008 at 09:51PM PST, ID: 20752581
>> I got around the whole thing by using a couple of simple global variables
I suggest you Google "siq_atomic_t",  "Race Condition" and "c++ keyword volatile" (all without the quotes).
 
 
01.26.2008 at 09:52PM PST, ID: 20752589
You might also want to look at Boost threads
http://www.boost.org/doc/html/thread.html
 
 
01.26.2008 at 09:58PM PST, ID: 20752606
Hi evilrix - remember I said I was "prototyping" - very well aware of race conditions and issues/risks associated with using global variables.  Do appreciate the link on boost threads, will definitely check it out.  Thanks
 
 
01.26.2008 at 10:00PM PST, ID: 20752610
>> remember I said I was "prototyping"
Even prototypes should work :)

>> very well aware of race conditions and issues/risks associated
What about the other suggested searches (the reason I suggested them)?

Good luck.
 
 
01.26.2008 at 10:16PM PST, ID: 20752664
Prototype works great.  Sounds like you have something against global variables, which I think work very well when controlled properly :)  Don't worry, the prototype is monolithic.
 
 
01.26.2008 at 10:21PM PST, ID: 20752683
>> Prototype works great.
Debug build? If so, try it in release build!

http://www.devx.com/tips/Tip/15424
http://www.devx.com/tips/Tip/13750
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628