Sponsored Content
Top Forums Programming Child threads communicating with main thread via pipes Post 302667479 by Corona688 on Friday 6th of July 2012 10:52:16 AM
Old 07-06-2012
Quote:
Originally Posted by Majortom71
I basically want to inform main that a client disconnected in order to decrement the thread counter.
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.

Code:
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
}

 

10 More Discussions You Might Find Interesting

1. Programming

Implementing 2 pipes between a parent and child process

Hi all, I'm trying to write a program that has some data it wants to send through a filter program(in this case tr), and then recieve the output from that filter program. The way I'm trying to do it is by setting up two pipes between the programs and piping the data in through one pipe and back... (2 Replies)
Discussion started by: bwgoudey
2 Replies

2. Programming

Can SIGTERM to main process kill the detached threads?

Hi, I am stuck up with a strange problem. I am writing an application - a kinda tracker that reads data from memcache and invokes theads to process each record of the memcache. I dont want to join all my threads because my tracker should poll the cache in regular intervals say sum 300... (2 Replies)
Discussion started by: deepti_v25
2 Replies

3. Shell Programming and Scripting

starting many child threads simultaneously

i have a parent process and 5 child process. As soon as the parent process is completed the 5 child processes need to start simultaneously (like multithreading) All I need to do in a shell script the child process is a function can any one help me on this thanks in advance (1 Reply)
Discussion started by: trichyselva
1 Replies

4. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

5. Programming

Parent Thread Of Child Thread

Parent Thread Of Child Thread Suppose a process creates some threads say threadC and threadD. Later on each of these threads create new child threads say threadC1, threadC2, threadC3 etc. So a tree of threads will get created. Is there any way to find out the parent thread of one such... (1 Reply)
Discussion started by: rupeshkp728
1 Replies

6. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

7. Programming

unnamed pipes and threads

Hi! I am writing a C program that will create a child, child will create a thread and the thread will send a message to a unnamed pipe and will print the message before exiting. here is my work: #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include... (6 Replies)
Discussion started by: nimesh
6 Replies

8. Programming

Killing a Child Thread

What is the best way for a parent to kill a child thread that has blocked on a command it cannot finish and will never read another line of its code? Will pthread_cancel() work with a thread that will never stop processing its current line of code? Thanks. (4 Replies)
Discussion started by: Brandon9000
4 Replies

9. IP Networking

Message passing from child to parent using pipes

Hi, I am trying my hand in networking programming in C, and got stuck in piping. I was following some tutorial and did the forking like : while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) ... (4 Replies)
Discussion started by: abhi1988sri
4 Replies

10. UNIX for Advanced & Expert Users

How to kill a thread among several threads belongs to a process?

I would like to know is there any we can kill a single thread among multiple threads belongs to process? Since Signal action is process wise not per thread, i strongly feel that we can not or for that mater from external sources as well single thread can not be killed which is critical section... (2 Replies)
Discussion started by: murali242512
2 Replies
SEMAPHORES(3)						     Library Functions Manual						     SEMAPHORES(3)

NAME
sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy - operations on semaphores SYNOPSIS
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t * sem); int sem_trywait(sem_t * sem); int sem_post(sem_t * sem); int sem_getvalue(sem_t * sem, int * sval); int sem_destroy(sem_t * sem); DESCRIPTION
This manual page documents POSIX 1003.1b semaphores, not to be confused with SystemV semaphores as described in ipc(5), semctl(2) and semop(2). Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the counter atomically, and wait until the counter is non-null and decrement it atomically. sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to be shared between several pro- cesses ( pshared is not zero). LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero. sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically decreases the semaphore count. sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately returns with error EAGAIN. sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be used in asynchro- nous signal handlers. sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem. sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the semaphore at the time sem_destroy is called. In the LinuxThreads implementation, no resources are associated with semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore. CANCELLATION
sem_wait is a cancellation point. ASYNC-SIGNAL SAFETY On processors supporting atomic compare-and-swap (Intel 486, Pentium and later, Alpha, PowerPC, MIPS II, Motorola 68k), the sem_post func- tion is async-signal safe and can therefore be called from signal handlers. This is the only thread synchronization function provided by POSIX threads that is async-signal safe. On the Intel 386 and the Sparc, the current LinuxThreads implementation of sem_post is not async-signal safe by lack of the required atomic operations. RETURN VALUE
The sem_wait and sem_getvalue functions always return 0. All other semaphore functions return 0 on success and -1 on error, in addition to writing an error code in errno. ERRORS
The sem_init function sets errno to the following codes on error: EINVAL value exceeds the maximal counter value SEM_VALUE_MAX ENOSYS pshared is not zero The sem_trywait function sets errno to the following error code on error: EAGAIN the semaphore count is currently 0 The sem_post function sets errno to the following error code on error: ERANGE after incrementation, the semaphore value would exceed SEM_VALUE_MAX (the semaphore count is left unchanged in this case) The sem_destroy function sets errno to the following error code on error: EBUSY some threads are currently blocked waiting on the semaphore. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_mutex_init(3), pthread_cond_init(3), pthread_cancel(3), ipc(5). LinuxThreads SEMAPHORES(3)
All times are GMT -4. The time now is 02:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy