Suspension and recovery of threads in Linux (process paused)

Source: Internet
Author: User
Tags thread stop

Http://www.linuxidc.com/Linux/2013-09/90156.htm

Today on the Internet to check the implementation of Linux in the process of the suspension and recovery, the poor data, most of them are paste copy. There is no complete and detailed code. So I tidied up a bit

The program flow is: The main thread to create a child thread (the current sub-thread state is the stop state), 5 seconds after the main thread wakes the child thread, 10 seconds after the main thread hangs the driver threads, 15 seconds after the main thread wakes the child thread again, 20 seconds after the main thread execution waits for the child thread to exit.

The code is as follows:
#include
#include
#include
#include
#include


#define RUN 1
#define STOP 0


pthread_mutex_t mut = Pthread_mutex_initializer;
pthread_cond_t cond = Pthread_cond_initializer;


int status = STOP;
void * Thread_function (void)
{
    static int i = 0;
    while (1)  
    {  
        Pthread_mutex_lock (&mut );
        while (!status)
        {
          &NB Sp Pthread_cond_wait (&cond, &mut);
       }
        Pthread_mutex_unlock (&mut);
    
        printf ("Child pthread%d\n", i++);
        if (i = =)  
            break;
        sleep (1);
   }  
}


void Thread_resume ()
{
    if (status = = STOP)
    {  
    & nbsp   Pthread_mutex_lock (&mut);
        status = RUN;
        pthread_cond_signal (&cond);
        printf ("Pthread run!\n");
        Pthread_mutex_unlock (&mut);
   }  
    Else
    {  
        printf ("Pthread run already\n");
   }  
}


void Thread_pause ()
{
if (status = = RUN)
{
Pthread_mutex_lock (&mut);
status = STOP;
printf ("Thread stop!\n");
Pthread_mutex_unlock (&mut);
}
Else
{
printf ("Pthread pause already\n");
}
}


int main ()
{
int err;
static int i = 0;
pthread_t Child_thread;


#if 0
if (Pthread_mutex_init (&mut, NULL)! = 0)
printf ("Mutex init error\n");
if (Pthread_cond_init (&cond, NULL)! = 0)
printf ("Cond init error\n");
#endif


    err = pthread_create (&child_thread, NULL, (void *) thread_function, null);
    if (err! = 0)
        printf ("Can ' t create thread:%s\n", strerror (Err));
    while (1)
    {
        father ("Pthread%d\n", i++);
        sleep (1);
        if (i = = 5)
            thread_resume ();
        if (i = =)
            thread_pause ();
        if (i = =)
            thread_resume ();
        if (i = =)
            break;
   
    if (0 = = Pthread_join (Child_thread, NULL))
        printf ("Child t Hread is over\n ");
    return 0;
}

Related reading:

Understanding of Pthread_join in multithreaded programming in Linux http://www.linuxidc.com/Linux/2013-09/89931.htm

How to see if a thread in a process is surviving in Linux multithreaded programming http://www.linuxidc.com/Linux/2013-09/89930.htm

About the creation of Linux threads http://www.linuxidc.com/Linux/2013-08/88530.htm

How to restart the system after a Linux kernel thread deadlock or dead loop http://www.linuxidc.com/Linux/2013-04/82063.htm

Linux C language implementation of multi-threaded file replication http://www.linuxidc.com/Linux/2013-03/81373.htm

Suspension and recovery of threads in Linux (process paused)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.