當前位置:首頁 » 編程語言 » c語言進程管理

c語言進程管理

發布時間: 2022-04-27 22:38:11

c語言模擬操作系統進程調度和管理

給,已經編譯運行通過了,簡單寫的:

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

/*********************以下是全局數據結構和變數***********************/
/*PCB 結構*/
struct PCB{
int pname;
int pri;
int runtime;
int waittime;
struct PCB *next;
}pcb[7];

/* 運行指針*/
struct PCB *running;

/*高優先順序就緒隊列頭指針*/
struct PCB *Hready;

/*低優先順序隊列頭指針*/
struct PCB *Lready;

/*等待隊列頭指針*/
struct PCB *wait;

int sig=0;

/**************************以下是函數說明****************************/
/*利用循環實現延遲*/
void delay();

/*模擬進程3-9*/
void proc(struct PCB *running);

/*將node插入到head所指示的隊列的尾部*/
void InsertIntoQueueTail(struct PCB ** head,struct PCB *node);

/*進程調度函數*/
int proc_switch();

/*進程等待函數*/
void proc_wait();

/*進程喚醒函數*/
int proc_wakeup();

/************************以下是函數定義及注釋************************/
/*主函數*/
main()
{
int i;
/*初始化,創建進程3-9,置低優先順序,等待時間為0,
依次插入低優先順序隊列*/
for(i = 0;i < 7;i++){
pcb[i].pname = i+3;
pcb[i].pri = 0;
pcb[i].waittime = 0;
InsertIntoQueueTail(&Lready,&pcb[i]);
}
/*等待隊列和高優先順序隊列為空*/
wait = NULL;
Hready=NULL;

printf("\nThe process_switch begin:\n");
/*模擬進程調度開始*/
for(;;)
{
switch(sig){
case 0:/*無進程等待調度,列印信息並返回*/
if(!proc_switch())
{
printf("No Process to run,press any key to return:\n");
getchar();
}
break;
case 1:proc_wait();
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:proc(running);
break;
default:printf("\nerror!");
exit(-1);
}
}
}

/*功能:延遲一個時間片*/
/*入口參數:無*/
/*出口參數:無*/
void delay()
{
int i,j;
for(i=0;i<20000;i++)
for(j=0;j<10000;j++)
{
}
}

/*功能:進程3-9*/
/*入口參數:運行指針*/
/*出口參數:無*/
void proc(struct PCB * running)
{
int i;
srand( (unsigned)time( NULL ) );
/*顯示當前運行的進程的id*/
printf("\nNow Process %d is running\n",running->pname);
/*當前進程執行running->runtime個時間片*/
for(i=running->runtime;i>0;i--){
/*顯示剩餘的時間片*/
printf("%d time slice(s) left\n",i);
/*延遲*/
delay();
proc_wakeup();
/*產生一個1到1000的隨機數,若該隨機數小餘100,當前進程等待,*/
if((rand()%1000+1)<100){
printf("Process %d begins to wait.\n",running->pname);
sig=1;
return;
}
}
/*顯示時間片耗盡,進程轉為低優先順序就緒狀態*/
printf("Time slices for process %d exhausted.\n",running->pname);
InsertIntoQueueTail(&Hready,running);
sig=0;
return;

}
/*功能:將一個節點插入隊列尾部*/
/*入口參數:隊列頭指針地址head,待插入結點node*/
/*出口參數:無*/
void InsertIntoQueueTail(struct PCB **head,struct PCB *node)
{
struct PCB *p;
node->next=NULL;
/*被插入隊列為空*/
if(*head==NULL){
*head=node;
return;
}
/*被插入隊列不為空*/
else{
p=*head;
/*找到隊列的最後一個結點*/
while(p->next!=NULL) p=p->next;
p->next=node;
}
}

/*功能:進程調度*/
/*入口參數:無*/
/*出口參數:若調度成功,返回1,否則返回0*/

int proc_switch()
{
/*若高優先順序就緒隊列和低優先順序就緒隊列均為空,則循環執行進程喚醒*/
while(Hready == NULL && Lready == NULL)
if(!proc_wakeup()) return 0;

/*若高優先順序就緒隊列非空,則執行其第一個進程,分配2個時間片*/
if(Hready != NULL){
running = Hready;
Hready = Hready -> next;
running->runtime = 2;
}
/*若高優先順序就緒隊列為空,則執行低優先順序就緒隊列的第一個進程,
分配5個時間片*/
else{
running = Lready;
Lready=Lready -> next;
running -> runtime = 5;
}
/*別調度進程的id賦給sig*/
sig = running -> pname;
return 1;
}

