2

FIFO アルゴリズムに従うプロセス スケジューラの構築に取り組んでいます。レポートの一部を除いて、すべてのコードを作成しましたが、動作しています。レポートがどのように出力されるかの例を次に示します。

FIFO PROCESS SCHEDULER
Simulation # Report:  
- Schedule Simulation Algorithm: FIFO  
- Number of process: 3  
- Start Time: 19/12/2012 02:54:09 ***<-HERE WORKS!!!!***  
- Duration: 40.0 seconds  
- Simulation Outflow: 0.075000 process / second  
- Total Block Time: 10 seconds  
Processes # Report:  
- Process Number: 0  
- Start Time: 15:51:44 4452907-04-04  ***<- HERE DON'T!!!!***  
- Duration: -140458013347901.0 seconds  
- End Time: 12/31/1969 21:00:03  
- Blocked Time: 10 seconds  
- Executed Time: 1 seconds  
Processes # Report:  
- Process Number: 1  
- Start Time: 14/03/1970 19:00:32  ***<- AND SO ON...***  
- Duration: 140458002575076.0 seconds  
- End Time: 21:25:08 4452907-02-11  
- Blocked Time: 0 seconds  
- Executed Time: 13 seconds  
Processes # Report:  
- Process Number: 2  
- Start Time: 14/03/1970 19:00:32  
- Duration: 140458002575076.0 seconds  
- End Time: 21:25:08 4452907-02-11  
- Blocked Time: 0 seconds  
- Executed Time: 16 seconds 

プロセスからの時間が奇妙な値を持ち、シミュレーターの時間がそうでないのはなぜですか?
前もって感謝します。

ここにCコード


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

#define PROCESS_NUM_MIN         1
#define PROCESS_NUM_MAX         3
#define PROCESS_TIME_EXEC_MIN   3
#define PROCESS_TIME_EXEC_MAX   20
#define PROCESS_BLOCK_TIME_MIN  2
#define PROCESS_BLOCK_TIME_MAX  10

typedef struct process {
    time_t  startTime;
    time_t  endTime;
    double  durationTime;
    int     totalTime;
    int     execTime;
    int     gonaBlock;
    int     blockTime;

} Process;

typedef struct simulatorTimeInfo {
    time_t  startTime;
    time_t  endTime;
    int     mediumReturnTime;
    int     mediumAnswerTime;
    int     blockTime;
    int     simulationDuration;
} SimulatorTimeInfo;

void    initProcessScheduler();
void    *schedulerExecute();
Process getBufferedProcess(int position);
int     compareBufferCount(int position);
void    processExecute(Process process);
void    showReport();
void    showSimulationReport();
void    showProcessesReport();

Process createProcess(int count);
int     determineProcessBlock(int count);
void    allocateProcess(Process process, int position);
void    *processFactoryExecute();

int     ramdomNumber(int max, int min);
int     rdtsc();

time_t  getSystemTimeNow();
void    printSystemTime(time_t t);      

sem_t   bufferMutex;
sem_t   bufferCount;

pthread_t   processFactoryThread;
pthread_t   schedulerThread;

Process             processBufferVector[PROCESS_NUM_MAX];
SimulatorTimeInfo   simulatorTimeInfo;

int     numberOfProcess;
int     numberOfProcessToBlock;

int main(int argc, char **argv) {
    //get simulation start time         
    simulatorTimeInfo.startTime = getSystemTimeNow();

    printf("FIFO PROCESS SCHEDULER - Matheus Arleson\n");

    //starting semaphores
    sem_init(&bufferMutex, 0, 1);
    sem_init(&bufferCount, 0, 0);

    //starting scheduler
    initProcessScheduler();

    pthread_create(&processFactoryThread, NULL, processFactoryExecute, NULL);
    pthread_create(&schedulerThread, NULL, schedulerExecute, NULL);
    pthread_join(schedulerThread, NULL);

    //DEBUG ONLY!!!
    //pthread_join(processFactoryThread, NULL);
}

void initProcessScheduler(){
    //printf("-INITIALIZING SCHEDULER\n");
    //determine the number of processes
    numberOfProcess = ramdomNumber(PROCESS_NUM_MAX, PROCESS_NUM_MIN);
    //printf("--NUMBER OF PROCESS TO WORK: %d\n", numberOfProcess);
    //determine number of processes to block
    numberOfProcessToBlock = (numberOfProcess / 2);
    //printf("--NUMBER OF PROCESS TO BLOCK: %d\n", numberOfProcessToBlock);
    //printf("-SCHEDULER INITIALIZED\n");
}

