Passing data between PHP and C executable in linux

前端 未结 4 515
迷失自我
迷失自我 2021-01-12 15:12

Under Linux ,if I want to pass pure string from PHP to C, how do i do that? what I\'ve tried do far is:

exec(\"./myexec.bin -a mystring\");

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 15:45

    A few options spring immediately to mind:

    • Store the data in a file and pass the filename on the command line. It's easy and simple but does require permissions to create and store files somewhere on the filesystem.

    • Open a pipe between your program and the C program; leave both processes running, at least until the C program has consumed the entire contents of your string. popen() is a convenient wrapper around this approach, but it does assume that standard input is the right destination, and it is unidirectional. Managing the pipes yourself lets you use a different file descriptor -- and you can tell the child which file descriptor to read via a command line argument. (See gpg(1)'s command line option --passphrase-fd to see what I mean.)

    • Use a SysV or POSIX shared memory segment to store you data in PHP, and then attach to the shared memory segment from your C program to read the contents. Note that shared memory segments persist, so you must clean up after them when you are done -- otherwise you will leak memory. This doesn't require permissions to create files in the filesystem and might be a nicer mechanism than dealing with pipes and keeping both processes alive long enough for one to completely write the data and the other to completely read the data.

提交回复
热议问题