- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是一个非常初级的程序员,我正在 Ubuntu 中开发客户端服务器程序。这段代码属于我的服务器
文件,我想实现一个信号SIGALRM
,但我不知道如何才能正确地做到这一点。我的目标是:在我调用信号 alarm(ans.game_time)
的代码部分,我想运行那个函数 Time,这个函数应该等待 ans.game_time
秒,在这几秒内,玩家应该加入游戏(这最后一部分尚未实现,“播放”命令)。
结构:
typedef struct request req;
struct request
{
char str[256];
int client_pid;
int login; // In case of client, to identify if is logged
int whois; // To identify who is the client and the server
};
typedef struct answer ans;
struct answer
{
char str[256];
int server_pid;
int type;
int login;
int num_users;
char game_name[25];
int game_time;
int game_users[4];
};
服务器文件:
#include "header"
int array_client_PID[4], num_users = 0;
int GAME_STATUS = 0;
void Time(int sign)
{
signal(SIGALRM, Time);
alarm(3);
printf("I'm Alive");
}
int main(){
int fifo_1,fifo_2, pid, i, number_args;
char FIFO_CLIENT[20], command[20], arg_1[20], arg_2[20];
struct request req;
struct answer ans;
signal(SIGALRM, Time);
do{
read(fifo_1, &req, sizeof(req)); // Read request
if(req.login == 1) // USER REGISTATION: If logged
{
number_args = sscanf(req.str, "%s %s %s", command, arg_1, arg_2);
if(strcasecmp(command, "new") == 0)
{
if(GAME_STATUS == 0)
{
ans.game_time = atoi(arg_2); // Converts the string into a integrer time game
strcpy(ans.game_name, arg_2); // Put the name of the game on the structure
ans.game_users[0] = req.client_pid; // Put on the users avaiable to play, the name of the game creator
alarm(ans.game_time);
//CreateGame(ans); // not implemented yet
}
else
{
strcpy(ans.str,"One game is in execution. Just wait...\n");
}
}
if(GAME_STATUS== 1) // GAME STATUS: ON
{
printf("INSIDE GAME_STATUS 1\n");
// Receive commands inside the game
}
}
sprintf(FIFO_CLIENT, "FIFO_%d", req.client_pid); //2nd FIFO name with client PID
fifo_2=open(FIFO_CLIENT, O_WRONLY); // Open 2nd FIFO to answer
write(fifo_2, &ans, sizeof(ans)); // Write an answer
}while(1);
}
我试图理解它是如何正常工作的,但这个信号真的让我感到困惑。
最佳答案
alarm()
不是在服务器中实现周期性事件的好方法。它用作异步信号,因此很难在其他事情发生时进行适当处理。例如,如果您的服务器正在向客户端发送消息,让警报响起并开始生成输出可能会中断消息。底线是,这不是解决此问题的正确方法。
大多数客户端/服务器应用程序在其核心使用select()
或poll()
系统调用。这些系统调用允许您的应用程序在任意数量的文件描述符上等待事件发生(例如,数据到达),并可选择超时。这就是大多数服务器应用程序一次处理与多个客户端的连接的方式。
使用这些系统调用可能需要您围绕“状态机”模型重构您的应用程序,而不是使用程序流来表示状态。解释如何有效地做到这一点是一项比像这样的简短回答合理的任务更大的任务;准备做一些研究!
关于c - SIGALRM 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25706783/
我正在尝试编写一个基准测试来接收来自内核的信号,告诉它调整其参数。我正在尝试研究主动或被动方法是否最有效。 在主动方法中,我使用 setitimer 定期设置警报并强制基准测试查看其目前的性能并重新调
我有一个我自己似乎无法解决的问题。我正在编写一个小的 python 脚本,我想知道为什么我的 signal.alarm 在它所在的函数返回后仍然有效。这是代码: class AlarmExceptio
achul@achul:~/chennai/linux/SM_practise$ cat doubt.c #include #include main() { int ret; pri
我们正在尝试使用 sigaction 和 setitimer 从我们的三个函数中的 while(1) 循环中获取控制权,我们现在面临的问题是仅生成一次 SIGALRM,而下一次它不会生成,即我们能够从
我是信号的新手,我正在尝试在 UDP 回显服务上设置 SIGALRM,作为套接字编程实践。 所以这里我有一个 UDP 套接字,客户端向服务器发送一个字符串并等待响应(任何响应,这里的字符串由服务器回显
我是一个非常初级的程序员,我正在 Ubuntu 中开发客户端服务器程序。这段代码属于我的服务器文件,我想实现一个信号SIGALRM,但我不知道如何才能正确地做到这一点。我的目标是:在我调用信号 ala
我在做作业。 作业是在 Linux 系统(在 Ubuntu 上)上制作虚拟实时操作系统。 我的问题是 SIGALRM,它是调度程序的处理程序。 void scheduler(int signo) {
我是 Linux 信号的新手。如果一个进程初始化多个定时器,那么进程如何知道哪个 SIGALRM 属于哪个定时器? 最佳答案 这取决于您如何设置 SIGALRM 处理程序。 考虑不使用 alarm 而
这段代码有问题.. 我需要等待 (sigwait) 一个过程,直到到达两个信号 SIGUSR1/SIGUSR2 (也许我可以使用 sigalrm 捕捉信号?) #include #include
我需要在超时后中断系统调用(在本例中为 recvfrom)。为此,我正在使用 alarm(),但是生成的 SIGALRM 会终止我的进程,而不仅仅是中断系统调用。 我已经尝试调用 signal(SIG
我在使用 SIGALRM 时遇到了问题。我用它每 3 秒写一次事件进程的 pids。但是一旦它杀死主进程,它就会触发。我做错了什么?一段时间后,我还使用信号杀死每个子进程,我在那里使用 SIGTERM
因此,我尝试调用警报以每秒显示一条消息“仍在工作......”。我包含了 signal.h。 在我的 main 之外,我有我的功能:(我从不为 int 声明/定义 s) void display_me
我编写了这段代码,我必须使用警报信号 (SIGALRM) 让程序打印消息“我还活着”。每 3 秒一次。 但它不起作用,只有当我按下 CTR-C 时它才会发送消息“我还活着”,我猜我没有把 SIGALR
在处理类的系统调用时,我遇到了以下代码的麻烦。无论出于何种原因,当信号处理程序中的 print 语句末尾有换行符时,它会按预期运行,接收和处理信号并显示消息。但是,当换行符不存在时,根本不会显示任何输
我有一个问题。我需要实现一个使用计时器和 SIGALRM 切换 ucontext 线程的程序,但是当我使用 evict_thread 函数切换线程时出现段错误。我相信这是竞争条件的结果,因为它发生在程
所以基本上我的程序中有 4-5 个函数。这是很多行代码,需要从文件中读取和写入,并且可能会以无限循环结束(更糟糕的情况),如果超过 20 秒,我想终止我的程序。下面的代码不起作用,我的程序挂起,操作系
嗨,我正在尝试了解有关信号的更多信息,我编写了一个简单的代码,该代码应该只打印“再见”发送警报信号的所有内容。我正在使用 sigaction 来设置它。但是,我在错误检查中不断返回 NULL,有人可以
我目前正在使用 select() 作为计时器。我有一个循环内发生的网络接收操作,每隔几秒需要发生一个处理操作。 因此,select() 语句的超时不断变化——随时间减少到零,然后从 3 重新开始。 r
好吧,不太确定这是内存损坏还是只是我的代码中的错误,但这是问题的描述:单线程程序注册一个SIGALRM处理程序: struct sigaction action; sigset_t blocked;
我有一个 C 程序,如果 n 作为参数传递,它会计算前 n 个素数。 $ ./prime 5 2 3 5 7 11 如果我可以给程序的进程SIGALRM,程序的逻辑允许我进入一个想要的函数。 所以基本
我是一名优秀的程序员,十分优秀!