The following example runs hcidump on a child thread.  This happens to be a bluetooth bluez tool which continuously outputs events as it seens them. This code allows this to happen on a child thread with the output passed back to the main application thread using a named pipe.

Create the pipe and the child thread process

#include <iostream>
#include <fcntl.h>
#include <unistd.h>

int our_input_fifo_filestream = -1;

	//--------------------------------------
	//----- CREATE A FIFO / NAMED PIPE -----
	//--------------------------------------
	int result;

	printf("Making FIFO...\n");
	result = mkfifo(OUR_INPUT_FIFO_NAME, 0777);		//(This will fail if the fifo already exists in the system from the app previously running, this is fine)
	if (result == 0)
	{
		//FIFO CREATED
		printf("New FIFO created: %s\n", OUR_INPUT_FIFO_NAME);
	}

	printf("Process %d opening FIFO %s\n", getpid(), OUR_INPUT_FIFO_NAME);
	our_input_fifo_filestream = open(OUR_INPUT_FIFO_NAME, (O_RDONLY | O_NONBLOCK));
					//Possible flags:
					//	O_RDONLY - Open for reading only.
					//	O_WRONLY - Open for writing only.
					//	O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
					//											if there is no input immediately available (instead of blocking). Likewise, write requests can also return
					//											immediately with a failure status if the output can't be written immediately.
	if (our_input_fifo_filestream != -1)
		printf("Opened input FIFO: %i\n", our_input_fifo_filestream);


	//----------------------------------------------
	//----- RUN HCIDUMP ON A BACKGROUND THREAD -----
	//----------------------------------------------
	int pid2 = fork();
	if(pid2 == 0)
	{
		//----- THIS IS THE CHILD THREAD -----
		//cout << "I am the child" << endl;

		FILE* pipe2 = popen("sudo hcidump", "r");		//Send the command, popen exits immediately 
		if (pipe2)
		{
			//----- THIS CHILD THREAD WILL RUN FOREVER -----
			cout << "Bluetooth hcidump child thread started" << endl;

			//Create an output filestream for this child thread
			int our_output_fifo_filestream = -1;
			our_output_fifo_filestream = open(OUR_INPUT_FIFO_NAME, (O_WRONLY | O_NONBLOCK));
								//Possible flags:
								//	O_RDONLY - Open for reading only.
								//	O_WRONLY - Open for writing only.
								//	O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
								//											if there is no input immediately available (instead of blocking). Likewise, write requests can also return
								//											immediately with a failure status if the output can't be written immediately.
			if (our_output_fifo_filestream != -1)
				cout << "Opened output FIFO: " << our_output_fifo_filestream << endl;

			char buffer2[128];
			while(!feof(pipe2))						//Wait for the output resulting from the command
			{
				if(fgets(buffer2, 128, pipe2) != NULL)
				{
					write(our_output_fifo_filestream, (void*)buffer2, strlen(buffer2));	
					//cout << buffer2 << endl;
				}
			}
			cout << "Bluetooth hcidump child thread ending" << endl;
			pclose(pipe2);
		}
		_exit(0);		//Don't forget to exit!
	}
	else
	{
		//cout << "I am the parent\n" << endl;
		wait();		//Optional if you want to wait on the child thread, remove if not
	}

Reading the output from the main application thread

	//---------------------------------------------
	//----- CHECK FIFO FOR ANY RECEIVED BYTES -----
	//---------------------------------------------
	// Read up to 255 characters from the port if they are there
	if (our_input_fifo_filestream != -1)
	{
		unsigned char buffer3[256];
		int length3 = read(our_input_fifo_filestream, (void*)buffer3, 255);		//Filestream, buffer to store in, number of bytes to read (max)
		if (length3 < 0)
		{
			//An error occured (this can happen)
		}
		else if (length3 == 0)
		{
			//No data waiting
		}
		else
		{
			//Bytes received
			buffer3[length3] = '\0';
			cout << buffer3 << endl;
		}
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. Kennethmor

    3 years ago

    Good afternoon . Home maid clean and Best Spring Cleaning Service specialized international company more than seven years. We are working with different customers: car dealers, cultural and entertainment establishments. Doing put away Housekeeping services. We guarantee fulfillment specified deadlines and safety any property . Order in our company Tidying Dry cleaning carpets,Maid service at home and Best spring cleaning. Our specialized holding individually for any client and calculating for cleaning proceeding from area premises . 7-year-old practical experience in the field cleaning gives an opportunity Home maid clean predict possible difficulties, arising in progress Cleaning Elite cleaning and Best spring cleaning and Maid service country house , houses . Working with modern equipment and highly professional cleaning products, we and Spring Cleaning we achieve impeccable results and ready tidying up as in city and country mansions and cottages. Offers company profitable those who appreciates their free time and not at all wants spend it on put away Bathroom cleaning. Make a call or write to our mail , then to learn price and buy take away Removal of complex contaminants, Maid service, Spring Cleaning and генеральную уборку Вашего дома.

    In property cleaning services – perfect
    [url=https://maidsmanhattan.club/maid-for-a-day/]Maid for a day[/url]
    – it is actually simple, practical as well as inexpensive along with our company.
    Leave the sanitation forerunners to home cleansing Brooklyn! Our company make use of professional cleaning agents and technical devices of worldwide producers in our work and perform an outstanding project along with cleaning of any complication.

    [url=https://maidsmanhattan.club/][size=8][/size][/url]
    Collaboration along with the firm is the backer of an exquisite, profitable and also dependable cleansing of specialist home cleansing as

Comments

Your email address will not be published. Required fields are marked *