3

I have two application - a php web application and c program. The php app has to send data to the c program which returns results. The c program has to run all the time in a cycle (while(true), because it has to hold data of previous input in memory.

I handle the problem passing data between c and php by using files (input & output files). If there is a change in my web application my php script update a file, that is read by my c program. The output is written into an other file. The web application is looking all the time for changes.

I think it is not the smartest solution to solve a problem like this. Probaly its a lot not necessary file access.

With inspiration and help from following thread i thought using a kind of c php server bridge could be a good solution.

How to pass value and run c program in php( web application)

Passing data between PHP and C executable in linux

I've started with mongoose. Mongoose is a very lightweight webserver. My current idea is that my web application would send data via POST directly to my c program. The request is handle by the mongoose webserver. Is this the way to go? Or would be a better solution to split it in two application 1) a webserver that handle input output - the variable between both program is passed by shared memory 2) my current c program for my operation (input / output via shared memory)

Or is this all the complete wrong way and a) i should stick to my file solution b) there is a even better way? I am sorry for all this question but i am pretty new to c and struggle a lot and not feeling really confident with all this.

Edit
I try to be more specific what my c program does. Basically its a simulation. It is like a stateflow that have around 20-40 inputs that are all integer and most of the time just 0 or 1. The program acts on the base of the inputs and of previous states. The inputs are reset after one iteration by changing the input file to origin state.

The output file is updated all the time with the corresponding output values (like var = 1 var2 = 3)

Community
  • 1
  • 1
jonas
  • 125
  • 2
  • 8
  • Do you have a concurrency problem? What if multiple PHP requests happen concurrently? What prevents the requests from interfering with each other as the C program runs? – Dwayne Towell Jul 15 '14 at 14:28
  • Not really, the application needs to handle only one request at once There will be only one user that input states. The output is for everyone the same. I thought about the problem, what if it is necessary to handle different sessions with different inputs. Basically i need for each simulation a new instance of my c program. As i dont have any idea to handle this i stick with my current solution. – jonas Jul 16 '14 at 08:55
  • 1
    Since I'm working with ZeroMQ lately and all that, among supplied suggestions I'll suggest using ZeroMQ with an event loop at C program side (or a polling mechanism). Decode your data with JSON in PHP, use ZeroMQ to PUSH the message to C program. C program acts as a server and reacts when there's data to read on the PULL ZeroMQ socket. It reads the message, decodes JSON, does work. Job done. It's a lot to absorb, but the actual code from both C and PHP side would be extremely minimal, but completely optimal. – N.B. Jul 16 '14 at 09:16
  • Thank you for this idea. I ve looked a little bit into ZeroMQ and it is looking great. I ve played around with it and basically it looks pretty easy and after a few minutes i was able to have an php example. At the moment i am struggling with creating a c program (maybe it is not working with cygwin (?)) But reason for this, should be my lag of knowledge of c But if i can get into this and solve my basic problem, i would go with it. this presentation http://vimeo.com/20605470 is a great first impression – jonas Jul 17 '14 at 06:50
  • I suggest you look into pipes. Both languages provide basic support and that way the data never has to actually hit the disk unless one process is "ahead" of the other. – Dwayne Towell Jul 17 '14 at 19:55

2 Answers2

1

You can use a DBMS for data sharing.

miltos
  • 1,009
  • 1
  • 6
  • 10
  • Thank you for the idea, the data that i have to share are only int (and most of the time just 0/1) and and maybe around 40 variables. I think a dbms would be to much, and i dont see the big difference to the file base solution (expect the managing aspect that i dont need) I ll try to edit my post to be more specific what the c program does. – jonas Jul 15 '14 at 13:47
0

I would develop this C program as SOAP WebService, this way it can be executed once and wait for requests. (or eventually work in CGI mode and be executed if needed)

Your PHP script (using SOAP library) could be able to call a single C function from your program and receive it's result. All data like function arguments, objects etc is exchanged between different languages using XML syntax.

I did it once to establish communication between PHP, C and Python, it worked fine. I used gSOAP. I's officially for C++, I know it's not proper way to do so, but you can mix them since they are compatible.

Marek
  • 1,413
  • 2
  • 20
  • 36
  • Thank you for your idea. I think it would be a good way. But i think its a little bit heavy. If i would use an other language e.g. java i would go for it. With c it looks time consuming and complicated than e.g. mongoose or especially ZeroMQ – jonas Jul 17 '14 at 06:57