#include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <signal.h> //下一步时间间隔 #define TIME_NEXT 50 //定义信号,此处直接使用系统信号,项目中可根据需要自定义信号值#define SIG_UI_QUIT35 #define SIG_PHONE_QUIT 36 #define SIG_UI_QUIT 35 //定义通话状态 enum TASK_PHONE_STATE { TASK_PHONE_STATE_NONE = 0, TASK_PHONE_STATE_RING, TASK_PHONE_STATE_TALK, TASK_PHONE_STATE_HANGUP, }; int phone_state = TASK_PHONE_STATE_NONE; //设置通话状态 void set_state(int state) { phone_state = state; } //获取通话状态 int get_state(void) { return phone_state; } int get_ui_pid() { int pid = -1; FILE *fp = NULL; char buf[12] = {0}; //打开管道,执行 shell 命令查找进程名为task_ui_sig 的pid fp = popen("ps -e I grep \'task_ui_sig\' | awk \'{print $1}\'", "r"); fgets(buf, sizeof(buf), fp); if (strlen(buf) > 0) { pid = atoi(buf); } return pid; } //信号处理函数 void sig_deal(int sig) { if (sig == SIG_UI_QUIT) { printf("Task ui hangup!\n"); set_state(TASK_PHONE_STATE_HANGUP); } } int main(void) { int time = 0; //设置SIG UI QUIT信号处理函数 signal(SIG_UI_QUIT, sig_deal); while (1) { /*模拟与其他用户处理通信协议,每隔5s进入下一状态*/ time++; if (time >= TIME_NEXT) { time = 0; if (get_state() == TASK_PHONE_STATE_RING) { set_state(TASK_PHONE_STATE_TALK); } else if (get_state() == TASK_PHONE_STATE_TALK) { set_state(TASK_PHONE_STATE_HANGUP); } else { set_state(TASK_PHONE_STATE_RING); } printf("Current state is %d!\n", get_state()); /*若当前通话状态为挂断,则退出任务,并发送信号给UI*/ if (get_state() == TASK_PHONE_STATE_HANGUP) { if (get_ui_pid() > 0) { kill(get_ui_pid(), SIG_UI_QUIT); printf("Send quit msg!\n"); } break; } usleep(100 * 1000); } return 0; } }这段代码有什么bug
时间: 2024-02-05 17:05:07 浏览: 117
用c语言实现的逆序文本程序
这段代码存在一个逻辑错误,导致程序无法正常退出。具体来说,在while循环中,当通话状态为挂断时,会调用kill函数发送SIG_UI_QUIT信号给UI进程,然后退出循环。但是,由于kill函数是异步的,它发送信号的时刻是不确定的。因此,如果kill函数在循环结束之后才执行,就会导致UI进程收不到信号,从而无法正常退出。为了解决这个问题,可以在发送信号之后添加一个sleep函数,让程序等待一段时间,确保信号被UI进程接收到。修改后的代码如下:
```c
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#define TIME_NEXT 50
#define SIG_UI_QUIT 35
#define SIG_PHONE_QUIT 36
enum TASK_PHONE_STATE {
TASK_PHONE_STATE_NONE = 0,
TASK_PHONE_STATE_RING,
TASK_PHONE_STATE_TALK,
TASK_PHONE_STATE_HANGUP,
};
int phone_state = TASK_PHONE_STATE_NONE;
void set_state(int state) {
phone_state = state;
}
int get_state(void) {
return phone_state;
}
int get_ui_pid() {
int pid = -1;
FILE *fp = NULL;
char buf[12] = {0};
fp = popen("ps -e I grep \'task_ui_sig\' | awk \'{print $1}\'", "r");
fgets(buf, sizeof(buf), fp);
if (strlen(buf) > 0) {
pid = atoi(buf);
}
return pid;
}
void sig_deal(int sig) {
if (sig == SIG_UI_QUIT) {
printf("Task ui hangup!\n");
set_state(TASK_PHONE_STATE_HANGUP);
}
}
int main(void) {
int time = 0;
signal(SIG_UI_QUIT, sig_deal);
while (1) {
time++;
if (time >= TIME_NEXT) {
time = 0;
if (get_state() == TASK_PHONE_STATE_RING) {
set_state(TASK_PHONE_STATE_TALK);
} else if (get_state() == TASK_PHONE_STATE_TALK) {
set_state(TASK_PHONE_STATE_HANGUP);
} else {
set_state(TASK_PHONE_STATE_RING);
}
printf("Current state is %d!\n", get_state());
if (get_state() == TASK_PHONE_STATE_HANGUP) {
int ui_pid = get_ui_pid();
if (ui_pid > 0) {
kill(ui_pid, SIG_UI_QUIT);
printf("Send quit msg!\n");
sleep(1); // 等待信号被接收
}
break;
}
}
usleep(100 * 1000);
}
return 0;
}
```
阅读全文