【问题标题】:System V IPC msgrcv with timer HowtoSystem V IPC msgrcv 与计时器 Howto
【发布时间】:2010-12-14 07:25:39
【问题描述】:

我们正在使用 System V 消息队列,其中 msgrcv 函数在阻塞模式下被调用。我们想在阻塞msgrcv函数上实现一个定时器,这样当定时器到期而我们还没有收到消息时,我们可以解除阻塞msgrcv并继续执行。

您对我们如何通过编程实现这一目标有什么建议吗?

【问题讨论】:

标签: linux ipc message-queue sysv


【解决方案1】:

我已经用报警信号解决了这个问题。

如果有帮助,请检查以下程序:

int msg_recv(int id, MSG_DATA *msgptr)
{

    int n;


    **alarm(2);**    //After 2 second msg_recv interrupt and return errno "Interrupted system call"

    n = msgrcv(id, (MSG_DATA *) msgptr, sizeof(MSG_DATA) , 0, 0);

    perror("Return from msgrcv");

    printf ("N = %d\n %d %s\n\n",n,errno,strerror(errno));

    if ( n < 0) //goto LOOP;  // This forces the interrupted msgrcv to repeat
    return(n);
}




void sigalrm_handler()
{
    printf("Alarm signal delivered !\n");

    return;
}




int  main();


int main()
{
   //signal (SIGALRM, times_up);         /* go to the times_up function  */
                                       /* when the alarm goes off.     */
   **signal(SIGALRM, sigalrm_handler);**     

   int msqid;                          /* return value from msgget() */   

   MSG_DATA msg_data;

   msqid = 0;



   printf("Ready to receive ... \n");

   **msg_recv(msqid, &msg_data);**

   printf("read message \n");


   return 0;                               
}

【讨论】:

    【解决方案2】:

    信号处理程序有一个 int 参数:

    void sigalrm_handler(int)
    {
        printf("Alarm signal delivered !\n");
        return;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      相关资源
      最近更新 更多