当前位置: 技术问答>linux和unix
在linux中如何让子线程挂起、唤醒?
来源: 互联网 发布时间:2014-12-13
本文导语: 就像windows里的suspend、resume那样 | 用信号量来控制吧, suspend 用 sem_wait resume 用 sem_post | #include #include static void sig_proc(int); pid_t chld_pid; int main(void) { ...
就像windows里的suspend、resume那样
|
用信号量来控制吧,
suspend 用 sem_wait
resume 用 sem_post
suspend 用 sem_wait
resume 用 sem_post
|
#include
#include
static void sig_proc(int);
pid_t chld_pid;
int main(void)
{
int flag = 0;
if((chld_pid = fork())==0){
if(signal(SIGALRM,sig_proc)==SIG_ERR){
perror("register signal process failn");
return 1;
}
alarm(10);
for(;;){
printf("I am child process, I am runningn");
sleep(1);
}
}
else{
if(signal(SIGUSR1,sig_proc)==SIG_ERR){
perror("register signal process failn");
return 1;
}
pause();
sleep(10);
kill(chld_pid,SIGCONT);
printf("I am father process, I have waked the child process upn");
sleep(10);
kill(chld_pid,SIGQUIT);
printf("I am father process, I have stopped the child processn");
}
return 0;
}
static void sig_proc(int in_sig)
{
switch(in_sig){
case SIGUSR1:
kill(chld_pid,SIGSTOP);
printf("I am father process, I have suspended the child processn");
break;
case SIGALRM:
kill(getppid(),SIGUSR1);
break;
default:
break;
}
}
The result is:
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am father process, I have suspended the child process
I am father process, I have waked the child process up
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am father process, I have stopped the child process
#include
static void sig_proc(int);
pid_t chld_pid;
int main(void)
{
int flag = 0;
if((chld_pid = fork())==0){
if(signal(SIGALRM,sig_proc)==SIG_ERR){
perror("register signal process failn");
return 1;
}
alarm(10);
for(;;){
printf("I am child process, I am runningn");
sleep(1);
}
}
else{
if(signal(SIGUSR1,sig_proc)==SIG_ERR){
perror("register signal process failn");
return 1;
}
pause();
sleep(10);
kill(chld_pid,SIGCONT);
printf("I am father process, I have waked the child process upn");
sleep(10);
kill(chld_pid,SIGQUIT);
printf("I am father process, I have stopped the child processn");
}
return 0;
}
static void sig_proc(int in_sig)
{
switch(in_sig){
case SIGUSR1:
kill(chld_pid,SIGSTOP);
printf("I am father process, I have suspended the child processn");
break;
case SIGALRM:
kill(getppid(),SIGUSR1);
break;
default:
break;
}
}
The result is:
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am father process, I have suspended the child process
I am father process, I have waked the child process up
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am child process, I am running
I am father process, I have stopped the child process
|
可以用互斥锁和条件变量来达到目的。
pthread_cond_wait()
pthread_cond_signal()
可以看一下man
pthread_cond_wait()
pthread_cond_signal()
可以看一下man