Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing program to run on server, requesting experienced advice

Tags:

c

I'm developing a program that will need to run on Internet servers (a back-end component to be used by several cross-platform programs). I'm familiar with the security precautions to take (to prevent buffer overflows and SQL Injection attacks, for instance), but have never written a server program before, or any program that will be used on this scale.

The program needs to be able to serve hundreds or thousands of clients simultaneously. The protocols are designed for processing speed and to minimize the amount of data that must be exchanged, and the server side will be written in C. There will be both a Windows and a Linux version from the same code.

Questions:

  • How should the program handle communications -- multiple threads, a single thread handling all the sockets in turn, or spawn a new process for every so many incoming connections (or for each one)?
  • Do I need to worry about things like memory fragmentation, since this program will need to run for months at a time?
  • What other design issues, specific to this kind of programming, might an experienced developer of cross-platform programs for desktop and mobile systems not be aware of?

Please, no suggestions to use a different language. That decision has already been made, for reasons I'm not at liberty to go into.

like image 906
Head Geek Avatar asked Apr 15 '13 08:04

Head Geek


People also ask

What should I write in application server?

Describe examples of times you used your serving or customer service skills to improve the patron experience. You can include specific serving achievements and how your serving skills can benefit the restaurant.] [The third paragraph explains why you're the best server for the open position.

What is a server-side programming?

Server-side programming allows developers to make use of sessions — basically, a mechanism that allows a server to store information on the current user of a site and send different responses based on that information.

What is a server program?

A server is a computer program or device that provides a service to another computer program and its user, also known as the client. In a data center, the physical computer that a server program runs on is also frequently referred to as a server.


2 Answers

For I'd use libevent or libev and non-blocking I/O. This way the operating system will take case of most of your scheduling problems. I'd also use a thread pool for processing tasks, that by nature are blocking, so they don't block the main loop. And if you ever need to read or write large amounts of data to or from the disc, use mmap, again to let the OS handle as much as possible.

The basic advice is use the OS, as much as possible. If you want a good example of a program which does this look at Varnish, it is very well written, and performs fantastic.

like image 158
jbr Avatar answered Oct 26 '22 18:10

jbr


With my experience running multiple servers for over 3 years of uptime, and programs with little over a year of uptime I can still recommend making the setup so that the system gracefully recovers from a program error and from a server reboot.

Even though performance gets a hit when a program is restarted, you need to be able to handle that as external circumstances can force the program to such a restart.

Don't try to reinvent the wheel when not needed, and have a look at zeromq or something like that to handle distribution of incoming communications. (If you are allowed to, prototype the backends in a more forgiving language than C like Python, then reimplement in C but keeping the communications protocol)

like image 45
Anthon Avatar answered Oct 26 '22 19:10

Anthon