sem_wait似乎在随机迭代次数后阻塞

时间:2014-02-07 23:39:14

标签: c semaphore

我正在尝试让我的非周期性任务等待特定的按键事件。我 非周期函数等待事件发生的第一次,但发布它运行3次迭代而不等待,然后再次等待。我无法辨别代码中的错误。我已经为每个

初始化了事件信号量数组,其值为0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>

sem_t mutex[10],event[10];
sem_t mtx,mtx1,mtx2,mtx3; //These 4 mutexes and the array mutex[10] are defined for periodic task

typedef struct task
{
  int taskid;
  int priority;
  int iter;
}task;


void *wait_for_event()
{
int fd;
fd = open("/dev/input/by-path/platform-i8042-serio-0-event-kbd", O_RDONLY);
struct input_event ev;

while (1)
{
     read(fd, &ev, sizeof(struct input_event));
     if(ev.type == 1) /* signal any waiting thread for keypress event */
     sem_post(&event[(ev.code -1)%10]);
} 
}

void *aperiodic(void *t)
{
    pthread_t tid;  
    sem_wait(&mtx);

      while(1){ 
       sem_wait(&mtx1);
       int i,j;
       struct task *temp=(struct task *)t;      
       printf(" The running task is : %d",temp->taskid);    
 /*do some busy computation */
           for(i=0;i<temp->iter;i++)
           {
           j=i+j;
           }
           printf("\nTOTAL iterations %d\n", j);

       printf("\n ENTERED APERIODIC TASK..Enter the input event id\n");
/*wait for event with the task id temp->taskid to occur. This sem_wait waits for   keypress event. But it is not waiting in every iteration of while loop. It is waiting once in 3 iterations.  */

     sem_wait(&event[temp->taskid]);
     sem_post(&mtx1); 
}

in main method i have initialized
for(i=1;i<=10;i++)
{ /*the initial value of all mutexes in event array is 0 */
sem_init(&event[i],0,0);
}

0 个答案:

没有答案