linux中线程的挂起与恢复(进程暂停)
来源:互联网 发布:沈阳seo顾问 编辑:程序博客网 时间:2024/04/30 04:48
今天在网上查了一下linux中对进程的挂起与恢复的实现,相关资料少的可怜,大部分都是粘贴复制。也没有完整详细的代码。故自己整理了一下
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); }
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"); }
if (status == RUN) { pthread_mutex_lock(&mut); status = STOP; printf("thread stop!\n"); pthread_mutex_unlock(&mut); } else { printf("pthread pause already\n"); }
int err; static int i = 0; pthread_t child_thread;
if (pthread_mutex_init(&mut, NULL) != 0) printf("mutex init error\n"); if (pthread_cond_init(&cond, NULL) != 0) printf("cond init error\n");
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;
程序流程为:主线程创建子线程(当前子线程状态为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)
{
}
void thread_resume()
{
}
void thread_pause()
{
}
int main()
{
#if 0
#endif
}
0 0
- linux中线程的挂起与恢复(进程暂停)
- linux中线程的挂起与恢复(进程暂停)
- linux中线程的挂起与恢复(进程暂停)
- linux中线程的挂起与恢复(进程暂停)(2013-06-19 13:42:58)
- Linux中对线程的挂起与恢复的实现
- linux线程的挂起与恢复
- 线程的挂起与恢复
- 线程的挂起与恢复
- 线程的挂起与恢复
- 线程的挂起与恢复
- 每天学点Linux--(进程的挂起与恢复)
- Android中java线程的暂停与恢复
- 根据Linux 线程挂起与唤醒原理,实现Sleep的暂停与继续
- 线程挂起与恢复
- 线程挂起与恢复
- 进程与线程(二):线程的开启与挂起
- Linux 进程的挂起和恢复
- Linux 进程的挂起和恢复
- 基础算法系列(三)查找算法之分块查找
- Contest1319 - 期末习题汇总(二)计算机基础---进制转换相关
- BAT批处理如何截图
- ListView的优化方案
- HLSL 像素着色器ps_2_0运行没问题,ps_3_0运行有问题
- linux中线程的挂起与恢复(进程暂停)
- C语言基础:C语言结构体(1) - 结构体的基本认识
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
- 很多win8.1的用户在装了SQL server2008后在连接数据库时经常出现错误
- 没有 Android世界会是怎么样的
- Entoloma saundersii Mushroom
- 一元二次方程
- iOS-TextView、UIButton加边框,加下划线
- 全球最大的搜索引擎排名~~~~~~~~!!!!