Child threads communicating with main thread via pipes

I have a simple client/server program I am using for learning purposes.
I have it setup so that after server is setup and listening it than goes into a loop where it accepts incoming client connections. After each connection, the client socket is than passed to a thread routine where it can be processed.
I now need to pass information from the client to the server only.

My question is what is the best way to do this?
I have a loop in my main thread and if I do want to establish communication via a pipe.
Should I put most of my main server code into another thread so that I can read pipe data within main?

Another question is are pipes good for doing this?
I was thinking about using a global variable but that requires making it thread safe.
I am still new regarding threads so I was just thinking what's the best option for me.

If you would like to see code please let me know I can post it.

Thank you.

Up to you... if you wish to share your work, and hopefully accept people discussing your code, go ahead...
Dont forget to use code tags for that:

I basically want to inform main that a client disconnected in order to decrement the thread counter. Right now my program is setup to only create a certain number of threads to serve the connecting clients. If the client disconnects the thread count needs to be decremented so that it does not max out and not allow further threads to be made for waiting clients.

Now I can use a global variable, but I am doing this to learn more about Unix system programming and I thought either pipes or message queues would be interesting to try out. My problem is I am not sure how my code can utilize this to send information from child thread to main thread.

I wrote some quick and dirty code as an to test out different ways of what I need to do.
In my code I commented where I want to send message back to main process.

I have client code if you do need I can post, but the code below should be sufficient.

Thanks for any help! :slight_smile:


#include <stdio.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFERSIZE	512
#define NTHREADS	10
#define SERVER_PORT 6001
#define POOL_SIZE 5

void *main_client_thread(void *arg);

typedef struct mainthreadArg
{
	int csocketId;
	int threadCounter;
}mtArg;

int main(int argc, char** argv)
{
	int csock, ssock;
	socklen_t addr_len;
	pthread_t cthread[NTHREADS];
	
	struct sockaddr_in addr;
	bzero((void *)&addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(SERVER_PORT);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	addr_len = sizeof(addr);
	
	/* socket */
	if((ssock=socket(PF_INET, SOCK_STREAM, 0))==-1)
	{
		fprintf(stderr,"Unable to create server socket\n");
		exit(1);
	}
	
	/* bind */
	if(bind(ssock, (struct sockaddr *)&addr, sizeof(addr))==-1)
	{
		fprintf(stderr,"bind failed.\n");
		exit(1);
	}

	
	/* listen */
	if(listen(ssock, POOL_SIZE)==-1)
	{
		fprintf(stderr, "listen failed.\n");
		exit(1);
	}
	
	mtArg ta;
	ta.threadCounter = 0;   /* thread counter */
	
	/* main loop to manage clients */
	while(1)
	{
		if(ta.threadCounter > NTHREADS)
		{
			printf("No more threads can be created. Sorry\n");
			exit(1);
		}
		
		/* accept */
		if((csock=accept(ssock, (struct sockaddr *)&addr, &addr_len))==-1)
		{
			fprintf(stderr, "accept failed.\n");
			exit(1);
		}

		ta.csocketId = csock;
		if((pthread_create(&cthread[ta.threadCounter], NULL, main_client_thread, &ta)) != 0)
		{
			fprintf(stdout, "pthread was not created\n");
			exit(0);
		}
		ta.threadCounter++;
	}
	
	return 0;
}

void *main_client_thread(void *arg)
{
	mtArg t = *(mtArg *)arg;
	int cli,cnt;
	cli = t.csocketId;
	cnt = t.threadCounter;
	
	int stayConnected = 1;
	int nread;
	/* int nwrite; */
	char buffer[BUFFERSIZE];
	
	while(stayConnected == 1)
	{
		bzero(buffer, sizeof(buffer));
		if((nread=read(cli, buffer, BUFFERSIZE)) > 0)
		{
			printf("Thread %d read %d bytes from client.\n", t.threadCounter, nread);
			printf("Thread %d buffer=%s\n", t.threadCounter, buffer);
		}
		else  /* client disconnected */
		{
			printf("nread failed.\n");
			t.threadCounter--;
			/* report back to main thread so that thread counter is decremented */
                        /* use pipe or message queue for this */
			stayConnected = 0;
		}
	}
	return NULL;
}

Why not use a semaphore? They're threadsafe counters which do exactly what you want. See man sem_init and man sem_wait and man sem_post.

sem_t sem;

void main_thread(void)
{
        sem_init(&sem, 0, 5); // Create sem with value of 5
        while(1)
        {
                sem_wait(&sem); // If sem goes below 5, main will wait until something else adds
                if(!running) break;
                launch_thread(threadfunc);
        }
}

void threadfunc(void)
{
        while(1)
        {
                do_stuff;
                if(!running) break;
        }

        sem_post(&sem); // add 1 to sem
}