SIGALRM实施

时间:2014-09-07 03:19:02

标签: c linux ubuntu

我是一名非常初学的程序员,我正在Ubuntu中开发一个客户端服务器程序。此代码属于我的server文件,我想实现一个信号SIGALRM,但我只是不知道我怎么能正确地做到这一点。 我的目标是:在我调用信号alarm(ans.game_time)的代码部分中,我想运行该函数Time,这个函数应该等待ans.game_time秒,并且在这几秒钟内,玩家应加入游戏(最后一部分尚未实施,“播放”命令)。

Estructures:

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);
}

我试图理解它是如何正常工作的,但这个信号对我来说真的很混乱。

2 个答案:

答案 0 :(得分:3)

alarm()不是在服务器中实现定期事件的好方法。它起到异步信号的作用,因此在发生其他事情时很难适当处理。例如,如果您的服务器正在向客户端发送消息,那么让闹钟响起并开始生成输出可能会破坏消息。最重要的是,这不是解决这个问题的正确方法。

大多数客户端/服务器应用程序在其核心使用select()poll()系统调用。这些系统调用允许您的应用程序在任意数量的文件描述符上等待事件发生(例如,数据到达),并具有可选的超时。这就是大多数服务器应用程序一次处理与多个客户端的连接的方式。

使用这些系统调用可能需要您围绕状态机"重新构建应用程序。模型,而不是使用程序流来表示状态。解释如何有效地执行此操作是一项更大的任务,而不是像这样简短的答案是合理的;准备做一些研究!

答案 1 :(得分:2)

当ALARM(ALRM)信号响起时,您很可能正在等待read(fifo_1 ...);来电,但您无法处理此情况。

if (read(fifo_1, ...) == -1) {
    /* some error occurred in the read... */
    if (errno == EINTR) {
        /* error was interruption, most likely by the ALARM */
        /* handle this in whatever way is appropriate */
    }
}

进一步改进,几乎总是,最好的做法是尽可能少地在信号处理程序中做 - 通常只需设置一个表示信号发生的标记。

static volatile sig_atomic_t alarms_happened = 0;
static sig_atomic_t alarms_handled = 0;

static void Time ( int signo ) {
    if (signo == SIGALRM) alarms_happened++;
}

然后在你的主循环中,无论你准备好处理警报:

if (alarms_happened != alarms_handled) {
    alarms_handled = alarms_happened;
    /* do whatever you need to do when there was an alarm */
}
相关问题