在Linux

时间:2016-03-28 16:59:27

标签: c++ linux signals

编写具有以下特征的调度程序Round-Robin: 调度程序必须接受量子qt时间以及要在命令行上执行的过程,格式如下:

./scheduler qt path_process1 startTime1 executionTime1 path_process2 startTime2 executionTime2 ...

创建和调整调度程序的每个进程都应该是一个单独的程序,它按照以下格式每0.5秒打印一条消息:

Program i: Messages printed: 1.
Program i: Messages printed: 2. 
Program i: Messages ...
    ...

当循环启动和终止每个进程本身都会打印消息:

Process pid is executing.
//Execution of process pid
Process pid is suspended.

停止和执行过程将包含信号SIGTSTPSIGCONT。  对于具有以下开始和运行时间的五个过程,考虑并评论系统行为的量子时间的小和大值(例如,qt = 0.5s,1s,2s,4s)。

startTimes = [0,2,4,6,8]
execTimes = [3,6,4,5,2]

鉴于提示:  要将进程子进程作为单独的程序执行,请使用命令execl (...)。  要测量从调度程序运行开始所经过的时间,请使用命令gettimeofday (...)

代码

Scheduler.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h> 
#include <unistd.h>
#include <cstdio>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/signal.h>

using namespace std;

float qt;
int k,j,i;
int total_time=0;
int pass_time=0;

void stp_handler(int signal){
}
void con_handler(int signal){
}

int main(int argc, char *argv[])
{
     int p=2;
     int s=3;
     int e=4;
     int count=0;

     signal(SIGTSTP,stp_handler);
     signal(SIGCONT,con_handler);
     int start_time[(argc-2)/3];
     float execution_time[(argc-2)/3];
     char *path_process[(argc-2)/3];
     float remaining_time[(argc-2)/3];
     timeval time;
     int start,end;


     if (argc < 2)
        {
        cout<<"You didn't enter any arguments" << endl;
        exit(0);
        }
    else{
        qt = atoi(argv[1]);
        while (count < ((argc-2)/3) ){
            path_process[count] = argv[p];
            start_time[count] = atoi(argv[s]);
            execution_time[count] = atoi(argv[e]);
            remaining_time[count] = execution_time[count];
            total_time = total_time + execution_time[count];
            cout << path_process[count] << " " << start_time[count] << " " << execution_time[count] << endl;
            p+=3;
            s+=3;
            e+=3;
            count++;
        }
    }
        i=0;
        pid_t pid[count];

    while(i<count){
         pid_t par,child;
         child=fork();

         if (child==0){
            par = getppid();
            cout << "Child pid: " << i << " " << getpid() << endl << "Parent pid: " << par << endl;
            char num[100]={0x0};
            sprintf(num,"%d",i+1);
            char pi[100]={0x0};
            sprintf(pi,"%d",getpid());
            execl(path_process[i],path_process[i],pi,num,(char*)0);
            exit(0);
         }

         else{
            sleep(1);
            pid[i] = child;
            i++;
         }
    }

    i=0;
    cout << endl << "Processes time: " << total_time << endl << endl;
    gettimeofday(&time,NULL);
    start = time.tv_sec;

    while(pass_time<total_time){
        for(j=0;j<count;j++){
            if (pass_time<start_time[j]){
                continue;
            }
            else if (remaining_time[j]<=0){
                continue;
            }
            else if (remaining_time[j]>= qt){
                kill(pid[j],SIGCONT);
                usleep(qt*1000000);
                kill(pid[j],SIGTSTP);
                pass_time = pass_time + qt;
                remaining_time[j]=remaining_time[j]-qt;
                i++;
            }
            else{
                kill(pid[j],SIGCONT);
                usleep(remaining_time[j]*1000000);
                kill(pid[j],SIGTSTP);
                pass_time = pass_time + remaining_time[j];
                remaining_time[j]=0;
                i++;
            }
        }
    }

    sleep(1);
    gettimeofday(&time,NULL);
    end = time.tv_sec;
    cout << "Scheduler time: " << end - start << endl;
    for(i=0;i<count;i++)
        kill(pid[i],SIGKILL);

    return 0;
}

Signals.cpp

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <sys/wait.h>
#include <sys/signal.h>

using namespace std;

int j=1;
char* pid;

void sig_handler(int signal){

    if (signal == SIGCONT){
    cout << endl << "Process " << pid << " is executing." << endl << endl;
    j=0;
    }
    else if (signal == SIGTSTP){
    cout << endl << "Process " << pid << " is suspended." << endl << endl;
    j=1;
    }
}

  main(int argc, char *argv[])
  {

     signal(SIGTSTP,sig_handler);
     signal(SIGCONT,sig_handler);
     pid = argv[1];
     int i=1;

     while(1){
        while(j==0){
            cout << "Program: " << argv[2] << " messages printed: " << i << endl;
            usleep(500000);
            i++;
        }
    }
}

调度程序

的输出

(我猜这是正确的)

process1 0 3
process2 2 6
process2 4 4
process3 6 5
process4 8 2
Child pid: 0 3406
Parent pid: 3405
Child pid: 1 3407
Parent pid: 3405
Child pid: 2 3408
Parent pid: 3405
Child pid: 3 3409
Parent pid: 3405
Child pid: 4 3410
Parent pid: 3405

Processes time: 20

信号输出

我什么都没得到,但正如我上面提到的,我希望它能以某种方式存在。

Program i: Messages printed: 1.
Program i: Messages printed: 2. 
Program i: Messages ...
    ...

当循环启动和终止每个进程本身都会打印消息:

Process pid is executing.
//Execution of process pid
Process pid is suspended.

P.S:为了让调度程序工作,我编译它并使用

运行它
>./scheduler 0.5 process1 0 3 process2 2 6 process2 4 4 process3 6 5 process4 8 2

为什么信号不会导致任何输出?

0 个答案:

没有答案