473,761 Members | 5,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP script communicating with C/C++ program

Hi, I wasn't sure which forum this post belongs to, so I've posted it
to a couple forums that I thought may be appropriate.

In giving me advice, please consider me a beginner. Below is a synopsis
of my problem/question:

SOME BACKGROUND:
- I am writing a php based web application.
- There is a very data intensive task I need to do that requires
reading and lookup of a lot of data.
- This data is all stored in a database. If I did the computation
directly from php, then the load on the database is too intensive, and
takes a unacceptable amount of time (over 20 secs) to calculate (I've
already tried this).
- So I thought what I need to do is load all this data into memory,
and then do this operation from there, and this should be a lot faster
than doing lookups on a database.

WHAT I WANT TO DO:
- Write a C/C++ program that is constantly running that is dedicated
to doing this data intensive operation.
- This program would read all the data from the dbase into memory upon
load. Then it would take requests from a PHP script to run the
operation on that data and return the results to the PHP script.

THINGS I'VE LOOKED INTO:
- I know you can write extensions to php in C++
(http://bugs.tutorbuddy.com/php5cpp/php5cpp/) but I don't know if this
is what I want to do because (as far as I understand) an extension is
loaded and unloaded with the script. The C++ program I need has to be
constantly running in the background, taking requests and returning
results.

If anyone has any advice of how I can go about writing this c/c++
program that communicates with a php script or doing something similar,
I would appreciate any help. (even if you think I am using the wrong
technology to accomplish this, I'd like to know) Thank you!

PS- my current apache/php/mysql setup is in Windows, however when this
project goes live, I expect it to be running in unix.

Aug 17 '05 #1
9 2469
NC
Nimit wrote:

In giving me advice, please consider me a beginner. Below is a synopsis
of my problem/question:

SOME BACKGROUND:
- I am writing a php based web application.
- There is a very data intensive task I need to do that requires
reading and lookup of a lot of data.
- This data is all stored in a database. If I did the computation
directly from php, then the load on the database is too intensive, and
takes a unacceptable amount of time (over 20 secs) to calculate (I've
already tried this).
- So I thought what I need to do is load all this data into memory,
and then do this operation from there, and this should be a lot faster
than doing lookups on a database.

WHAT I WANT TO DO:
- Write a C/C++ program that is constantly running that is dedicated
to doing this data intensive operation.
- This program would read all the data from the dbase into memory upon
load. Then it would take requests from a PHP script to run the
operation on that data and return the results to the PHP script.


This program has been written a long time ago; it's called MySQL.
MySQL supports HEAP tables, which, unlike any other tables, are
stored in memory, not on disk. See MySQL Manual for details:

http://dev.mysql.com/doc/mysql/en/me...ge-engine.html

Also, there is a good chance that you task can be sped up without
using HEAP tables. Reduce the number of queries (i.e., use one
complicated query instead of a series of simple queries), think
if your indexing is helping as much as it can, and perhaps
consider if a change in data architecture can contribute to
better performance...

Cheers,
NC

Aug 17 '05 #2
Nimit wrote:
WHAT I WANT TO DO:
- Write a C/C++ program that is constantly running that is dedicated
to doing this data intensive operation.
- This program would read all the data from the dbase into memory upon
load. Then it would take requests from a PHP script to run the
operation on that data and return the results to the PHP script.


Here are two ideas that should work on Windows, but not on unix.
However, perhaps it will give you some ideas. Before you try these,
though, I would really double check, maybe even post a question about,
whether you are doing the most complicated computations in the world
that just can't be speeded up. Last guy who asked a question like this
turned out to have mixed up references...

On to the meat: What you are asking for is to write a server (this
isn't the only view possible. You could use polling, for example, or
your app could prepare a file based cache of relevant data and your PHP
could call a second app or pick it up directly) - you want something
that will respond to a client (PHP's) request.

COM technology is useful here. The most straighforward thing to do is
to create a COM object which will act as a server. Probably you can
already do this with your C/C++ knowledge. For those who only know VB,
you can use the wonderful, free, and hard to locate Microsoft provided
VB5CCE to create an .OCX that can be used. The OCX or DLL you create
will act as your server - once you've learned how to do it, it's very
easy. Then (since this COM object will be alive in the background),
you will need to get ahold of it with
http://php.net/com_get_active_object. Mind the warning on that page
since you're wanting to use this in exactly the fashion that the warn
you about. But this is my recommended option - I think it has less
learning overhead than option 2 and should be more efficient since it
will have less code.

There is a second path using COM. Create a CLI (command line) php
program that will sit in the background. This php program will bring
up a hidden version of IE (during development you'd better use a
visible version or you'll go nuts) and stay running by means of
com_message_pum p. This instance of IE is globally locatable (because
you will give it a unique title). Your server PHP script will use
$oShell=new COM("WScript.Sh ell") to loop through all the IE windows and
check for a matching title. At that point you could use this IE to
communicate back and forth with your CLI php. You can add your own
custom variables/values from PHP onto IE. But more importantly, you
can use IE to reach into PHP classes and execute PHP code there.
Although I haven't tried it across PHP scripts, I imagine you could use
your server script to execute an IE function which will call into your
CLI php script. Pretty exciting stuff, but such hookup is beyond the
scope of this post and of course it carries the same warning as the
prior method.

I'll be curious to know your final solution,
Csaba Gabor from Vienna

Aug 17 '05 #3
Some tasks are simply not suited for development in the form of web
applications. However, with all due respect it is much more likely that you
have not designed your application properly. For example, data lookup is not
a procedure that is limited by the processing speed of PHP; it's the
responsibility of the database (that's what SQL is for).

You may want to read "Advanced PHP Programming" by George Schlossnagle and
"High Performance MySQL" by Jeremy D. Zawodny & Derek J. Balling, both
available at B&N & online at Amazon.com. They deal with optimization
techniques, programming methods and database architecture/query design for
enterprise-level applications. If this is deep water for you, it may be time
to sub-contract. A large web development project is almost guaranteed to
require at least one expert who has experience developing systems on the
same scale.

PHP and MySQL are quite capable of providing a platform for scalable
on-demand high-volume applications, with the right hosting infrastructure,
optimizers and (crucially) forward-thinking application design.

Good luck,

ECRIA
http://www.ecria.com
Aug 17 '05 #4
Nimit wrote:
THINGS I'VE LOOKED INTO:
- I know you can write extensions to php in C++
(http://bugs.tutorbuddy.com/php5cpp/php5cpp/) but I don't know if this
is what I want to do because (as far as I understand) an extension is
loaded and unloaded with the script. The C++ program I need has to be
constantly running in the background, taking requests and returning
results.
That's not true. An extension is loaded once (when the server starts)
and stays in memory. It's not the place where you'd put your processing
code though, as Apache would spawn multiple copies of itself on Unix.
If anyone has any advice of how I can go about writing this c/c++
program that communicates with a php script or doing something similar,
I would appreciate any help. (even if you think I am using the wrong
technology to accomplish this, I'd like to know) Thank you!


Write a daemon that listens on a socket for requests and have PHP
communicate with it.

Aug 17 '05 #5
Hi NC. You've helped me with this problem a few months back if you
remember. The post is at: http://tinyurl.com/8d2ro. The query I am
using is pretty much the one you suggested, where the knows table joins
on itself once for each degree of separation we want to calculate.

To fill everyone else in, what I have is a social network (like
friendster) stored in a database. The database has a "knows" table that
has a row for each relation between two people. If you go to the post
I've mentioned above, you will probably get a better understanding of
what exactly my problem is. I didn't mention it in my original post
because I didn't want to make it too long or go off topic, but I guess
it was necessary.

I took your suggestion to try out Heap tables and it slashed my time a
LOT. I can do the 3rd degree search in about 3.75 seconds now, verus
about 17-18 seconds using the normal tables. That is awesome! I think
that is the answer I was looking for.

However, for that 3.75 seconds, my system totally freezes...like,
winamp stops playing, and everything stops responding. This scares me a
bit, because how many of these queries could this system handle at the
same time? Also, my laptop is has pretty good specs, pentium M 1.7ghz,
with 1GB of RAM, so I don't think it's the hardware. Any thoughts on
this?

Thanks again to all of you for your help and responses.

Aug 17 '05 #6
Nimit wrote:
Hi NC. You've helped me with this problem a few months back if you
remember. The post is at: http://tinyurl.com/8d2ro. The query I am
using is pretty much the one you suggested, where the knows table joins
on itself once for each degree of separation we want to calculate.

To fill everyone else in, what I have is a social network (like
friendster) stored in a database. The database has a "knows" table that
has a row for each relation between two people. If you go to the post
I've mentioned above, you will probably get a better understanding of
what exactly my problem is. I didn't mention it in my original post
because I didn't want to make it too long or go off topic, but I guess
it was necessary.

I took your suggestion to try out Heap tables and it slashed my time a
LOT. I can do the 3rd degree search in about 3.75 seconds now, verus
about 17-18 seconds using the normal tables. That is awesome! I think
that is the answer I was looking for.

However, for that 3.75 seconds, my system totally freezes...like,
winamp stops playing, and everything stops responding. This scares me a
bit, because how many of these queries could this system handle at the
same time? Also, my laptop is has pretty good specs, pentium M 1.7ghz,
with 1GB of RAM, so I don't think it's the hardware. Any thoughts on
this?


As suspected, the real problem lay elsewhere.
Here is an approach that should sinificantly reduce your load.
People are not going to be updating their contacts that frequently and
the amount of updating is (in the grand scheme of things) relatively
minor. Ie. once established, the table does not change quickly.

Therefore, have a separate table where for each pair of people you make
a one time computation of their degree of separation (or whatever is
important) and put it in as: Person1, Person2, separationInfo (degree,
otherDetails) indexed on Person1 and Person2, of course. otherDetails
could be a string: a minimal chain to get to the person or whatever
might be interesting to you. At 10^8 pairs, that is a lot of entries
so you can be happy that you live in an era where 100 Gig hardrives can
be had.

But the important point is that updating can be done in the background
with an appropriate message pump so other apps can get their slices of
time (By the way, I'm sure you've come across Dijkstra's algorithm for
these types of computations). At the same time, extraction of the info
you want is now fast because mySQL just has to return a contiguous
block of 10K entries (if you wanted them all), that the info for each
person is grouped in one block. The central idea here is that you've
cached all your possible responses.

But beware that the memory requirements for this solution grow
quadratically with the number of people.

Csaba Gabor from Vienna

Aug 17 '05 #7
On Wed, 17 Aug 2005 08:12:09 -0700, Nimit wrote:
Hi, I wasn't sure which forum this post belongs to, so I've posted it
to a couple forums that I thought may be appropriate.

In giving me advice, please consider me a beginner. Below is a synopsis
of my problem/question:

SOME BACKGROUND:
- I am writing a php based web application.
- There is a very data intensive task I need to do that requires
reading and lookup of a lot of data.
- This data is all stored in a database. If I did the computation
directly from php, then the load on the database is too intensive, and
takes a unacceptable amount of time (over 20 secs) to calculate (I've
already tried this).
- So I thought what I need to do is load all this data into memory,
and then do this operation from there, and this should be a lot faster
than doing lookups on a database.

I think you need to look into how a proper rdbms manages its data. You'll
find that regularly used data *will* automatically be stored in memory[1].

Try transferring a data set into postgresql ( it's available for windows
if you must ), and see whether the performance becomes acceptable.

Failing that, use Oracle (:

Steve
[1] Assuming you've got enough spare!
Aug 18 '05 #8
Everyone here is telling you how to optimize your queries.
look into indexing, caching, stored procedures (mysql5 ?).. optimizing
queries and code.

But your question was how to let your script communicate with your c++ code.
The way I understand your question, your options would be:

execute scripts in php like this:
exec() shell() or ` ` quotes.

Alternatively, you can communicate with your C++ code via sockets.

But as suggested by others, I don't think programming C++ will help, because
the bottle neck is not your PHP code, rather running the queries against the
database.
"Nimit" <ni********@gma il.com> wrote in message
news:11******** ************@g1 4g2000cwa.googl egroups.com...
Hi, I wasn't sure which forum this post belongs to, so I've posted it
to a couple forums that I thought may be appropriate.

In giving me advice, please consider me a beginner. Below is a synopsis
of my problem/question:

SOME BACKGROUND:
- I am writing a php based web application.
- There is a very data intensive task I need to do that requires
reading and lookup of a lot of data.
- This data is all stored in a database. If I did the computation
directly from php, then the load on the database is too intensive, and
takes a unacceptable amount of time (over 20 secs) to calculate (I've
already tried this).
- So I thought what I need to do is load all this data into memory,
and then do this operation from there, and this should be a lot faster
than doing lookups on a database.

WHAT I WANT TO DO:
- Write a C/C++ program that is constantly running that is dedicated
to doing this data intensive operation.
- This program would read all the data from the dbase into memory upon
load. Then it would take requests from a PHP script to run the
operation on that data and return the results to the PHP script.

THINGS I'VE LOOKED INTO:
- I know you can write extensions to php in C++
(http://bugs.tutorbuddy.com/php5cpp/php5cpp/) but I don't know if this
is what I want to do because (as far as I understand) an extension is
loaded and unloaded with the script. The C++ program I need has to be
constantly running in the background, taking requests and returning
results.

If anyone has any advice of how I can go about writing this c/c++
program that communicates with a php script or doing something similar,
I would appreciate any help. (even if you think I am using the wrong
technology to accomplish this, I'd like to know) Thank you!

PS- my current apache/php/mysql setup is in Windows, however when this
project goes live, I expect it to be running in unix.

Aug 20 '05 #9
Lisa Pearlson wrote:
Everyone here is telling you how to optimize your queries.
look into indexing, caching, stored procedures (mysql5 ?).. optimizing
queries and code.

But your question was how to let your script communicate with your c++ code.
The way I understand your question, your options would be:

execute scripts in php like this:
exec() shell() or ` ` quotes.

Alternatively, you can communicate with your C++ code via sockets.

But as suggested by others, I don't think programming C++ will help, because
the bottle neck is not your PHP code, rather running the queries against the
database.


Hi Lisa, you're right, the bottleneck is not PHP. What I was planning
to do with C++ however was to have a dedicated program on a server that
would first load the entire person network into memory, and then do the
calculations on that network in memory, which would be a lot faster
than making thousands of sql queries for every calculation.

I kind of solved this problem by NC's suggestion of using Heap tables,
as those are stored in memory. The performance in these tables wasn't
optimal either, but it was workable. I will be looking into caching
schemes of some sort (such as perhaps caching all 2nd degree friends)
or something to speed it up more. I will post about those when I come
up with something. Thanks for your response :)

Aug 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3719
by: FPGA05 | last post by:
Hello All, I am developing a small application in which I would need a C++ application to read the output from a shell script. A shell script keeps looking for user inputs and once the user gives his inputs it runs a series of commands and writes an output to a file. Once the output is producd it needs to communicate to the C++ aplication which would then need to read from this file. This whole process repeats continuously. Is there a...
6
1743
by: Don | last post by:
I'm trying to come up with a way within a client-side web page of uploading a couple files to a server-side PHP program without using a <form...>. I don't want to give up the page which happens after the <form...> executes. Instead, I want to upload the files, then execute a <form...> for another purpose. Any help would be appreciated. Thanks, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----...
1
2476
by: Harsh | last post by:
i want to communicate crystal report with java script. I have a heirarchy tree displayed in java script and based on the node selected on that tree i want to generate and display report in crystal report.how can i communicate among them so that i know which node is selected and can display appropriate report. Thanks Himanshu.
10
2049
by: Nimit | last post by:
Hi, I wasn't sure which forum this post belongs to, so I've posted it to a couple forums that I thought may be appropriate. In giving me advice, please consider me a beginner. Below is a synopsis of my problem/question: SOME BACKGROUND: - I am writing a php based web application. - There is a very data intensive task I need to do that requires reading and lookup of a lot of data.
3
23877
by: Stan | last post by:
Hallo, I have developed an application in MS Access 2000 (Polish version) under MS Windows XP prof (also Polish). Now I would like to run this code on MS Windows XP EN and MS Access XP EN. I have converted the mdb to version 2002 under MS Access XP Polish and under this version everything works OK. The problem starts when I copy the mdb file to Windows XP EN and start it with MS Access XP EN. I get following error: "The expression On...
1
345
by: Serdar C. | last post by:
hello again, i have another question... i am writing a program with 2 windows forms... first form have a search button and several text buttons to list the data loaded from an sql database second form just have a listbox that shows the search results in the listbox.. i want to let user select from the second form's listbox and show the results from the first form. how can i "post" te selected items name to the
5
1477
by: caroalv | last post by:
Hiya, I hope I'm using the correct group here. I'm a newbie to C. I need a Java program and external program to send and receive messages to and from each other, but the only way I can do it is through a C program (and it has to be C due to the external hardware). As a result, I am writing a C program that will create a client socket to the Java program and a server socket to an external program. So, I need the program to: Set up a...
2
1834
by: an96su | last post by:
Hi, I'm new to php. i'm trying to send information from my php (client) script to a website that is running javascript. Here is an extract of the server's response to my initial http request. I need to know how to communicate further with the server. Thanks, anne
1
1159
by: baxy77bax | last post by:
Hi, as i said in title i need complex example script for communicating with mysql db through dbi . maybe in a contest with some perl program that is calling that script.
0
171
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 srinivasan srinivas wrote: No, the remote PID isn't magically transferred via RSH. The remote script must communicate the PID back. Just writing it remotely as first line and on the client side reading the first line via the subprocess module could do the trick.
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.