1

in my previous question: Passing data between PHP and C executable in linux i was able to pass strings with

proc_open();

in PHP and

fget(stdin,"r");

in C ; after that, i used strtok(); to split up my string and turn them into values in C

is there any other ways to pass data structure? since my way doesn't seems to be a good idea

Community
  • 1
  • 1
tom91136
  • 8,662
  • 12
  • 58
  • 74
  • This is starting to sound more and more like homework :) is it? – Ahmed Masud Nov 28 '11 at 10:56
  • not really, i was trying to do image processing using FFT and other stuff with PHP , and figured out that PHP is way too slow to do FFT and image related task, so i decided to switch to C. problem appears, the ultimate result have to be displayed via browser.. – tom91136 Nov 28 '11 at 11:01
  • anyways, it's my little personal project... – tom91136 Nov 28 '11 at 11:02
  • Fair enough... okay so there are a couple of ways of passing the data along. The easiest is to do it over command-line or in a file, but if i read your previous question correctly you are passing a whole bunch of data ? – Ahmed Masud Nov 28 '11 at 11:04
  • yep, i was trying to pass the entire image array(i've turned them into a long string) – tom91136 Nov 28 '11 at 11:05
  • You know easiest would be to save the image to a temporary file :) and pass the path to it right? OR you could use a UNIX socket (but that seems to be a pain in this case). If you really wanted to you could extend PHP by developing a PECL ... http://php-baustelle.de/CodeGen_PECL/manual.html and you can check out http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/ – Ahmed Masud Nov 28 '11 at 11:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5400/discussion-between-ahmed-masud-and-tom91136) – Ahmed Masud Nov 28 '11 at 11:15

1 Answers1

1

On unix and therefore linux, text is the universal interface. You can pass text via stdin/stdout and parameters to scripts and binary programs, and also convey information with environmental variables. All you have to do is parse the text. But Data Structures like hash-maps,linked lists and tree sets,etc can not be passed between distinct processes.

So using this methods are classics on command like scripts and binary programs, after that there is using file locking to read text file databases which is messy to say the least.

And then is the realm of binary data passing between processes, which MUST be encoded in some kind of common protocol. Here you start playing with file pipes, unix sockets and network sockets.

I won't get into details of each, network sockets even the local loopback device is very favored today for universal client/server style of communication. because allows multiplexing of communications and is easily portable.

I won't give you code, you have plenty of data to google now and code for every single kind of communication would be way too big.

Here are some tutorials on sockets:

www.linuxhowtos.org/C_C++/socket.htm
http://gnosis.cx/publish/programming/sockets.html

AS one last caveat. Sockets are not as simple as stdin/stdout data passing, so your needs really must be complex enough to justify the use of sockets data transmision.

stdin/stdout is practical for most wrapping situation, specially for glue code.

Edit: there is also a "third option", using a database like mysql or postgres, where the sockets are wrapped by the database api. An the API and bindings for many popular languages as PHP, C, java, perl, python, ruby, etc, allow inter process data exchange in a ordered and safe way. But then you have to learn a database api, sql, maybe some normalization and best practices... Your experience and available features will be greater but not necessarily quicker or easier.

Community
  • 1
  • 1
RedComet
  • 1,192
  • 5
  • 11