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参数传递给processExecute按值。这意味着在processExecute函数内部,process变量就像任何其他局部变量一样,变量的生命周期就是函数的生命周期。因此,对结构的任何更改都不会向上传递。

您需要Process使用指针通过引用传递结构:

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

    /* ... */
}

这意味着重新设计您的程序,因为您只Process在更多地方将结构作为值(这意味着它们只是副本)传递。

于 2012-12-19T08:28:43.517 回答
0

您的代码太长,无法通读。时间是否设置在不同的线程上?据我所知,堆栈没有共享,所以我希望您在一个线程上设置的内容在另一个线程上不可见。在堆上分配以避免这种情况。

于 2012-12-19T06:59:18.857 回答