測試程式範例說明:
  1. 每個 thread 都先變更信號處理程序,再設定一個計時器,之後再 sleep 15 秒,就結束執行。
  2. 主要 thread 會先變更信號處理程序,再建立子 threads,之後再設定一個計時器,最後等待子 threads 結束。
#include <pthread.h> #include <stdio.h> #include <sys/signal.h> #include <sys/time.h> #define NUMTHREADS 3 void sighand2(int signo) { pthread_t tid = pthread_self(); printf("a.Thread %lu in signal handler \n", tid); return; } void *threadfunc(void *parm) { pthread_t tid = pthread_self(); struct sigaction actions; struct itimerval itv; int rc; memset(&actions, 0, sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags = 0; actions.sa_handler = sighand; rc = sigaction(SIGALRM,&actions,NULL); itv.it_interval.tv_sec = (tid & 0xf) + 1; itv.it_interval.tv_usec = 0; itv.it_value = itv.it_interval; setitimer(ITIMER_REAL, &itv, NULL); printf("start time=%ld\n", time(NULL)); printf("Thread %u(%x) entered\n", tid, tid); rc = sleep(15); if (rc != 0) { printf("Thread %u did not get expected results! rc=%d,%ld\n", tid, rc, time(NULL)); pthread_exit(NULL); } printf("Thread %lu completed masked work\n", tid); pthread_exit(NULL); } void sighand(int signo) { pthread_t tid = pthread_self(); printf("Thread %lu in signal handler \n", tid); return; } int main(int argc, char **argv) { int i, rc; struct itimerval itv; struct sigaction actions; pthread_t threads[NUMTHREADS]; printf("Enter Testcase - %s\n", argv[0]); printf("Set up the alarm handler for the process\n"); memset(&actions, 0, sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags = 0; actions.sa_handler = sighand; sigaction(SIGALRM,&actions,NULL); printf("Create unmasked threads\n"); for(i=0; i<NUMTHREADS; ++i) { rc = pthread_create(&threads[i], NULL, threadfunc, NULL); if (rc != 0) { printf("%d, %s\n", rc, strerror(rc)); return -1; } } itv.it_interval.tv_sec = 2; itv.it_interval.tv_usec = 0; itv.it_value = itv.it_interval; setitimer(ITIMER_REAL, &itv, NULL); printf("Wait for unmasked threads to complete\n"); for(i=0; i<NUMTHREADS; ++i) { rc = pthread_join(threads[i], NULL); } printf("Main completed\n"); return 0; } 使用 NPTL on i386 結果說明:
  1. main thread 跟 child threads 的定時器共用,也就是只有一個可以用。
  2. main thread 定時 2s,child threads 定時 1s,如果 main thread 先跑,則定時器為會改回 1s,如下結果跑了 15 次 alarm。
  3. main thread 跟 child threads 的 signal 共用,也就是只有一個可以用。
  4. 當信號觸發時,會依照 thread 生成時間來決定執行的 thread,如下結果所示,都是由 main thread 來執行。
  5. threads 的 pid 都一樣,符合 POSIX 規定。
  6. 信號處理函數共用,後設定的會覆蓋掉原設定的值。
Enter Testcase - ./a.out Set up the alarm handler for the process Create unmasked threads Wait for unmasked threads to complete start time=1242723595 Thread 3086281616(b7f4eb90), pid=25624 entered start time=1242723595 Thread 3075791760(b754db90), pid=25624 entered start time=1242723595 Thread 3065301904(b6b4cb90), pid=25624 entered a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler a.Thread 3086284480 in signal handler Thread 3086281616 completed masked work a.Thread 3086284480 in signal handler Thread 3075791760 completed masked work Thread 3065301904 completed masked work Main completed 使用 LinuxThreads on arm7 結果說明:
  1. 所有 threads 的定時器各自獨立,不會互相干擾。
  2. 所有 threads 的信號接受各自獨立,不會互相干擾。
  3. threads 的 pid 都不同,不符合 POSIX 規定。
  4. 信號處理函數共用,後設定的會覆蓋掉原設定的值。
Enter Testcase - ./aa Set up the alarm handler for the process Create unmasked threads start time=1242781260 Thread 1026(402), pid=218 entered start time=1242781260 Thread 2051(803), pid=219 entered Wait for unmasked threads to complete start time=1242781260 Thread 3076(c04), pid=220 entered a.Thread 1024 in signal handler a.Thread 1026 in signal handler Thread 1026 did not get expected results! rc=12,1242781263 a.Thread 1024 in signal handler a.Thread 2051 in signal handler Thread 2051 did not get expected results! rc=11,1242781264 a.Thread 3076 in signal handler Thread 3076 did not get expected results! rc=10,1242781265 Main completed
創作者介紹

邱小新の工作筆記

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()