Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


Closed Thread
  Search this Thread
Old 09-21-2009, 05:27 PM   #1
LQ Newbie
Registered: Sep 2009
Posts: 8

Rep: Reputation: 0
Forking and piping problem.

Hi all. Im pretty new to linux but having some random problem with my app that has left me scratching my head for the last couple of days and was hoping you guys could help.

My app is using MPG321 to decode .mp3 files and i am having trouble killing the mpg forked task, both when i want to stop in the middle of a song(starting a new one) or starting a new song after completing last song.

I am piping the output of the MPG321 app to my application to get relevant track info but i'm pretty sure this is creating a problem when trying to destroy the forked process. If i do not attach the output to the pipe i don't get a problem.

Here is the relavent code

				if (pipe(inst->_info_pipe))
					__SERVERDBG("Pipe failed");

				inst->_SongLen = 0;

				frk_pid = fork ();

				if (frk_pid == (pid_t) 0)
					dup2(inst->_info_pipe[1], 2);



				else if (frk_pid < (pid_t) 0)

					__SERVERDBG("Pipe failed");
					inst->_main_thread = -1;

					inst->_current_state = UNDEFINED;
					__SERVERDBG("fork parent %i", frk_pid);
					inst->_main_thread = frk_pid;

		//			fcntl(inst->_info_pipe[0], F_SETFL, O_NONBLOCK);

					inst->_current_state = PLAYING;
		else if(inst->_info_pipe[0] != -1) // MP3 checks
			if(inst->_current_state == PLAYING )
			 // wont return from here till song is finished
					char* Tmp;
					int r = read(inst->_info_pipe[0], Tmp,50);

					if(r > 0)
						char tmp1[r +1];
						memcpy(tmp1, Tmp, r);
						tmp1[r] = '\0';

						if(strncmp("ALSA", tmp1, 4) == 0)
							__SERVERDBG("%s", tmp1);
					//	__SERVERDBG("%s", tmp1);

						inst->ParseOutput(Tmp, r);
				}while(inst->_current_state == PLAYING);
this is my destroy process

void MP3Controller::StopPlayerApp()
	pid_t t;
	int reply;

	if(_main_thread == -1) // not thread running

	__SERVERDBG("Stopping Player %i", _main_thread);
	//Stop player
   int i = close(_info_pipe[0]); // shut pipe from MP3 app

	__SERVERDBG("Close pipe %i err %i", i , errno);


	reply = kill(-_main_thread, SIGTERM);

	__SERVERDBG("Stopping Player1 reply %i err %i", reply, errno);

	t = waitpid (_main_thread, NULL, WNOHANG);

	__SERVERDBG("waitpid %i err %i", t, errno);

	/*if (t != _main_thread && t != -1)
		/* this one WILL HANG, so if someone sent a SIGSTOP to mpg123...
		t = waitpid (_main_thread, NULL, 0);

		__SERVERDBG("waitpid %i err %i", t, errno);

	__SERVERDBG("Stopping Player2");
	_main_thread = -1;

	// Close pipes

    _info_pipe[0] = -1;
    _info_pipe[1] = -1;

	__SERVERDBG("Stopping Player3");

    __SERVERDBG("Stopping MP3");

i currently get an error from the waitpid() function. of 10 which is app dosent exist..?

when i do a ps in the terminal i see that the task has Zombied..(is that a word?). i can however start a new instance of mpg from command line without problem..

This was working on my pc but since porting to an embedded solution ... NO DEAL..

Thanks in advance for any help


Last edited by ajb181; 09-21-2009 at 06:28 PM.
Old 09-22-2009, 12:00 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql problem with forking in TCL jlinkels Programming 3 09-04-2009 06:42 PM
Linux piping vs Windows piping emanabela Linux - Newbie 1 05-10-2009 12:58 PM
piping forking and 2 way communication kev000 Programming 3 10-09-2006 11:10 PM
basic piping problem corck Programming 4 08-01-2005 03:30 AM
piping /forking geminigal Programming 3 04-10-2005 09:49 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration