Idleness with pthreads


 
Thread Tools Search this Thread
Top Forums Programming Idleness with pthreads
# 1  
Old 07-12-2012
Idleness with pthreads

Hello,

I am writing a threaded program using the pthread library in linux. I have a master thread that needs to control worker threads, in particular, it has to be able to ensure that none of the worker threads will be running for a specified amount of time, even if the worker threads are active. This means that I cannot use the regular pthread_cond synchronization functions, because the other threads might be executing.

I basically need to replicate the 'suspend' method to control active worker threads. Is there any way to achieve this with pthreads? Or can you suggest other (preferably portable) libraries that give me this functionality?

Thank you very much!

P.S.
Why didn't they just add a suspend method to pthreads? I can see why the a cancel method might be unsafe, but not suspend...
# 2  
Old 07-12-2012
So, you want to start and stop threads at will from the outside?

What 'suspend' method do you want to emulate? I know of none.
# 3  
Old 07-12-2012
Quote:
Originally Posted by Corona688
So, you want to start and stop threads at will from the outside?
.
Yes, that's exactly what I want to do. But just a few minutes ago, I found a webite from INRIA's linuxthread package, which pretty much answers my questions - it's not possible. One alternate "solution" the worker thread poll a global variable (a "request") and based on the value, the worker thread suspends itself on a condition variable.

pauillac.inria.fr/~xleroy/linuxthreads/faq.html?#E.4

Quote:
Originally Posted by Corona688
What 'suspend' method do you want to emulate? I know of none.
Solaris threads has a suspend method, and i believe kernel threads have a suspend method too. But this man page confuses me: unix.com/man-page/all/3t/pthread_resume_np/

It lists a pthread_suspend method....

Any idea why?

---------- Post updated at 10:14 PM ---------- Previous update was at 10:07 PM ----------

Since (apparently) it cannot be done with pthreads, i thought i might try to accomplish this with the clone() system call. However, I keep getting segmentation fault. I have no idea why... (I know the printf isn't thread safe, but it's a quick and dirty way to see if the process is active or not. Even if i'm just doing arithmetic operations, I still get a segmentation fault)

What I'm trying to do is to have the main process "activate" the worker for 1 second every 2 seconds. Is this doable?

Code:
#define _GNU_SOURCE

#include <malloc.h> 
#include <signal.h> 
#include <sched.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <stdlib.h>


int worker() {
    while(1) 
    {
      printf("hello \t");
    }
 
    return 0;
    
}

int main(int argc, char *argv[]) {
    void **child_stack;
    int child_pid;
    
    child_stack = (void **) malloc(16328);
    
    child_pid = clone(worker, child_stack, CLONE_VM|CLONE_FILES, NULL);

    while(1) 
    {
      sleep(1);
      kill(child_pid, SIGSTOP);
      sleep(2);
      kill(child_pid, SIGCONT);
    }
    
    return 0;
}

# 4  
Old 07-13-2012
Using clone() will make your code linux-specific.

The proper way to do this would be to reorganize your code so that threads don't waste time idle, rather than killing and unkilling them from the outside all the time.
# 5  
Old 07-13-2012
Or use standard IPC mechanisms that cause execution to block, like blocking on a read of a message queue or blocking on sempahore using semwait().
# 6  
Old 07-17-2012
Thank you all for your suggestions. I should clarify that I'm developing something for a real time embedded system. I need to implement a TDMA server, so I'm going to have N thread executing periodically, with arbitrary execution times. It could happen that a thread finishes its execution before its timeslot, and I need to insure that the CPU will remain idle in that time to save energy.

My first idea was to simply suspend all the threads whose turn it wasn't, and to activate the one correct thread. Which is why I was looking for a way to implement a 'suspend' method.

jim mcnamara, I'm not sure I understand your suggestion. How could I preempt a running task and block it? The running task does not know when it should wait for synchronization.

Thank you again,
# 7  
Old 07-18-2012
If the thread doesn't know when it should wait for synchronization, that's a serious design flaw. You're trying to write something more akin to an operating system, capable of imposing order from the outside.

Threads are asynchronous, anyway. Your main thread can't possibly know when a worker thread has completed anything unless the worker thread tells it so. If you try and force synchronization from the outside by guessing, you're going to catch it too soon occasionally -- and if main is given reliable information from the thread, why can't the thread use this information itself?

If these jobs are periodic with long stretches of inactivity, why not create new threads on demand instead of keeping old ones around forever? Let them quit when they're done. Threads that don't exist use no CPU.

Last edited by Corona688; 07-18-2012 at 02:50 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

2. Programming

Problem with pthreads

hi i have a code: I found that after exiting from child thread memory isn't freed. I commented everything which is "some actions" here, so thread's function contains only two lines. But it doesn't help. What do I do wrong? Thanks a lot (3 Replies)
Discussion started by: sery0ga
3 Replies

3. HP-UX

pthreads

Hi! I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1, the debug version of the pthread lib. How do I force it to use pthreads? Thanks, :) (3 Replies)
Discussion started by: zackz
3 Replies

4. Programming

Variables betwen pthreads ?

I have a doubt .. ( maybe I don't know ..) Let's say , we got the folowing example ( to understand better what I mean ). We got two POSIX threads ( pthreads ) , one receives some strings and creates a menu ( for that strings , scrollable menu ) , and another one check's to see if it has to... (2 Replies)
Discussion started by: !_30
2 Replies

5. UNIX for Dummies Questions & Answers

Question on Pthreads

How to give superuser privileges while setting the attributes like pthread_attr_setschedpolicy( )?? Even with normal user mode ,it is working fine for me.But in man pages, they have specied that to set the scheduling policy as SCHED_FIFO, the process should have superuser privileges. (0 Replies)
Discussion started by: yashavant.a
0 Replies

6. Programming

Behavior of pthreads

Hi All, I ve written a small program to get started off with pthreads. I somehow feel the program doesnt meet the purpose. Please find the code and the output below. Please find my question at the bottom. #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *PrintThread1(void... (4 Replies)
Discussion started by: nhrraj
4 Replies

7. Programming

PThreads

I have created a thread program, it is attached. My problem is that I need to loop this program multiple times, and basically reset everything including the threads created previously. I try to loop the program, the first run is fine, as always, but the second run of the program, the initialize... (0 Replies)
Discussion started by: justgotthis
0 Replies

8. Programming

pthreads

howcome that pthtreads spawn 2 extra processes? I'm kind of new with pthreads but fork() did not act like this. Anyone who can give me a technical explanation of what happends with mother / daughter processes? Best regards Esaia. (2 Replies)
Discussion started by: Esaia
2 Replies

9. Programming

pthreads

Does any one no of some good web site which will explain about how to program using pthreads in a UNIX enviroment? (6 Replies)
Discussion started by: fishman2001
6 Replies

10. UNIX for Advanced & Expert Users

PThreads

Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads. Kindly state by an example. (3 Replies)
Discussion started by: S.P.Prasad
3 Replies
Login or Register to Ask a Question