Question

I am working on Linux and C/C++. I wrote a program with some threads (#include pthread.h) and I run it with sudo.

One thread runs a process (mplayer) and leaves it running by adding " &", so that system() can return quickly.

system("mplayer -loop 0 /mnt/usb/* &");

The mplayer process runs normally and plays music as expected.

After that, I get its process ID by running pidof. Let's say that it returns 2449. A posix mutex is used to write/read that process ID on this thread and on the second thread.

On the second thread I try to write data to mplayer by using the /proc/2449/fd/0 pipe (is it called a pipe or stream?):

system("echo \">\" > /proc/2499/fd/0");

system() returns 0, but the mplayer process does not get anything. The ">" command should play the next track.

Is the stdin stream being inherited by some other process?

There are several fd's listed under the 2449 process, is one of them (besides 0) the stdin stream?

root@pisanlink:/proc# cd 2499
root@pisanlink:/proc/2499# cd fd
root@pisanlink:/proc/2499/fd# ls
0  1  2  3  4  5  7
root@pisanlink:/proc/2499/fd# 

I also tried another approach... I used popen() with write permissions. I tried sending the command with fprintf, but mplayer didn't seem to receive anything as well.

If any more code is needed, please let me know.

Any hints will be appreciated. Thanks.

Was it helpful?

Solution 2

I used the mplayer slave option and the input as a fifo file. It is working correctly.

Create the Linux fifo file with mkfifo:

system("mkfifo /tmp/slpiplay_fifo");

Open mplayer with:

system("mplayer -slave -idle -really-quiet -input file=/tmp/slpiplay_fifo /mnt/usb_slpiplay/* &");

Pass a "next" command to mplayer by using the fifo:

system("echo \"pt_step 1\" >> /tmp/slpiplay_fifo");

OTHER TIPS

Use popen (not system) to open the process. It will create the process with a pipe that you can either read from or write to (but not both). In your case, you'd open it with "w" for writing. From there you can simply use fwrite to send data to the process' stdin.

Pseudo-code Example:

FILE * pFile = popen("mplayer -loop 0 /mnt/usb/*", "w");

if(pFile == NULL)
   // Handle error

// Send ">" to process' stdin
const char * psData = ">";
const size_t nDataLen = strlen(psData);
size_t nNumWritten = fwrite(psData, 1, nDataLen, pFile);

if(nNumWritten != nDataLen)
   // Handle error

...

pclose(pFile);
pFile = NULL;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top