【问题标题】:Real time task (periodic task)实时任务(周期性任务)
【发布时间】:2019-05-25 19:02:43
【问题描述】:

大家晚上好,

我还在学习实时编程,我正在尝试使用信号量同步两个线程,第一个线程计算总和并返回一个值(总和)。总和将作为参数传递给第二个线程,该线程将使用它来计算平均值(这只是操纵信号量的示例)。我现在的问题是这两个任务不是周期性的,因为一旦线程返回结果,它就会离开循环,而 main() 完成工作!!!现在怎么做任务期??谢谢你的帮助,这是我的源代码。

  #include <stdio.h> 
  #include <stdlib.h> 
  #include <pthread.h> 
  #include <semaphore.h>

   sem_t evt; 

  //first task
  void *tache1(void *arg)
  {

    int j;
    int s=0;

    struct timespec time;
    clock_gettime(CLOCK_REALTIME, &time);

    while(1){ 

        printf("first THREADDDDDDDDD \n");

        for(j=0; j<100; j++)
            s= s + j;


        return (void*) s;
        sem_post(&evt);

        sleep(3);

     } 
  }

  //second task

  void *tache2(void *arg){

     int moyenne = 1;
     int sum = *((int*) arg);

     struct timespec time;
     clock_gettime(CLOCK_REALTIME, &time);

     while(3){ 

        sem_wait(&evt);

        printf("second THREADDDDDDDDD \n");
        moyenne= sum/10;

        return(void*)moyenne;

        sleep(2);   

     }  
  }

  int main() 
  { 
         pthread_t th1, th2; 
         int sum;
         int moyenne;

         int status;
         sem_init(&evt, 0,0); 
         printf("start main\n") ; 

         pthread_create(&th1, NULL, tache1, NULL); 
         pthread_join(th1, (void*) &sum); 


          pthread_create(&th2, NULL, tache2, &sum); 
          pthread_join(th2, (void*) &moyenne); 

          printf("the sum in the main is %d.\n", sum);
          printf("AVG %d.\n", moyenne);
          printf("End main\n") ;

          return 0; 
     }

【问题讨论】:

  • 现在仔细查看代码,因为它已格式化。主要问题应该很明显。
  • 另外,pthread_create 后面紧跟pthread_join 表示您不是真正多线程,因为您启动线程然后等待它完成再继续。
  • 是的,但是当我在“pthread_create”之后创建“pthread_join”时,执行将被阻止!!!
  • 你现在写东西的方式,th1 必须在你开始 th2 之前完成。这不是多线程。
  • 还要注意return 语句会导致当前函数立即返回。它不只是继续。

标签: c pthreads real-time semaphore


【解决方案1】:

当你这样做时:

pthread_create(&th1, NULL, tache1, NULL); 
pthread_join(th1, (void*) &sum); 

pthread_create(&th2, NULL, tache2, &sum); 
pthread_join(th2, (void*) &moyenne); 

你启动一个线程,等待它完成,然后启动另一个线程,然后等待它完成。那不是多线程。您希望两个线程同时处于活动状态。

此外,您的两个线程函数都包含一个return 语句,后面还有更多语句。一旦return 语句被命中,整个函数立即返回。在它们之后不执行任何语句。另请注意,这些函数中的任何一个都不需要while (1)(或while (3))循环。

信号量(和互斥体)背后的想法是多个线程同时运行,并且两个线程都需要对全局数据进行操作,因此一个线程需要等待另一个线程执行某些操作才能访问全局数据。

所以创建summoyenne 全局变量,同时启动两个线程,并去掉线程函数中的额外循环。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

sem_t evt;
int sum;
int moyenne;

void *tache1(void *arg)
{
    int j;
    int s=0;

    printf("first THREADDDDDDDDD \n");

    for(j=0; j<100; j++)
        s= s + j;

    sum = s;
    sem_post(&evt);

    return NULL;
}


void *tache2(void *arg)
{
    sem_wait(&evt);

    printf("second THREADDDDDDDDD \n");
    moyenne= sum/10;

    return NULL;
}

int main()
{
    pthread_t th1, th2;

    sem_init(&evt, 0,0);
    printf("start main\n") ;

    pthread_create(&th1, NULL, tache1, NULL);
    pthread_create(&th2, NULL, tache2, NULL);

    pthread_join(th1, NULL);
    pthread_join(th2, NULL);

    printf("the sum in the main is %d.\n", sum);
    printf("AVG %d.\n", moyenne);
    printf("End main\n") ;

    return 0;
}

【讨论】:

  • 非常感谢您的回答.. 我现在的问题是为什么要删除 while ??根据我对实时系统的了解,任务必须是周期性的,所以在我的代码中是周期性的......你能向我解释一下这一点吗!!
  • 如何使用两个实时任务执行处理(求和和平均)??
  • @ikram 你的代码只计算总和和平均一次,所以线程只需要运行一次,所以不需要while循环。
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多