How to write to a file synchronously after fork()? - c

I am going to implement a server in C as a course project. The server should serve more than one client simultaneously. Description of the project states that fork() should be used to serve more than one client. Each children should write something to a common file. How to handle this synchronously? Is there any mechanism like in Java where only one thread can use a function at the same time?

Related

Cross-platform way of communcation between processes in C webserver

I made a simple webserver in C now with config implementation. Now, I want to add feature to manage server by calling same process with command line arguments. Like Nginx for example: nginx -s reload will send signal to server and it will reload config from file. I want to achieve the same. But how? In Linux, I can send a signal to server master process by getting PID from pidfile. But how to make it in Windows? Or maybe there is another way?
You can listen on a fixed port number with loopback interface for IPC. There are many cross-platform libraries like libuv and nanomsg.
--
Another method is to use some abstraction provided by a library. For example, still libuv and nanomsg. (Both use domain socket on Unix and named pipes on Windows.)

Multiple instance writing on same mounted folder using camel-file component

We have requirement to write to single file using multiple instance of camel interface running simultaneously.
The file is on windows shared file system which has been mounted on JBoss server using SMB.
We are using camel file component to write file from each instance as a local file.
Below is the endpoint URI in camel context
file:/fuse/server/location/proc?fileName=abc.csv&fileExist=Append
The file generate has no issues when the write is happening from single instance, but in case of multiple instance it add junk characters to the file at random lines.
We are using JBoss Fuse 6.0.0 and the interface have been written using camel 2.10 version.
How can this be fixed? Is this the issue with SMB mount or the interface need to handle it.
I've had a look at the source code of the relevant camel component (https://github.com/apache/camel/tree/master/camel-core/src/main/java/org/apache/camel/component/file) & there isn't any built in support for concurrent access to a single file from multiple JVMs. Concurrent access from a single JVM is handled though.
I think you have two basic options to address your requirement:
Write some code to support shared access to a single file. The camel file component looks like it was built with extension in mind, or you could just create a standalone component to do this.
As #Namphibian suggests use some queuing system to serialise your writes (though I don't think seda will work, as it doesn't span JVMS.
My solution would be to use ActiveMQ. Each instance of your application would send messages to a single, shared queue. Some other processes would then consume the messages from MQ & write them to disk.
With a single process consuming all MQ messages there would be no concurrent writes to the filesystem.
A more robust solution would to run ActiveMQ in a cluster (possibly with a node in each of your application instances). Look at "JMSXGroupID" to prevent concurrent consumption of messages.

REST and C process integration

I have a need to provide restful api support to a linux deamon process that will maintain and manipulate a in-memory table (simple C structure of arrays). This deamon will act as a configuration entity and will relay the table contents to another process on its bootup or during configuration request.
Now in this context i would be happy to obtain the following information:
Would it be good to have an integrated web server or have an independent web server and talk to this daemon. Please note this server would not be required to handle huge loads.
Please suggest some good web servers with good REST support.
If an independent web server then what is the best mechanism for web server to deamon communication.
Please note this would be deployed on a small embedded board running debian.
Well, a possible solution is based on developing a CGI that is executed by any webserver (apache, lighttpd, ...) . This program connects to the main daemon with an IPC mechanism such as sockets, fifos or message queues and after interacting with the daemons returns the desired output to the REST client.
The CGI program can be written in any language, but if you want to write it in C, check this project: it's a CGI program written in C that takes commands for an IP camera. The connection with a main daemon is not implemented, since it was outside the scope of the project. I like it because it has an embedded XML parser and does not require any external library

Apache Camel: How do I signal to other processes that I moved/renamed a file?

I am trying to develop a file receipt process using Camel. what I am trying to do seems simple enough:
receive a file
invoke a web service which will look at that file and generate some metadata
move the file to a new location based on that metadata
invoke subsequent process(es) which will act on the file in it's new location
I have tried several different approaches but none seem to work exactly as I would like. My main issues are that since the file is not moved/renamed until the route is completed, I cannot signal to any downstream process that the file is available within that route.
I need to invoke webservices in order to determine the new name and location, once I do that the body is changed and I cannot use a file producer to move the file from within the route.
I would really appreciate hearing any other solutions.
You can signal the processing routes and then have them poll using the doneFile functionality of the file component.
Your first process will copy the files, signal the processing routes, and when it is done copying the file it will write a done file. Once the done file has been written the file consumers in your processing routes will pick up the file you want to process. This guarantees that the file is written before it is processed.
Check out the "Using done files" section of the file component.
http://camel.apache.org/file2.html
Using other components you could have used the OnCompletion DSL-syntax to trigger a post-route message for futher processing.
However, with the file component, this is not really doable, since the move/done thingy happends in parallell with that "OnCompletion" trigger, and you can't be sure that the file is really done.
You might have some luck with the Unit of Work API which can register post route execution logic (this is how the File component fires of the move when the route is done).
However, do you really need this logic?
I see that you might want to send a wakeup call to some file consumer, but do the file really have to be ready that very millisec? Can't you just start to poll for the file and grab it once ready, once you received the trigger message? That's ususually how you do things with file based protocols (or just ignore the trigger and poll once every now and then).

Spawning multiple C-executable processes with Apache/FastCGI

I've not been successful in finding help with this issue. What I want to do is following: I have some C-based executables that implement the server side logic. There should be one process running this executable per client. The process should be invoked upon the first HTTP request form the client and killed once a specific HTTP request comes in later on.
So here's the question. How can I start a new process from a FCGI program based on HTTP request internals, run my C-executable in that process and later kill the process from the same FCGI program? Effectively the serving C application processes then run in the background.
if you download the devkit from fastcgi.com it contains an example for threaded server that uses the libfcgi functions to pick apart the headers in the worker threads.
should give you a good starting point for managing your executable instance(s).

Resources