带计时器的 System V IPC msgrcv Howto

发布于 2024-10-07 11:10:16 字数 187 浏览 3 评论 0原文

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

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

We are using a System V message queue with the msgrcv function being called in blocking mode. We want to implement a timer on the blocking msgrcv function so that when the timer expires and we have not received a message, we can unblock msgrcv and continue execution.

Do you have any suggestions on how we can achive this by programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-10-14 11:10:16

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

请检查以下程序是否有帮助:

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

I have solved this problem using alarm signal.

Please check the following program if it helps:

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;                               
}
我爱人 2024-10-14 11:10:16

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

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

signal handler has a int param:

void sigalrm_handler(int)
{
    printf("Alarm signal delivered !\n");
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文