I'm getting some behaviour in a program I can't explain that probably has an obvious answer, here goes:
I'm using this block of code I found to play .mp3 tracks (Linux):
pid_t x; // a special kind of int
char kil[20] = "kill -s 9 ";
x = fork(); /* now there's actually two "x"s:
if fork succeeds, "x" to the CHILD PROCESS is the return value of fork (0)
and "x" to the PARENT PROCESS is the actual system pid of the child process.*/
if (x < 0) { // just in case fork fails
puts("fork failure");
exit(-1);
}
else if (x == 0) { // therefore this block will be the child process
execlp("mpg123", "mpg123", "-q",command , 0);
} // see GNU docs, "system" also works
else { printf("from parent: mpg123 is pid %d\nENTER to quit\n", x);
sprintf(kil,"%s%d",kil,x);
getchar(); // wait for user input
system(kil);
printf("All ");
} /* witness that the "else if" and "else" blocks are both executed here in parallel. The "else"
(parent) block is continuous with the rest of the program (since the PARENT PROCESS is actually
the program itself) so... */
printf("done.\n");
exit(0);
I don't particularly understand how it works. I've replaced part of the execlp command with a variable 'command' (line 13), which is a char array.
If I specify 'command' to be a specific value e.g.
//char selection; //selection = getchar(); char command[5]; //command[0]=selection; command[0]='2'; command[1]='.'; command[2]='m'; command[3]='p'; command[4]='3'; command[5]='\0';
The .mp3 file is played fine no problems, however if I uncomment those lines not a thing (and obv. comment out assigning [0] to '2' . The tracks number 0-9.mp3, the intention is for the user to enter a number and the program will play the associated track.
It specifically stops working upon uncommenting the getch() line, even if I don't uncomment the assigning of command[0] to the value of selection, therefore I'm almost certain this is breaking it. Using cin and every other way of getting input I can think of produces the same result...I'm at a total loss here, if anyone could help I'd be v. grateful.
NB// I have it outputting the value stored in command to terminal and the output looks totally fine e.g. "7.mp3".
This post has been edited by Mr_GriM: 05 August 2011 - 10:14 AM

New Topic/Question
Reply



MultiQuote




|