/*
 * Scheduler Methods
 */
void *schedulerExecute(){
    //printf("-THREAD SCHEDULER RUNNING\n");
    int countScheduler = 0;

    for (countScheduler = 0; countScheduler < numberOfProcess; ++countScheduler) {

        //printf("--TRYING TO GET PROCESS NUMBER %d FROM BUFFER\n", countScheduler);
        while(compareBufferCount(countScheduler)){
            //printf("---STUCK\n");
        }
        Process p = getBufferedProcess(countScheduler);
        //printf("--PROCESS NUMBER %d GET\n", countScheduler);
        //printf("---EXECUTING PROCESS NUMBER %d\n",countScheduler);
        processExecute(p);
        //printf("---PROCESS NUMBER %d EXECUTED\n", countScheduler);
    }

    //get simulation end time           
    simulatorTimeInfo.endTime = getSystemTimeNow();

    showReport();
    //printf("%d PROCESSES SCHEDULED. FINISHING SCHEDULER THREAD EXECUTION\n", countScheduler);
    pthread_exit(NULL);
}

Process getBufferedProcess(int position){           
    sem_wait(&bufferMutex);
        Process p = processBufferVector[position];
    sem_post(&bufferMutex);
    return p;
}

int compareBufferCount(int position){
    int positionNow;
    sem_getvalue(&bufferCount, &positionNow);
    if(positionNow <= position){
        return 1;//fique preso
    } else {
        return 0;//se solte
    }
}

void processExecute(Process process){           
    process.startTime = getSystemTimeNow();
    printSystemTime(process.startTime);


    int time;
    int processExecTime = process.execTime;
    //printf("----PROCESS EXECUTION TIME: %d\n", processExecTime);

    if(process.gonaBlock == 1){
        int processBlockTime = process.blockTime;
        //printf("----PROCESS BLOCK TIME: %d\n", processBlockTime);

        while(processExecTime > 0 || processBlockTime > 0){
            if(processExecTime > 0){
                time = ramdomNumber(processExecTime, 1);
                //printf("-----EXECUTING TIME: %d\n", time);
                sleep(time);
                processExecTime = processExecTime - time;
            }
            if(processBlockTime > 0){
                time = ramdomNumber(processBlockTime, 1);
                //printf("-----BLOCKED TIME: %d\n", time);
                sleep(time);
                processBlockTime = processBlockTime - time;
            }
        }
    }else{
        time = processExecTime;
        //printf("-----PROCESS NOT BLOCKED. EXECUTING TIME: %d\n", time);
        sleep(time);
    }


    process.endTime = getSystemTimeNow();
    printSystemTime(process.endTime);

    process.durationTime = difftime(process.endTime, process.startTime);
}

void showReport(){
    showSimulationReport();
    printf("================================\n");
    showProcessesReport();
    printf("================================\n");
}

void showSimulationReport(){
/ * Simulator:
            a. Number of processes, OK -> numberOfProcess;
            b. Execution time in quantity;-OK> simulatorTimeInfo.startTime - simulatorTimeInfo.endTime
            c. flow;
            d. Return Time, in quantity and Average Response Time in quantity;
            f. Block Time in quantity; OK -> simulatorTimeInfo.blockTime
         * /
    double  simulationDuration  =   difftime(simulatorTimeInfo.endTime, simulatorTimeInfo.startTime);
    double  simulationOutFlow   =   numberOfProcess / simulationDuration;

    printf("#Simulation Report:\n");
    printf("- Simulation Schedule Algorithm: FIFO\n");
    printf("- Number of process: %d\n", numberOfProcess);       

    printf("- Start Time: ");
    printSystemTime(simulatorTimeInfo.startTime);

    printf("- Duration: %.1f seconds\n", simulationDuration);
    printf("- Simulation Outflow: %f process/second\n", simulationOutFlow);
    //item d
    //item e
    printf("- Total Block Time: %d seconds\n", simulatorTimeInfo.blockTime);
}