/*功能:進程等待。將當前運行進程置高優先順序,等待時間為20,
插入等待隊列尾部*/
/*入口參數:無*/
/*出口參數:無*/
void proc_wait()
{
struct PCB *p;
running->pri=1;
running->waittime=20;
InsertIntoQueueTail(&wait,running);
sig=0;
return;
}

/*功能:進程喚醒*/
/*入口參數:無*/
/*出口參數:若等待隊列為空,則返回0,否則返回1*/
int proc_wakeup()
{
struct PCB *p,*last,*MoveToReady;
p = wait;
/*等待隊列為空,返回0*/
if(p == NULL) return 0;

/*延遲*/
delay();
/*等待隊列中每個進程的等待時間減1*/
while(p != NULL){
p -> waittime -= 1;
p=p->next;
}
p=wait;
/*從等待隊列中摘除等待時間為0的進程,插入到高優先順序就緒隊列的尾部*/
while(p!=NULL){
if(p -> waittime == 0){
MoveToReady = p;
if (p == wait)
wait = p->next;
else
last -> next = p->next;
p = p -> next;
InsertIntoQueueTail(&Hready,MoveToReady);
}
else{

p = p -> next;
}
}
sig =0;
return 1;
}

❷ 應用進程管理器 在c語言里怎麼表示

360tray.exe--360實時保護360Safe.exe--360主程序liveupdate360.exe--360升級加速safeboxTray--360保險箱保護程序360safebox.exe--360保險箱主程序望採納 (*^__^*) 嘻嘻……

❸ 用c語言編寫的進程管理程序

#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
structPCB_type
{charpid[100];//進程名輸入用字元串健壯性比較好
intpriority;
intcputime;
intstate;
};//缺少「};」
intshumu=0,pid_l;
structPCB_typeneicun[20];
structPCB_typehc[10];
intmax=0;
intnumber=0;
voidcreate();
voidrun();
voidhuanchu();
voidkill();
intmain()
{
intn,a;
n=1;
system("color1d");
while(n==1)
{
system("cls");
printf(" **********************************************");
printf(" *進程演示系統*");
printf(" **********************************************");
printf(" 1.創建新的進程2.查看運行進程");
printf(" 3.換出某個進程4.殺死運行進程");//新增了一個喚醒功能
printf(" 5.退出系統");
printf(" **********************************************");
printf(" 請選擇(1~5):");
scanf("%d",&a);
switch(a)
{case1:
create();
printf(" pressanykeytogoon~");
getch();
break;
case2:
run();
printf(" pressanykeytogoon~");
getch();
break;
case3:
huanchu();
printf(" pressanykeytogoon~");
getch();
break;
case4:
kill();
printf(" pressanykeytogoon~");
getch();
break;
case5:
exit(0);
default:
n=1;
break;
}
}
}
voidcreate()
{
if(shumu>=20)
{
printf(" 內存已滿,請先結束或換出進程 ");
}
else
{
shumu++;
printf(" 請輸入新進程的程序名 ");
scanf("%s",neicun[shumu-1].pid);
printf(" 請輸入新進程的優先順序(數字) ");//
scanf("%d",&neicun[shumu-1].priority);
printf(" 請輸入新進程的需要的運行時間 ");//
scanf("%d",&neicun[shumu-1].cputime);
printf(" 創建進程時令其狀態為就緒 ");
neicun[shumu-1].state=2;//1為等待,2就緒,3為運行

}
printf(" 創建進程成功! ");
}
voidrun()
{
if(shumu<=0)//查看//判斷是否存在進程
{
printf("當前狀態無進程,按任意鍵繼續創建進程 ");
return;
}
intmax=0;
for(inti=0;i<shumu;i++)//
if((neicun[i].state==2&&neicun[i].priority>=neicun[max].priority))
{
max=i;//這里判斷優先順序,優先順序高的進程優先執行。
}
if(neicun[max].state==2)
{
neicun[max].state=3;//進程運行,狀態為3
system("color5F");
printf("/*********************當前已有進程%d個*************************/: ",shumu);
for(inti=0;i<shumu;i++){
printf("進程編號:%d",i+1);
printf(" /***********正在運行進程程序名:%s*************************/ ",neicun[i].pid);
printf(" /***********該進程的優先順序:%d*****************************/ ",neicun[i].priority);
printf(" /***********該進程的需要運行時間:%d***********************/ ",neicun[i].cputime);
printf(" /***********該進程的狀態:%d(1為等待,2就緒,3為運行)******/ ",neicun[i].state);}//這里增加顯示當前運行的進程
}

}
/*換出*/
voidhuanchu()
{
intk;
if(shumu<=0)//判斷是否存在進程
{
printf("當前進程數目為0,不能執行該操作 ");
return;
}
printf("當前已有進程%d個: ",shumu);
for(inth=0;h<shumu;h++)//當前所有的進程
printf("序號:%d 程序名:%s 優先順序:%d 運行時間:%d 狀態:%d "
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state);
printf("請輸入要換出程序的序號:");
scanf("%d",&k);
if(neicun[k].state==3)
{
neicun[k].state=1;
printf("已被換出,進程名為:%s、狀態為:[%d]",neicun[k].pid,neicun[k].state);
}
else
printf("無法換出,進程名為:%s的進程",neicun[k].pid);//換出結果提示
}
voidkill()
{

if(shumu<=0)//對存在的進程進行判斷
{
printf("當前進程數目為0,不能執行該操作 ");
return;
}
intk=0;
printf("/******************當前已有進程%d個******************/: ",shumu);
for(inth=0;h<shumu;h++)//當前所有的進程
printf("序號:%d 程序名:%s 優先順序:%d 運行時間:%d 狀態:%d "
,h,neicun[h].pid,neicun[h].priority,neicun[h].cputime,neicun[h].state);
printf("請輸入要殺死程序的序號:");
scanf("%d",&k);
neicun[k].cputime=0;
neicun[k].priority=0;
neicun[k].state=0;
if(k==(shumu-1))
shumu--;
else{
for(intj=k+1;j<shumu;j++)
{
strcmp(neicun[j-1].pid,neicun[j].pid);
neicun[j-1].priority=neicun[j].priority;
neicun[j-1].cputime=neicun[j].cputime;
neicun[j-1].state=neicun[j].state;
}
shumu--;
}
printf("進程名為:%s已被殺死! ",neicun[k].pid);//顯示進程已被殺死

}

