ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, all
I have written the socket program. It listen on the TCP port, receive the commands from the clients and make the system operations: add, delete and chpass users.
I have made it "daemon" and use fork() in it to serve more than one clients simultaneously.
Every child process write the log message to the log file /some/where/in/the/file/system/log.
How should I write to the file if the several process will be write to that file simultaneously?
Make one process that will deal with the log file and pass the messages to it using IPC routines. If you start with reading 'man msgsnd' you should understand how to do it fast.
You can also use locks, but I don't recommend it. It's easy to make the whole thing stop because of lock (when a number of processes wants to write and one is keeping the file. When using messages you don't need to deal with this problem.
Locks lock files. In this case you may lock the log file. Message queues may be used to pass the data to be written to the log file to one process/thread from many others. Semaphores - if you have a semaphore that protects the log file (like lock, but at different level) and only teh thread that has it writes in, it will also work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.