2017-05-03 6 views
-2

과제를 위해 속도 단조 스케줄러를 만들고 있으며, 모든 스레드를 동일한 프로세서에서 실행해야합니다. 내가 잘못하고있는 것이 확실하지 않지만 같은 오류가 계속 발생합니다 (아래 그림 참조). 나는 시도하고 무엇을해야 할지를 이해하기 위해 많은 리눅스 문서를 조사해 왔지만 나는 할 수 없다. 나는 정말로 약간의 도움에 감사 할 것이다. 미리 감사드립니다.'cpu_set_t'는 C++ 유형의 이름이 아닙니다.

Error 내 Scheduler.cpp 파일 : 나는 빠른 응답 주셔서 감사합니다 :이 게시물에 대답 모든 사람에게

#define _GNU_SOURCE 
#include <iostream> 
#include <pthread.h> 
#include <unistd.h> 
#include <mutex> 
#include <condition_variable> 
#include <semaphore.h> 
#include <sched.h> 
#include <stdio.h> 
#include <assert.h> 
#include <stdlib.h> 

using namespace std; 

sem_t semaphore; 
sem_t mutex1; 
sem_t mutex2; 
sem_t mutex3; 
sem_t mutex4; 

// initialze variables 
int i = 0; 
int overrun1 = 0; 
int overrun2 = 0; 
int overrun3 = 0; 
int overrun4 = 0; 

int doWork(); 
void* p1(void *param); 
void* p2(void *param); 
void* p3(void *param); 
void* p4(void *param); 



int main(int argc, char const *argv[]) 
{ 
cpu_set_t cpus; 

CPU_ZERO(&cpus); 
CPU_SET(1, &cpus); 

sem_init(&mutex1, 0, 0); 
sem_init(&mutex2, 0, 0); 
sem_init(&mutex3, 0, 0); 
sem_init(&mutex4, 0, 0); 

// initialze all threads 
pthread_t thread1; 
pthread_t thread2; 
pthread_t thread3; 
pthread_t thread4; 

// actually create all threads 
pthread_create(&thread1, NULL, p1, NULL); 
pthread_create(&thread2, NULL, p2, NULL); 
pthread_create(&thread3, NULL, p3, NULL); 
pthread_create(&thread4, NULL, p4, NULL); 

while (i < 160) 
{ 
    if (i == 0) // initial case. at time 0 schedule all threads 
    { 
     sem_post(&mutex1); 
     sem_post(&mutex2); 
     sem_post(&mutex3); 
     sem_post(&mutex4); 
    } 

    else if (i % 16 == 0) // last case which happens every 16 units which schedules all threads again 
    { 
     sem_post(&mutex1); 
     sem_post(&mutex2); 
     sem_post(&mutex3); 
     sem_post(&mutex4); 
    } 

    else if (i % 4 == 0) // case that happens every 4 units of time 
    { 
     sem_post(&mutex1); 
     sem_post(&mutex2); 
     sem_post(&mutex3); 
    } 

    else if (i % 2 == 0) // case that happens every other unit of time 
    { 
     sem_post(&mutex1); 
     sem_post(&mutex2); 
    } 

    else if (i % 1 == 0) // case that happens every unit of time 
    { 
     sem_post(&mutex1); 
    } 
    i++; // increment i to go through the loop again 
} 

// join all threads back to the main one 
pthread_join(thread1, NULL); 
pthread_join(thread2, NULL); 
pthread_join(thread3, NULL); 
pthread_join(thread4, NULL); 

return 0; 
} 

// doWork function 

int doWork() 
{ 
int lousyArray[10][10]; 
int product = 1; 

for (int i = 0; i < 10; i++) 
{ 
    for (int j = 0; j < 10; j++) 
    { 
     lousyArray[i][j] = 1; 
    } 
} 
for (int k = 0; k < 1; k++) 
{ 
    for (int j = 0; j < 10; j++) 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      product *= lousyArray[i][j]; 
     } 
    } 
} 
return 1; 
} 


void* p1(void *param) 
{ 
    bool thread1FinishFlag = false; 
    while(1) 
    { 
     sem_wait(&mutex1); 
     thread1FinishFlag = false; 
     for (int i = 0; i < 1; i++) 
     { 
      doWork(); 
     } 
     //increment counter here 
     thread1FinishFlag = true; 
    } 
} 
void* p2(void *param) 
{ 
    bool thread2FinishFlag = false; 
    while(1) 
    { 
     sem_wait(&mutex2); 
     thread2FinishFlag = false; 
     for (int i = 0; i < 2; i++) 
     { 
      doWork(); 
     } 
     //increment counter here 
     thread2FinishFlag = true; 
    } 
} 
void* p3(void *param) 
{ 
    bool thread3FinishFlag = false; 
    while(1) 
    { 
     sem_wait(&mutex3); 
     thread3FinishFlag = false; 
     for (int i = 0; i < 4; i++) 
     { 
      doWork(); 
     } 
     //increment counter here 
     thread3FinishFlag = true; 
    } 
} 
void* p4(void *param) 
{ 
    bool thread4FinishFlag = false; 
    while(1) 
    { 
     sem_wait(&mutex4); 
     thread4FinishFlag = false; 
     for (int i = 0; i < 16; i++) 
     { 
      doWork(); 
     } 
     //increment counter here 
     thread4FinishFlag = true; 
    } 
} 

void nsleep() 
{ 
    struct timespec delay; 

    delay.tv_sec = 0; 
    delay.tv_sec = 100000000L; 
    nanosleep(&delay, NULL); 
} 
+1

[mcve]를 제공하기 위해 질문을 편집하십시오. –

+0

'cpu_set_t'와'CPU_ZERO'와'CPU_SET' 매크로를 선언 한'#include '파일이 없습니다. – Barmar

+0

@BaummitAugen 그는 전체 소스 파일을 게시했습니다. MCVE에 더 필요한 것은 무엇입니까? – Barmar

답변

0

. 무엇보다 먼저이 프로그램을 컴파일하기 위해 Cygwin을 Windows 용으로 사용하고 있다고 언급 했어야합니다. 둘째, 게시물을 더 짧게 만들지 않아서 사과드립니다. 마지막으로, Cygwin 자체가 코드 자체 내에서 프로세서 친 화성 설정을 지원하지 않는다는 사실을 동료에게 묻자 알아 냈습니다.

고맙습니다.