C - FIFO,RR和优先级,Mac和Linux上的线程

时间:2017-10-19 03:01:27

标签: c linux multithreading macos

我正在创建一个程序来执行读取excel文件的任务并相应地为我提供输出。这个确切的代码在使用gcc -c main.c编译时可以在Mac上运行,但在Linux上运行时,它不会创建线程或执行任何任务:

  else if(strcmp(argv[1],"multi")==0) {
    if(argv[2]==NULL) {
      printf("Multi-thread mode with no parameters\n");
      pthread_create(&tid[0], &attr, task1, &(thread_ids[0]));
      pthread_create(&tid[1], &attr, task2, &(thread_ids[1]));
      pthread_create(&tid[2], &attr, task3, &(thread_ids[2]));
    } else if(strcmp(argv[2],"sched")==0) {
      if(argv[3]==NULL) {
           fprintf(stderr, "No scheduling parameter given, options are FIFO or RR\n");
      } else if(strcmp(argv[3],"FIFO")==0){ /* First In First Out */
           printf("Scheduling FIFO\n");
           pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
      } else if(strcmp(argv[3], "RR")==0){ /* Round Robin */
         printf("Do RR algorithm\n");
         pthread_attr_setschedpolicy(&attr, SCHED_RR);
         schedparam.sched_priority = 10;
             pthread_attr_setschedparam(&attr, &schedparam);
             pthread_create(&tid[0], &attr, task1, NULL);
             pthread_create(&tid[1], &attr, task2, NULL);
         pthread_create(&tid[2], &attr, task3, NULL);
         pthread_join(tid[0], NULL);
         pthread_join(tid[1], NULL);
         pthread_join(tid[2], NULL);
      }
      else{
           fprintf(stderr, "Scheduling parameter invalid, only RR and FIFO accepted\n");
      }
    } else if(strcmp(argv[2], "priority")==0){
      if(argv[3]==NULL){
        fprintf(stderr, "No task parameter given for priority\n");
      } else{

          if(atoi(argv[3])==1){
              if(strcmp(argv[4], "high")==0){
                  schedparam.sched_priority = 2;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[0], &attr, task1, &(thread_ids[0]));
              }
              else if(strcmp(argv[4], "low")==0){
                  schedparam.sched_priority = 75;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[0], &attr, task1, &(thread_ids[0]));
              }
              schedparam.sched_priority = 20;
              pthread_attr_setschedparam(&attr, &schedparam);
              pthread_create(&tid[1], &attr, task2, &(thread_ids[1]));
              pthread_create(&tid[2], &attr, task3, &(thread_ids[2]));
          }
          else if(atoi(argv[3])==2){
              if(strcmp(argv[4], "high")==0){
                  schedparam.sched_priority = 2;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[1], &attr, task2, &(thread_ids[1]));
              }
              else if(strcmp(argv[4], "low")==0){
                  schedparam.sched_priority = 75;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[1], &attr, task2, &(thread_ids[1]));
              }
              schedparam.sched_priority = 20;
              pthread_attr_setschedparam(&attr, &schedparam);
              pthread_create(&tid[0], &attr, task1, &(thread_ids[0]));
              pthread_create(&tid[2], &attr, task3, &(thread_ids[2]));
          }
          else if(atoi(argv[3])==3){
              if(strcmp(argv[4], "high")==0){
                  schedparam.sched_priority = 2;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[2], &attr, task3, &(thread_ids[2]));
              }
              else if(strcmp(argv[4], "low")==0){
                  schedparam.sched_priority = 75;
                  pthread_attr_setschedparam(&attr, &schedparam);
                  pthread_create(&tid[2], &attr, task3, &(thread_ids[2]));
              }
              schedparam.sched_priority = 20;
              pthread_attr_setschedparam(&attr, &schedparam);
              pthread_create(&tid[0], &attr, task1, &(thread_ids[0]));
              pthread_create(&tid[1], &attr, task2, &(thread_ids[1]));
          }
      }

  }

有人可以帮我解释为什么这不能在Mac和Linux之间转移吗?如果用户输入./project multi sched FIFO/RR,则应根据自己的选择进行安排。如上所述,这适用于Mac而不是Linux,非常困惑。我也注意到我的优先级也没有改变/工作。我对此问题感到非常难过。我感谢所有的帮助。

0 个答案:

没有答案