2012-07-23 19:31



I have a C simulation for which I'm creating a graphical web interface. I used HTML with ajax to call PHP, which in turn calls the shell to run the simulation. The output is then passed back to the webpage.

The script generates an output file (with a link outputted to the webpage) as well as a graph (using a call to gnu plot and referencing the output file). The webpage also allows the user to upload an input file which is used in the C simulation. All of these files are user specific.

If there is only one user on the webpage at a time, there isn't any problem. However, if 5 people want to run the simulation at the same time, the user-specific files (output, graph, input) would all be getting overwritten and messed up.

I don't have any MySQL experience, and I also don't plan on saving the outputs or inputs for a given user (user can download them if he/she wishes), so I was hoping there might be a rather simple solution like, for example, generating a temporary directory on the server with some sort of identifying tag in the name based on the user's IP address or something. This folder could then be removed some number of minutes after the simulation runs. Anyone have any ideas about something like that?

Or will I have to resort to MySQL?

Edit: It would also be okay to have something like allowing a maximum of 5 users running at the same time and then create a queue for additional users, but that might be even more difficult.

Thanks, Josh

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongshuo1856 dongshuo1856 9年前

    You can't do it without saving the output somewhere, so your temp dir plan is fine.

    • Assign each new user a unique id, via md5() or sha1() or whatever.
    • Pass the id to the simulator and have it create its output files in a subdirectory named by that id.
    • Render the links and image HREFs according to that id..
    • Write a cron to periodically purge the stale subdirs.

    Each person will get their own output set, on an unguessable URL.

    点赞 评论 复制链接分享
  • dtkl55257 dtkl55257 9年前

    You can create a session identifier for a visiting user and pass that id as a parameter to your c program.

    When your c program creates files for user to display it should generate name using your session identifier for that user.

    From web page let users to view files that match their session identifier. This way they can see files they generated and also you can put a control for users if they have a file with their session identifier you don't need to do an unnecessary call to your c program.

    Just serve the one which was already created. Control life time of that identifier as you wish.

    点赞 评论 复制链接分享
  • duanqiang9212 duanqiang9212 9年前

    Perhaps you could use Threading in your C simulation. Alternatively, you could set up a MySQL database and store the data there using PHP Database Objects (PDO). If you can use C and are able to do ajax with PHP, mysql will probably the most time efficient solution to implement as opposed to threading in C (assuming the C simulation is relatively complex)

    点赞 评论 复制链接分享