[Asterisk-bsd] [FreeBSD 5.2.1. Only] Music On Hold forkblocksother threads

Chris Stenton asterisk-bsd@lists.digium.com
Thu, 22 Jul 2004 17:16:44 +0100


Rich wrote :- 

mpg123 -v
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2 and 3.
Version 0.59r (1999/Jun/15). Written and copyrights by Michael Hipp.
...
pkg_info -x mpg
Information for mpg123-esound-0.59r_13: 
.. snip 

Well it looks like it is a config issue cos I seem to have the same
version as you. I also tried the code below which generates a couple of
forked threads and they work ok.

Chris



/*
 * Three threads
 * one that just counts
 * two that fork and use tail -f on the maillog
 * just send a test mail to see if the forks are working
 */

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


void *slave (void *args);
void *slavefork (void *args);

typedef struct {
	int data;
	pthread_mutex_t *mut;
	pthread_cond_t *notFull, *notEmpty;
} simple;


	simple  status;

int main ()
{
	pthread_t sla, slafork, slafork2;

	pthread_create (&sla, NULL, slave, &status);
	pthread_create (&slafork, NULL, slavefork, &status);
	pthread_create (&slafork2, NULL, slavefork, &status);
	for(;;){
		
	}

	

	return 0;
}


void *slave (void *arg)
{
	simple  *status;
	int i;

	status  = (simple  *)arg;

	for(;	/* ever */ ;) {
		
	
		pthread_mutex_lock (status->mut);
		status->data++;
		usleep(500000);
		printf("******slave me me me %d  *********** \n",status->data );
		pthread_mutex_unlock (status->mut);
	}

		return (NULL);
}

#define	BUFSIZE 512

int makefork(void)
{

	pid_t pid;
	int pfildes[2];
	int n;
	char buf[BUFSIZE];
	

	
	if (pipe(pfildes) == -1)    {perror("demo"); return(0);}
	if ((pid = fork()) == -1)   {perror("demo"); return(0);}
	else if (pid == 0) {        /* child:  "tail -f /var/log/maillog     
*/
		close(pfildes[0]);    /* close read end of pipe               */
		dup2(pfildes[1],1);   /* make 1 same as write-to end of pipe  */
		close(pfildes[1]);    /* close excess fildes                  */
		execlp("/usr/bin/tail","tail","-f","/var/log/maillog",NULL);
		perror("demo");       /* still around?  exec failed           */
		return(0);             /* no flush                             */ 
        }
	else {                      /* parent:  "/usr/bin/wc"               */
		close(pfildes[1]);    /* close write end of pipe              */
		dup2(pfildes[0],0);   /* make 0 same as read-from end of pipe */
		close(pfildes[0]);    /* close excess fildes                  */
		printf("slavefork started \n");
		
		for(;;){
			if((n = read(0,buf, BUFSIZE)) > 0)
			{
				write(1,buf,n);
			}
			      
		}
		
		return(1);	        /* parent flushes                       */
        }
	
}



void *slavefork (void *arg)
{
	simple  *status;
	status  = (simple  *)arg;

	makefork();
	
	return (NULL);
}