❹ 操作系統的課程設計~進程式控制制系統~用C語言編寫的

補充:
這個程序當然可以在WINDOWS下編輯了.
不過編譯就必須要UNIX了.
下面的程序假設你的當前目錄有一個helloworld程序,
這個你會寫吧?
:)
#include
<stdio.h>
#include
<unistd.h>
#include
<sys/wait.h>
int
main()
{
int
pid;
int
status;
pid
=
fork();
if
(pid
==
0)
{
/*
child
process
*/
execve("./helloworld",
NULL,
NULL);
}
else
if
(pid
<
0)
{
/*
failed
to
fork,
print
error
message
*/
printf("fork
error,
errno
=
%d\n",
errno);
}
else
{
waitpid(pid,&status,0)
}
return
0;
}

❺ \c語言\ _\結束進程\

首先確定進程空間,然後查找到進程的句柄,在用某個系統調用結束它。
在現代計算機系統中,進程管理是操作系統的工作,所以就必須進行系統調用才能結束進程,這就涉及到了你的具體環境,比如是Win32的API還是linux下的C編程,他們的方法是不同的。

❻ 如何用c語言實現linux進程管理

你搜索一下關於「c shell」的命令,可以找到關於linux的進程管理,不是很復雜

❼ 用C語言,數據結構實現進程的創建,追加,刪除,查找,退出

需要設計進程式控制制塊的主體數據結構PCB, 進程式控制制塊需要包含進程管理的幾個主要的數據要素如下:
1. 後繼指針
2. 現場指針
進程創建主要完成數據的初始化,並把進程放入就緒隊列
不需要追加函數, 對於刪除,退出通常是數據結構的處理, 比較重要的是進程的調度,這里需要涉及到對CPU體系結構的依賴,需要了解CPU的寄存器,X86,ARM, PPC都有區別。

❽ c語言怎麼實現對windows進程管理器中的進程進行管理

進程之間是相互隔離的,你可以調用CreateToolhelp32Snapshot進行獲得進程使用和線程快照,如果終止當前進程可用ExitProcess,如果結束其它進程可用TerminateProcess

❾ 求: 用c語言實現進程式控制制的源碼

createthread(...)等等有一系列windowsAPI
你願意用嗎?
pv操作是利用信號量等的變數實現的,也有專門的api函數用於操作信號量等

熱點內容
dmporacle資料庫 發布:2025-05-16 02:44:31 瀏覽:830
雲主機上傳 發布:2025-05-16 02:44:30 瀏覽:82
滑鼠如何編程 發布:2025-05-16 02:29:09 瀏覽:816
安卓70能用什麼軟體 發布:2025-05-16 01:45:09 瀏覽:481
編程發展史 發布:2025-05-16 01:38:52 瀏覽:529
android圖片氣泡 發布:2025-05-16 01:38:40 瀏覽:887
文件加密編輯器下載 發布:2025-05-16 01:30:41 瀏覽:344
linuxapacheyum安裝 發布:2025-05-16 01:30:31 瀏覽:477
大連賓利浴池wifi密碼是多少 發布:2025-05-16 01:25:36 瀏覽:172
緩存數據生產服務 發布:2025-05-16 01:08:58 瀏覽:585