我想了解上下文切換如何工作以及如何在接收特定信號後使您的進程切換上下文。這裏是我的代碼上下文切換執行相同的語句兩次
#include<stdio.h>
#include<stdlib.h>
#include<ucontext.h>
#include<signal.h>
#include<sys/time.h>
#include<sys/types.h>
#include<unistd.h>
#define STACK_SIZE 4096
static ucontext_t thread1, thread2;
void thread1_fun() {
static int a = 1;
while (1) {
printf("calling thread1 for %d time\n", a);
sleep(5);
a > 20 ? a = 0 : a++;
}
}
void thread2_fun() {
static int a = 1;
while (1) {
printf("calling thread2 for %d time\n", a);
sleep(5);
a > 20 ? a = 0 : a++;
}
}
void sig_handler(int signal) {
static int curr_thread = 0;
printf("received signal %d\n", signal);
if (curr_thread == 1) {
curr_thread = 0;
printf("switching from thread1 to thread2\n");
setcontext(&thread1);
} else {
curr_thread = 1;
printf("switching from thread2 to thread1\n");
setcontext(&thread2);
}
}
int main() {
int i = 0;
struct sigaction act;
act.sa_handler = sig_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, NULL);
/* sigaction(SIGTERM, &act, NULL); */
getcontext(&thread1);
thread1.uc_stack.ss_sp = malloc (STACK_SIZE);
thread1.uc_stack.ss_size = STACK_SIZE;
thread1.uc_stack.ss_flags = 0;
makecontext(&thread1, thread1_fun, 0);
getcontext(&thread2);
thread2.uc_stack.ss_sp = malloc (STACK_SIZE);
thread2.uc_stack.ss_size = STACK_SIZE;
thread2.uc_stack.ss_flags = 0;
makecontext(&thread2, thread2_fun, 0);
printf("%d\n", getpid());
while (1);
}
現在我給終端命令'kill -s SIGUSR1'。該過程在收到此信號後切換上下文,但問題在於它會打印'調用線程%d時間'兩次。
例如,如果線程1打印「調用線程1爲時間」,並進入睡眠狀態,雖然線程1,如果我發送信號切換的背景下,線程2開始執行,現在睡着了,如果我再次發送信號至開關然後thread1再次打印'調用thread1爲第3個時間'。理想情況下,它應該從睡眠中出來並增加a的值,正確嗎?爲什麼它打印兩次相同的值?
這裏是由代碼打印輸出:
接收信號10
從線程2切換到線程1
調用線程2 1周時間
接收信號10
從thread1切換到thread2
調用線程1 1周時間
接收信號10
從線程2切換到線程1
調用線程2 1周時間
接收信號10
從線程1切換到線程2
調用thread1 1次
請幫我這個。