void showProcessesReport(){
/ * Each process:
            a. Start Time, OK -> process.startTime
            b. Duration quantity;
            c. End Time, OK -> process.endTime
            d. flow and Return Time in quantity;
            g. Average Response Time in quantity;
            h. Lockout Time in quantity; OK -> process.blockTime
        * /
    int processesReportCount;
    for (processesReportCount = 0; processesReportCount < numberOfProcess; ++processesReportCount) {
        Process p = processBufferVector[processesReportCount];
        double  processDuration =   p.endTime - p.startTime;

        printf("#Processes Report:\n");
        printf("- Process Number: %d\n", processesReportCount);     

        printf("-- Start Time: ");
        printSystemTime(p.startTime);

        printf("-- Duration: %.1f seconds\n", processDuration);

        printf("-- End Time: ");
        printSystemTime(p.endTime);

        //item d
        //item e
        //item g
        printf("-- Blocked Time: %d seconds\n", p.blockTime);
        printf("-- Executed Time: %d seconds\n", p.execTime);
        printf("----------------------------------------\n");
    }
}
/*
 * Process Factory Methods
 */
void *processFactoryExecute(){
    //printf("#PROCESS FACTORY RUNNING\n");
    int countProcessFactory = 0;

    for (countProcessFactory = 0; countProcessFactory < numberOfProcess; ++countProcessFactory) {
        //printf("--CREATING PROCESS NUM %d.\n", countProcessFactory);
        Process p = createProcess(countProcessFactory);
        //printf("--PROCESS NUM %d CREATED.\n", countProcessFactory);
        //printf("---ALLOCATING PROCESS NUM %d.\n", countProcessFactory);
        allocateProcess(p, countProcessFactory);
        //printf("---PROCESS NUM %d ALLOCATED.\n", countProcessFactory);
    }
    //printf("#%d PROCESSES CREATED. FINISHING FACTORY EXECUTION\n", countProcessFactory);
    pthread_exit(NULL);
}

Process createProcess(int count){
    Process process;

    process.totalTime = 0;
    process.blockTime = 0;
    process.gonaBlock = 0;

    process.totalTime = ramdomNumber(PROCESS_TIME_EXEC_MAX, PROCESS_TIME_EXEC_MIN);

    if (numberOfProcessToBlock > 0) {
        process.gonaBlock = determineProcessBlock(count);
        if (process.gonaBlock == 1) {
            if (process.totalTime < PROCESS_BLOCK_TIME_MAX) {
                process.blockTime = ramdomNumber((process.totalTime-1), PROCESS_BLOCK_TIME_MIN);
            } else {
                process.blockTime = ramdomNumber(PROCESS_BLOCK_TIME_MAX, PROCESS_BLOCK_TIME_MIN);
            }
        }
    }

    process.execTime = process.totalTime - process.blockTime;
    simulatorTimeInfo.blockTime = simulatorTimeInfo.blockTime + process.blockTime;
    return process;
}

int determineProcessBlock(int count){
    int blockF;
    if(count <= numberOfProcessToBlock){
        blockF = 1;
        numberOfProcessToBlock = numberOfProcessToBlock - 1;
    }else{
        blockF = ramdomNumber(1, 0);
        if(blockF == 1){
            numberOfProcessToBlock = numberOfProcessToBlock - 1;
        }
    }
    //printf("#PROCESSO NUM %d STATUS: %d\n", count, blockF);
    return blockF;
}

void allocateProcess(Process process, int position){
    sem_wait(&bufferMutex);
        processBufferVector[position] = process;
        sem_post(&bufferCount);
    sem_post(&bufferMutex);
}

/*
 * Util Methods
 */
int ramdomNumber(int max, int min) {
    int n;
    srand(rdtsc());
    n = ((rand() % (max - min + 1)) + min);
    return n;
}

int rdtsc() {
    __asm__ __volatile__("rdtsc");
}

time_t getSystemTimeNow(){
    return time(NULL);
}

void printSystemTime(time_t t){
    const struct tm *tm = localtime(&t);
    printf ("%04d-%02d-%02d %02d:%02d:%02d\n", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);

}
4

2 に答える 2

2

process問題は、引数をprocessExecuteby 値に渡すことです。これは、processExecute関数内ではprocess変数が他のローカル変数と同様であり、変数の有効期間が関数の有効期間であることを意味します。そのため、構造への変更は上に渡されません。

Processポインターを使用して、参照によって構造体を渡す必要があります。

void processExecute(Process *process){
    process->startTime = getSystemTimeNow();

    /* ... */
}

Processこれは、より多くの場所で構造を値として (つまり、単なるコピーであることを意味する)渡すだけなので、プログラムの再設計を意味します。

于 2012-12-19T08:28:43.517 に答える
0

あなたのコードは長すぎて読むことができません。時間は別のスレッドに設定されていますか?私の知る限り、スタックは共有されていないため、あるスレッドで設定した内容が他のスレッドでは表示されないことが予想されます。これを回避するには、ヒープに割り当てます。

于 2012-12-19T06:59:18.857 に答える