首页 > > 详细

讲解C数据结构设计、C语言调试、讲解C数据结构语言程序、C语言辅导、辅导C编程

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Job
{
int pid; // job PID
int jid; // job ID
int state; // TSTP, CONT, etc
char cmdline[50]; // command line
int arr_time; // arrival time
int dur_time; // duration time
};
struct Job jobl[20];
static int ntime=0;
static int a;
int read(const char *argv)
{
FILE* fp;
char buf[100];
int i=0;//main
int a =0;
// if( (fp = fopen(argv,"r") ) == NULL){

if( (fp = fopen("job.txt", "r") ) == NULL){
printf("Can not open file\n" );
}
else
{
while (fgets(buf,100,fp) != NULL)
{

char *temp = strtok(buf,"\t");
jobl[i].arr_time=atoi(temp);
temp=strtok(NULL, "\t");
strcpy(jobl[i].cmdline,temp);
temp=strtok(NULL, "\t\n");
jobl[i].dur_time=atoi(temp);
i++;
a++;
}
}
return a;
}
void swap(int a)
{
Job jobltest;
int x=0;
for(int i=0;ijobl[x].arr_time)
{
jobltest=jobl[i];
jobl[i]=jobl[x];
jobl[x]=jobltest;
}
}
jobl[i].jid=i+1;
}
}
void show(int a)
{
printf("jobNo.\tarrival time\tcommand\tDuration\n");
for(int i=0;i0)
{

int kil;
waitpid(pid, NULL, 0);
ntime=ntime+jobl[i].dur_time;
if (i==0)
{
ntime=ntime+jobl[0].arr_time;
}
kil=kill(pid,SIGKILL);
}
}
return 0;
}
//
//
//
//
//
//
//RR
void RR(int a,int mypid)
{

int i;
ofstream outfile;
outfile.open("Jobout.txt",ios::out|ios::trunc);
if(!outfile)
{
cerr=0)
{
kill(getpid(), SIGSTOP);

if(jobl[i].dur_time==0)
{
break;
}
if (jobl[i].dur_time==1)
{
printf("%d sec: Job %d run\n",ntime,i+1);
outfile1)
{
printf("%d sec: Job %d run\n",ntime,i+1);
outfile< jobl[i].dur_time--;
jobl[i].dur_time--;
ntime++;
ntime++;
sleep(1);
if(jobl[i].dur_time==0)
{


printf("%d sec: Job %d terminated\n",ntime,i+1);
outfile< break;
}
else
{
printf("%d sec: Job %d suspended\n",ntime,i+1);
outfile< }
}
}

}
//? if(getpid()==mypid)
{
int i =0;
int finish=0;
while(wait(NULL)!=-1){}
int status;
while(finish<6*a)
{

kill(jobl[i].pid,SIGCONT);
waitpid(jobl[i].pid, status, WIFSTOPPED(status));
if (status==1) {
printf("xxxxxx\n");
}
i++;
finish++;
if (i==a) {
i=0;
}
}

}
}
int main(int argc, const char * argv[])
{
a=read(argv[1]);
swap(a);
show(a);
int choose;

printf("ID is %d\n", getpid());
static int ppp=getpid();
printf("FIFO for 1 \nRR for 2\n");
scanf("%d",choose);
if (choose==1)
{
int x=FIFO(a);
if(x!=1)
{
printf("thanks\n");
}
return 0;
}
if(choose==2)
{
RR(a,ppp);
}
return 0;
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!