티스토리 뷰

//단일 큐 구현 -> 작업 생성-> 스케줄링 생성 -> CPU 할당??

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

typedef struct _queue{
    char QUE[100];
    char front;
    char rear;
}Queue;

void QInit(Queue * pq){
    pq->front = 0;
    pq->rear = 0;

}

int QIsEmpty(Queue * pq){
    if(pq->front == pq->rear)
        return 1;
    else   
        return 0;
}

int NextPosIdx(int pos){
    if(pos == 99)
        return 0;
    else
        return pos+1;
}

void Enqueue(Queue * pq,char data){
   if(NextPosIdx(pq->rear) == pq->front)
        exit(0);

    pq->rear = NextPosIdx(pq->rear);
    pq->QUE[pq->rear] = data;
    printf("%c is enqueued.\n",pq->QUE[pq->rear]);
}


char Dequeue(Queue * pq){
    pq->front=NextPosIdx(pq->front);
    return pq->QUE[pq->front];

}

void CPU(char ch){
    printf("%c is finished.\n",ch);
}

int main(){
    Queue Oneque;
    QInit(&Oneque);

    Enqueue(&Oneque,'A');
    Enqueue(&Oneque,'B');
    Enqueue(&Oneque,'C');
    Enqueue(&Oneque,'D');
    Enqueue(&Oneque,'E');
    printf("\n");

    char CPU0[10];
    char CPU1[10];
    char CPU2[10];
    char CPU3[10];

    int i=0;
    
    while(!QIsEmpty(&Oneque)){
        if(i%4 == 0){
            CPU0[i] = Dequeue(&Oneque);
            CPU(CPU0[i++]);
        }
        else if(i%4 == 1){
            CPU1[i] = Dequeue(&Oneque);
            CPU(CPU1[i++]);
        }
        else if(i%4 == 2){
            CPU2[i] = Dequeue(&Oneque);
            CPU(CPU2[i++]);
        }
        else if(i%4 == 3){
            CPU3[i] = Dequeue(&Oneque);
            CPU(CPU3[i++]);
        }
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함