#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct node /*创建PCB*/
{ char name[10] /*进程标识*/
int prio /*进程优先数*/
int cputime/*进程占用CPU时间*/
int needtime/*进程完成所需时间*/
int count /*计数器*/
char state/*进程的状态*/
struct node *next/*链指针*/
}PCB
PCB *finish,*ready,*tail,*run
int N
firstin() /*创建就绪队列对头指针*/
{
run=ready
run->state='R'
ready=ready->next
}
void prt(char algo) /*演示进程调度*/
{
PCB *p
printf(" NAME CPUTIME NEEDTIME PRIORITY STATUS\n")
if(run!=NULL)
printf(" %-10s%-10d%-10d%-10d %c\n",run->name,
run->cputime,run->needtime,run->prio,run->state)
p=ready
while(p!=NULL)
{ printf(" %-10s%-10d%-10d%-10d %c\n",p->name,
p->cputime,p->needtime,p->prio,p->state)
p=p->next
}
p=finish
while(p!=NULL)
{ printf(" %-10s%-10d%-10d%-10d %c\n",p->name,
p->cputime,p->needtime,p->prio,p->state)
p=p->next
}
getch()
}
insert(PCB *q)
{
PCB *p1,*s,*r
int b
s=q
p1=ready
r=p1
b=1
while((p1!=NULL)&&b)
if(p1->prio>=s->prio)
{
r=p1
p1=p1->next
}
else
b=0
if(r!=p1)
{
r->next=s
s->next=p1
}
else
{
s->next=p1
ready=s
}
}
void create(char alg) /*创建各个进程*/
{
PCB *p
int i,time
char na[10]
ready=NULL
finish=NULL
run=NULL
for(i=1i<=Ni++)
{
p=malloc(sizeof(PCB))
printf("Enter NAME of process:\n")
scanf("%s",na)
printf("Enter TIME of process(less than 50):\n")
scanf("%d",&time)
strcpy(p->name,na)
p->cputime=0
p->needtime=time
p->state='w'
p->prio=50-time /*假设优先级与耗时之和为50*/
if(ready!=NULL)
insert(p)
else
{
p->next=ready
ready=p
}
}
clrscr()
printf(" DISPLAY OF THE PROGRESS:\n")
printf("************************************************\n")
prt(alg)
run=ready
ready=ready->next
run->state='R'
}
priority(char alg) /*优先级算法调度*/
{
while(run!=NULL&&run->prio>=0)
{
run->cputime=run->cputime+1
run->needtime=run->needtime-1
run->prio=run->prio-3
if(run->needtime==0)
{
run->next=finish
finish=run
run->state='F'
run=NULL
if(ready!=NULL)
firstin()
}
else
if((ready!=NULL)&&(run->prio<ready->prio))
{
run->state='W'
insert(run)
firstin()
}
prt(alg)
}
}
main()
{ char algo
clrscr()
loop:printf("Enter THE TOTAL NUMBER of PCB(less than 10 is better):\n")
scanf("%d",&N)
if(N>10)
{printf("it's too big,and select a small number.\n")
goto loop}
create(algo)
priority(algo)
}
holp you like!
写了一个主要函数,你可以在这两个线程的入口函数分别创建一个新线程或者使用定时器。然后使用Daemon()这个函数。Daemon这个函数用来监视同一个文件夹中的2个进程,如果要使用不同文件夹中的两个进程,把注释部分去掉就行了。#include <iostream>
#include <afxwin.h>
#include <TlHelp32.h>
int Daemon(CString AppName)
{
//如果需要守护的进程名为空
if(AppName.IsEmpty())
{
return 1
}
CString strName = AppName
//如果使用绝对路径,请去掉下面的注释
//int npos = strName.ReverseFind('\\')
//if(npos <= 0)
//{
// return 1
//}
//strName = AppName.Right(AppName.GetLength() - npos - 1)
//定义ProcessEntry32结构
PROCESSENTRY32 pe32
pe32.dwSize = sizeof(pe32)
// 给系统内的所有进程拍一个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf(" 无法得到进程快照 \n")
return -1
}
//需要启动进程的标志位,当进程关闭时,该标志位为TRUE
BOOL bNeedStart = TRUE
// 遍历进程快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap, &pe32)
while(bMore)
{
//如果该进程名在快照中存在
if(strcmp(pe32.szExeFile, strName) == 0)
{
//启动标志位置FALSE
bNeedStart = FALSE
}
bMore = ::Process32Next(hProcessSnap, &pe32)
}
::CloseHandle(hProcessSnap)
//如果标志位为True,即该进程没有在快照中
if(bNeedStart)
{
::Sleep(1000)
STARTUPINFO si = { sizeof(si) }
PROCESS_INFORMATION pi
si.dwFlags = STARTF_USESHOWWINDOW
si.wShowWindow = TRUE
//启动该进程
BOOL bRet = ::CreateProcess (NULL, AppName.GetBuffer(AppName.GetLength()), NULL,NULL,FALSE, CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)
if(bRet)
{
::CloseHandle (pi.hThread)
::CloseHandle (pi.hProcess)
}
}
return 0
}
strName = str.Right(str.GetLength() - nn - 1)
//定义ProcessEntry32结构
PROCESSENTRY32 pe32
pe32.dwSize = sizeof(pe32)
// 给系统内的所有进程拍一个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf(" 无法得到进程快照 \n")
return -1
}
//需要启动进程的标志位,当进程关闭时,该标志位为TRUE
BOOL bNeedStart = TRUE
// 遍历进程快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap, &pe32)
while(bMore)
{
//如果该进程名在快照中存在
if(strcmp(pe32.szExeFile, strName) == 0)
{
//启动标志位置FALSE
bNeedStart = FALSE
}
bMore = ::Process32Next(hProcessSnap, &pe32)
}
::CloseHandle(hProcessSnap)
//如果标志位为True,即该进程没有在快照中
if(bNeedStart)
{
::Sleep(1000)
STARTUPINFO si = { sizeof(si) }
PROCESS_INFORMATION pi
si.dwFlags = STARTF_USESHOWWINDOW
si.wShowWindow = TRUE
//启动该进程
BOOL bRet = ::CreateProcess (NULL, AppName.GetBuffer(AppName.GetLength()), NULL,NULL,FALSE, CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi)
if(bRet)
{
::CloseHandle (pi.hThread)
::CloseHandle (pi.hProcess)
}
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)