關於pause函式時掛起程序還是掛起執行緒的驗證
阿新 • • 發佈:2019-02-01
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
void * timer(void *)
{
for(int i = 0; i< 200; i++)
printf("my name is chen yi cong \n");
return NULL;
}
void *testp(void *)
{
int ret = alarm(2);
pause();
return NULL;
}
int main()
{
//int ret = alarm(5) ;//設定一個定時器
//signal(SIGALRM, timer);
//pause();
pthread_t t1, t2;
printf("begin to test\n") ;
pthread_create(&t1, NULL, &testp, NULL);
pthread_create(&t2, NULL, &timer, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
void * timer(void *)
{
for(int i = 0; i< 200; i++)
printf("my name is chen yi cong \n");
return NULL;
}
void *testp(void *)
{
int ret = alarm(2);
pause();
return NULL;
}
int main()
{
//int ret = alarm(5) ;//設定一個定時器
//signal(SIGALRM, timer);
//pause();
pthread_t t1, t2;
printf("begin to test\n") ;
pthread_create(&t1, NULL, &testp, NULL);
pthread_create(&t2, NULL, &timer, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
輸出如下:
從上面檔案的輸出可以看出,pause只是掛起所在的執行緒,然而當SIGALRM訊號到達的時候,pause函式會將整個程序終止掉。