Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 尽快检测变量变化_C_Pthreads_Mutex_Thread Synchronization - Fatal编程技术网

C 尽快检测变量变化

C 尽快检测变量变化,c,pthreads,mutex,thread-synchronization,C,Pthreads,Mutex,Thread Synchronization,首先,这与家庭作业有关。只要一点提示就足够了 我要做的是检测变量(信号)何时发生变化,并在1微秒或更短的时间内宣布它。我目前的进展: int main(int argc, char **argv) { int i; N = atoi(argv[1]); if (argc != 2) { printf("Usage: %s N\n" " where\n" " N : number of signals to monitor\

首先,这与家庭作业有关。只要一点提示就足够了

我要做的是检测变量(信号)何时发生变化,并在1微秒或更短的时间内宣布它。我目前的进展:

int main(int argc, char **argv)
{
  int i;

  N = atoi(argv[1]);

  if (argc != 2) {
    printf("Usage: %s N\n"
           " where\n"
           " N    : number of signals to monitor\n"
       , argv[0]);

    return (1);
  }

  // set a timed signal to terminate the program
  signal(SIGALRM, exitfunc);
  alarm(20); // after 20 sec

  // Allocate signal, time-stamp arrays and thread handles
  signalArray = (int *) malloc(N*sizeof(int));
  timeStamp = (struct timeval *) malloc(N*sizeof(struct timeval));

  pthread_t sigGen;
  pthread_t *sigDet = (pthread_t*) malloc(N * sizeof(pthread_t));

  long *signalid = (long*) malloc(N * sizeof(long));

  for (i=0; i<N; i++) {
    signalArray[i] = 0;
  }

  for (i = 0; i < N; i++)
  {
      signalid[i] = (long) i;
      pthread_create (&sigDet[i], NULL, ChangeDetector, (void*) signalid[i]);
  }
  pthread_create (&sigGen, NULL, SensorSignalReader, NULL);

  // wait here until the signal
  for (i = 0; i < N; i++)
  {
        pthread_join (sigDet[i], NULL);
  }
  return 0;
} 

void *SensorSignalReader (void *arg)
{

  char buffer[30];
  struct timeval tv;
  time_t curtime;

  srand(time(NULL));

  while (1) {
    int t = rand() % 10 + 1; // wait up to 1 sec in 10ths
    usleep(t*100000);

    int r = rand() % N;
    signalArray[r] ^= 1;

    if (signalArray[r]) {
      gettimeofday(&tv, NULL);
      timeStamp[r] = tv;
      curtime = tv.tv_sec;
      strftime(buffer,30,"%d-%m-%Y  %T.",localtime(&curtime));
      printf("Changed %5d at Time %s%ld\n",r,buffer,tv.tv_usec);
    }
  }
}

void *ChangeDetector (void *arg)
{
  char buffer[30];
  struct timeval tv;
  time_t curtime;
  long n = (long) arg;

  while (1) {

    while (signalArray[n] == 0) {}

    pthread_mutex_lock(&mutex);
    gettimeofday(&tv, NULL);
    curtime = tv.tv_sec;
    strftime(buffer,30,"%d-%m-%Y  %T.",localtime(&curtime));
    printf("Detcted %5ld at Time %s%ld after %ld.%06ld sec\n", n, buffer,tv.tv_usec,
       tv.tv_sec - timeStamp[n].tv_sec,
       tv.tv_usec - timeStamp[n].tv_usec);

    pthread_mutex_unlock(&mutex);
    while (signalArray[n] == 1) {}

  }
}
int main(int argc,char**argv)
{
int i;
N=atoi(argv[1]);
如果(argc!=2){
printf(“用法:%s\N”
“其中\n”
“N:要监视的信号数\N”
,argv[0]);
申报表(1);
}
//设置定时信号以终止程序
信号(SIGALRM,exitfunc);
报警(20);//20秒后
//分配信号、时间戳数组和线程句柄
signalArray=(int*)malloc(N*sizeof(int));
时间戳=(struct timeval*)malloc(N*sizeof(struct timeval));
pthread_t sigGen;
pthread_t*sigDet=(pthread_t*)malloc(N*sizeof(pthread_t));
long*signalid=(long*)malloc(N*sizeof(long));
对于(i=0;i您的
ChangeDetector()
依赖于忙等待标志。问题是,您不能有比CPU更多的进程同时忙等待-只有
ChangeDetector()
进程的一部分在任何时间点实际运行

这意味着,在运行并注意到标志已更改之前,通常必须等待将正确的
ChangeDetector
线程调度回CPU

如果希望每个标志都有一个
ChangeDetector
线程,则需要使用非忙等待方法,如pthread条件变量(每个标志可以有一个互斥体/条件变量对)。不过,我不确定是否能够通过这种方式获得亚微秒的延迟

如果您真的想继续使用busy waiting方法,您需要将
ChangeDetector
线程的数量限制在CPU数量以下,方法是让每个线程负责检查每个循环中的多个数组位置。

在锁定互斥锁之前获得一天中的时间将是一个明显的改进,但是对
signalArray
的访问一开始就非常不同步。