Linux中线程的挂起与恢复(进程暂停)
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中线程的挂起与恢复(进程暂停)的更多相关文章
- Java知多少(65)线程的挂起、恢复和终止
有时,线程的挂起是很有用的.例如,一个独立的线程可以用来显示当日的时间.如果用户不希望用时钟,线程被挂起.在任何情形下,挂起线程是很简单的,一旦挂起,重新启动线程也是一件简单的事. 挂起,终止和恢复线 ...
- Linux中线程使用详解
线程与进程为什么有了进程的概念后,还要再引入线程呢?使用多线程到底有哪些好处?什么的系统应该选用多线程?我们首先必须回答这些问题. 使用多线程的理由之一是和进程相比,它是一种非常"节俭&qu ...
- JAVA多线程之线程的挂起与恢复(suspend方法与resume方法)
一,介绍 本文讨论JAVA多线程中,使用 thread.suspend()方法暂停线程,使用 thread.resume()恢复暂停的线程 的特点. 先介绍二个关于线程的基本知识: ①线程的执行体是r ...
- JAVA并发实现三(线程的挂起和恢复)
package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...
- Linux - Linux中线程为何有PID?
重现 用htop的Tree view(按F5)之后查看线程 参考 https://segmentfault.com/q/1010000003586656 mousycoder的回答 https://u ...
- linux中线程池【转】
本文转载自:http://blog.csdn.net/yusiguyuan/article/details/18401277 一.线程池 大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时 ...
- 如何在 Linux 中找出 CPU 占用高的进程
1) 怎样使用 top 命令找出 Linux 中 CPU 占用高的进程 在所有监控 Linux 系统性能的工具中,Linux 的 top 命令是最好的也是最知名的一个.top 命令提供了 Linux ...
- 在 Linux 中找出 CPU 占用高的进程
列出系统中 CPU 占用高的进程列表来确定.我认为只有两种方法能实现:使用 top 命令 和 ps 命令.出于一些理由,我更倾向于用 top 命令而不是 ps 命令.但是两个工具都能达到你要的目的,所 ...
- 在 Linux 中找出内存消耗最大的进程
1 使用 ps 命令在 Linux 中查找内存消耗最大的进程 ps 命令用于报告当前进程的快照.ps 命令的意思是"进程状态".这是一个标准的 Linux 应用程序,用于查找有关在 ...
随机推荐
- 粉刷匠(bzoj 1296)
Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. 每个 ...
- 51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂
这道题我写了两种写法 一种利用逆元 a/b%mod=a*c%mod; (c是b的逆元)易得2的逆元就是5~~~04: 一种是矩阵快速幂 利用递推式得出结论 #include<cstdio> ...
- python 实现定时循环触发某个方法
直接贴上代码 import threading def sayhello(): print "hello world" global t #Notice: use global v ...
- mysql七:数据备份、pymysql模块
阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- linux平台从源码安装git【转】
转自:http://blog.csdn.net/lianshaohua/article/details/50571560 版权声明:本文为博主原创文章,未经博主允许不得转载. 如果是ubuntu等能自 ...
- Tomcat下载安装及常见问题解决办法
一.Tomcat的下载: 下载地址:http://tomcat.apache.org/ 下载Tomcat6.0(在左侧的Download下,考虑到稳定性现在企业大部分还在用Tomcat6.0) (1) ...
- MATLAB二维插值和三维插值
插值问题描述:已知一个函数上的若干点,但函数具体表达式未知,现在要利用已知的若干点求在其他点处的函数值,这个过程就是插值的过程. 1.一维插值 一维插值就是给出y=f(x)上的点(x1,y1),(x2 ...
- 对tmemorystream的一些改进_delphi教程
http://www.cnblogs.com/linyawen/archive/2010/12/11/1903072.html 怎么又是关于Stream的,呵呵,应该说只是最近比较关心程式的效率问题, ...
- [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1285 Solved: 708[Submit][Statu ...
- AC日记——Card Game codeforces 808f
F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...