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

今天在网上查了一下Linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制。也没有完整详细的代码。故自己整理了一下

程序流程为:主线程创建子线程(当前子线程状态为stop停止状态),5秒后主线程唤醒子线程,10秒后主线程挂起子线程,15秒后主线程再次唤醒子线程,20秒后主线程执行完毕等待子线程退出。

代码如下:
#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)
        {
            pthread_cond_wait(&cond, &mut);
        }
        pthread_mutex_unlock(&mut);
    
        printf("child pthread %d\n", i++);
        if (i == 20) 
            break;
        sleep(1);
    }  
}

void thread_resume()
{
    if (status == STOP)
    {  
        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)
    {
        printf("father pthread %d\n", i++);
        sleep(1);
        if (i == 5)
            thread_resume();
        if (i == 10)
            thread_pause();
        if (i == 15)
            thread_resume();
        if (i == 20)
            break;
    }
    if (0 == pthread_join(child_thread, NULL))
        printf("child thread is over\n");
    return 0;
}

相关阅读:

对Linux中多线程编程中pthread_join的理解 http://www.linuxidc.com/Linux/2013-09/89931.htm

Linux多线程编程时如何查看一个进程中的某个线程是否存活 http://www.linuxidc.com/Linux/2013-09/89930.htm

有关Linux下线程的创建 http://www.linuxidc.com/Linux/2013-08/88530.htm

Linux内核线程死锁或死循环之后如何让系统宕机重启 http://www.linuxidc.com/Linux/2013-04/82063.htm

Linux下C语言实现多线程文件复制 http://www.linuxidc.com/Linux/2013-03/81373.htm

Linux中线程的挂起与恢复(进程暂停)的更多相关文章

  1. Java知多少(65)线程的挂起、恢复和终止

    有时,线程的挂起是很有用的.例如,一个独立的线程可以用来显示当日的时间.如果用户不希望用时钟,线程被挂起.在任何情形下,挂起线程是很简单的,一旦挂起,重新启动线程也是一件简单的事. 挂起,终止和恢复线 ...

  2. Linux中线程使用详解

    线程与进程为什么有了进程的概念后,还要再引入线程呢?使用多线程到底有哪些好处?什么的系统应该选用多线程?我们首先必须回答这些问题. 使用多线程的理由之一是和进程相比,它是一种非常"节俭&qu ...

  3. JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)

    一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...

  4. JAVA并发实现三(线程的挂起和恢复)

    package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...

  5. Linux - Linux中线程为何有PID?

    重现 用htop的Tree view(按F5)之后查看线程 参考 https://segmentfault.com/q/1010000003586656 mousycoder的回答 https://u ...

  6. linux中线程池【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/18401277 一.线程池 大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时 ...

  7. 如何在 Linux 中找出 CPU 占用高的进程

    1) 怎样使用 top 命令找出 Linux 中 CPU 占用高的进程 在所有监控 Linux 系统性能的工具中,Linux 的 top 命令是最好的也是最知名的一个.top 命令提供了 Linux ...

  8. 在 Linux 中找出 CPU 占用高的进程

    列出系统中 CPU 占用高的进程列表来确定.我认为只有两种方法能实现:使用 top 命令 和 ps 命令.出于一些理由,我更倾向于用 top 命令而不是 ps 命令.但是两个工具都能达到你要的目的,所 ...

  9. 在 Linux 中找出内存消耗最大的进程

    1 使用 ps 命令在 Linux 中查找内存消耗最大的进程 ps 命令用于报告当前进程的快照.ps 命令的意思是"进程状态".这是一个标准的 Linux 应用程序,用于查找有关在 ...

随机推荐

  1. [POJ2774]Long Long Message 解题报告

    Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...

  2. 【Atcoder】ARC082 E - ConvexScore

    [算法]计算几何 [题意]给定平面直角坐标系上的若干个点,任意选点连成凸多边形,凸多边形的价值定义为2^(n-|S|),其中n为凸多边形内部点数(含边界),|S|为顶点数,求总价值.n<=10^ ...

  3. 【BZOJ 3907】网格(Catalan数)

    题目链接 这个题推导公式跟\(Catalan\)数是一样的,可得解为\(C_{n+m}^n-C_{n+m}^{n+1}\) 然后套组合数公式\(C_n^m=\frac{n!}{m!(n-m)!}\) ...

  4. react+redux基础用法

    在学react的是,发现一旦我们封装好了我们的组件,那么我们的项目就跟搭积木一样简单快速,可是我们发现了一个问题,在一个页面往往会嵌套很多的组件,子组件必须要通过父组件传递参数才能渲染出数据,我们回想 ...

  5. phpcms v9 后台添加修改页面空白页问题解决方法

    phpcms v9 添加修改页面空白页的解决方法 找一个正常运行的phpcms 将caches\caches_model\caches_data 目录下的 content_form.class.php ...

  6. Android的简单应用(一)——PreferenceFragment实现应用的设置

    今天主要讲解怎么使用PreferenceFragment.PreferenceFragment它与默认的SharedPreferences文件相连接,不需要你再自己写代码去操作SharedPrefer ...

  7. JavaSript中数组方法是否对原数组产生影响

    JavaScript中数组方法有很多.某次面试被问到,concat()方法会对影响到原数组吗.当时记得不牢,犹豫地说"会吧...".于是决定总结一下哪些数组方法会对原数组产生影响. ...

  8. BZOJ 1207

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3089  Solved: 1499[Submit][Statu ...

  9. javascript三种嵌入方式

    什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览器解释执行的. J ...

  10. 【python】抄写大神的百度贴吧代码

    原文链接:http://cuiqingcai.com/993.html 划重点: 1.提取帖子内容时,对图片,贴吧自动增加的超链接,制表符,换行符要做删除或替换处理 2.decode是把bytes转换 ...