|
|
|
@ -0,0 +1,338 @@
|
|
|
|
|
实验二.1
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<unistd.h>
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
|
|
|
|
|
//-------begin---------
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
pid_t son_pid,daughter_pid;
|
|
|
|
|
|
|
|
|
|
son_pid = fork();
|
|
|
|
|
|
|
|
|
|
if(son_pid<0){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(son_pid==0){
|
|
|
|
|
printf("I am son,%d,%d\n",getpid(),getppid());
|
|
|
|
|
return 0;
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
daughter_pid = fork();
|
|
|
|
|
|
|
|
|
|
if(daughter_pid<0){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(daughter_pid==0){
|
|
|
|
|
printf("I am daughter,%d,%d\n",getpid(),getppid());
|
|
|
|
|
return 0;
|
|
|
|
|
}else{
|
|
|
|
|
printf("I am father,%d,%d,%d\n",getpid(),son_pid,daughter_pid);
|
|
|
|
|
wait(NULL);
|
|
|
|
|
wait(NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
实验二.2
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<unistd.h>
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
|
|
//-------begin---------
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
pid_t son_pid,daughter_pid;
|
|
|
|
|
|
|
|
|
|
son_pid = fork();
|
|
|
|
|
|
|
|
|
|
if(son_pid<0){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(son_pid==0){
|
|
|
|
|
printf("I am son,%d,%d\n",getpid(),getppid());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
execlp("ls", "ls", NULL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
daughter_pid = fork();
|
|
|
|
|
|
|
|
|
|
if(daughter_pid<0){
|
|
|
|
|
return 1;
|
|
|
|
|
}else if(daughter_pid==0){
|
|
|
|
|
printf("I am daughter,%d,%d\n",getpid(),getppid());
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
execlp("pwd", "pwd", NULL);
|
|
|
|
|
exit(1);
|
|
|
|
|
}else{
|
|
|
|
|
printf("I am father,%d,%d,%d\n",getpid(),son_pid,daughter_pid);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
实验三
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#define NUM 8
|
|
|
|
|
#define LEN sizeof(PCB)
|
|
|
|
|
struct PCB
|
|
|
|
|
{
|
|
|
|
|
int name;
|
|
|
|
|
int runtime;
|
|
|
|
|
int runedtime;
|
|
|
|
|
int killtime;
|
|
|
|
|
struct PCB *next;
|
|
|
|
|
};
|
|
|
|
|
typedef struct PCB PCB;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
int timeslice=5;
|
|
|
|
|
PCB *runqueue;
|
|
|
|
|
PCB *top,*tail,*temp;
|
|
|
|
|
int i;
|
|
|
|
|
srand((int)time(0));
|
|
|
|
|
for(i=0;i<NUM;i++)
|
|
|
|
|
{
|
|
|
|
|
temp=(PCB*)malloc(LEN);
|
|
|
|
|
temp->name=i;
|
|
|
|
|
temp->runtime=rand()%15+1;
|
|
|
|
|
temp->runedtime=0;
|
|
|
|
|
temp->killtime=0;
|
|
|
|
|
temp->next=NULL;
|
|
|
|
|
if(i==0)
|
|
|
|
|
{
|
|
|
|
|
top=temp;
|
|
|
|
|
tail=temp;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tail->next=temp;
|
|
|
|
|
tail=temp;
|
|
|
|
|
}
|
|
|
|
|
printf("START:process name %d, runtime=%d, runedtime=%d,killtime=%d\n",tail->name,tail->runtime,tail->runedtime,tail->killtime);
|
|
|
|
|
}
|
|
|
|
|
printf("******************************\n");
|
|
|
|
|
while(top != NULL)
|
|
|
|
|
{
|
|
|
|
|
runqueue = top;
|
|
|
|
|
top = top->next;
|
|
|
|
|
runqueue->runtime -= timeslice;
|
|
|
|
|
runqueue->runedtime += timeslice;
|
|
|
|
|
runqueue->killtime += timeslice;
|
|
|
|
|
if(runqueue->runtime<=0)
|
|
|
|
|
{
|
|
|
|
|
free(runqueue);
|
|
|
|
|
printf("process name %d, runtime=%d, runedtime=%d,killtime=%d...END...\n",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tail->next = runqueue;
|
|
|
|
|
tail = runqueue;
|
|
|
|
|
tail->next = NULL;
|
|
|
|
|
printf("process name %d, runtime=%d, runedtime=%d,killtime=%d\n",runqueue->name,runqueue->runtime,runqueue->runedtime,runqueue->killtime);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
实验五
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#define LEN sizeof(MEMORY_BLOCK)
|
|
|
|
|
#define NUM 5
|
|
|
|
|
|
|
|
|
|
struct MEMORY_BLOCK
|
|
|
|
|
{
|
|
|
|
|
int name;
|
|
|
|
|
int address;
|
|
|
|
|
int length;
|
|
|
|
|
int flag;
|
|
|
|
|
struct MEMORY_BLOCK *next;
|
|
|
|
|
};
|
|
|
|
|
typedef struct MEMORY_BLOCK MEMORY_BLOCK;
|
|
|
|
|
|
|
|
|
|
void allocation(MEMORY_BLOCK *Header, int name, int length_p);
|
|
|
|
|
void reclaim(int processname, MEMORY_BLOCK *Header);
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
int length_p, i;
|
|
|
|
|
int processname;
|
|
|
|
|
MEMORY_BLOCK *Header, *t;
|
|
|
|
|
|
|
|
|
|
// 初始化存储空间
|
|
|
|
|
Header = (MEMORY_BLOCK*)malloc(LEN);
|
|
|
|
|
Header->name = -1;
|
|
|
|
|
Header->address = 0;
|
|
|
|
|
Header->length = 100;
|
|
|
|
|
Header->flag = 0;
|
|
|
|
|
Header->next = NULL;
|
|
|
|
|
|
|
|
|
|
srand((int)time(0));
|
|
|
|
|
for(i = 1; i <= NUM; i++)
|
|
|
|
|
{
|
|
|
|
|
length_p = rand() % 20 + 1; // 随机产生进程所需存储空间,至少为1
|
|
|
|
|
allocation(Header, i, length_p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("内存分配情况: \n");
|
|
|
|
|
t = Header;
|
|
|
|
|
while(t != NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("process name %d, address=%d, length=%d, flag=%d\n",
|
|
|
|
|
t->name, t->address, t->length, t->flag);
|
|
|
|
|
t = t->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("请输入回收的进程号(输入0结束):\n");
|
|
|
|
|
scanf("%d", &processname);
|
|
|
|
|
while(processname != 0)
|
|
|
|
|
{
|
|
|
|
|
printf("回收 process name %d\n", processname);
|
|
|
|
|
reclaim(processname, Header);
|
|
|
|
|
|
|
|
|
|
t = Header;
|
|
|
|
|
while(t != NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("process name %d, address=%d, length=%d, flag=%d\n",
|
|
|
|
|
t->name, t->address, t->length, t->flag);
|
|
|
|
|
t = t->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Header->next == NULL) break;
|
|
|
|
|
|
|
|
|
|
printf("请输入回收的进程号(输入0结束):\n");
|
|
|
|
|
scanf("%d", &processname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("当前内存分配情况:\n");
|
|
|
|
|
t = Header;
|
|
|
|
|
while(t != NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("process name %d, address=%d, length=%d, flag=%d\n",
|
|
|
|
|
t->name, t->address, t->length, t->flag);
|
|
|
|
|
t = t->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reclaim(int processname, MEMORY_BLOCK *Header)
|
|
|
|
|
{
|
|
|
|
|
MEMORY_BLOCK *temp, *t, *tt;
|
|
|
|
|
t = Header;
|
|
|
|
|
temp = t;
|
|
|
|
|
|
|
|
|
|
// 查找要回收的进程块
|
|
|
|
|
while(t != NULL && t->name != processname)
|
|
|
|
|
{
|
|
|
|
|
temp = t;
|
|
|
|
|
t = t->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("未找到进程 %d\n", processname);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 标记为空闲
|
|
|
|
|
t->name = -1;
|
|
|
|
|
t->flag = 0;
|
|
|
|
|
|
|
|
|
|
if(t->next != NULL) // t非尾结点
|
|
|
|
|
{
|
|
|
|
|
if(temp->flag == 0 && t->next->flag == 0) // 左右都为空
|
|
|
|
|
{
|
|
|
|
|
temp->name = -1;
|
|
|
|
|
temp->length = temp->length + t->length + t->next->length;
|
|
|
|
|
tt = t->next;
|
|
|
|
|
temp->next = tt->next;
|
|
|
|
|
free(t);
|
|
|
|
|
free(tt);
|
|
|
|
|
}
|
|
|
|
|
else if(temp->flag == 0) // 左为空
|
|
|
|
|
{
|
|
|
|
|
temp->name = -1;
|
|
|
|
|
temp->length = temp->length + t->length;
|
|
|
|
|
temp->next = t->next;
|
|
|
|
|
free(t);
|
|
|
|
|
}
|
|
|
|
|
else if(t->next->flag == 0) // 右为空
|
|
|
|
|
{
|
|
|
|
|
t->length = t->length + t->next->length;
|
|
|
|
|
tt = t->next;
|
|
|
|
|
t->next = tt->next;
|
|
|
|
|
free(tt);
|
|
|
|
|
}
|
|
|
|
|
else // 左右都不为空
|
|
|
|
|
{
|
|
|
|
|
// 不需要合并,只需标记为空闲
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else // t是尾结点
|
|
|
|
|
{
|
|
|
|
|
if(temp->flag == 0) // 左为空
|
|
|
|
|
{
|
|
|
|
|
temp->name = -1;
|
|
|
|
|
temp->length = temp->length + t->length;
|
|
|
|
|
temp->next = NULL;
|
|
|
|
|
free(t);
|
|
|
|
|
}
|
|
|
|
|
else // 左不为空
|
|
|
|
|
{
|
|
|
|
|
// 不需要合并,只需标记为空闲
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void allocation(MEMORY_BLOCK *Header, int name, int length_p)
|
|
|
|
|
{
|
|
|
|
|
MEMORY_BLOCK *temp, *t;
|
|
|
|
|
int minsize = 2; // 不可切割的分区阈值
|
|
|
|
|
|
|
|
|
|
t = Header;
|
|
|
|
|
while(t != NULL)
|
|
|
|
|
{
|
|
|
|
|
if(t->length >= length_p && t->flag == 0) break;
|
|
|
|
|
t = t->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(t == NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("无法为进程 %d 分配 %d 大小的内存\n", name, length_p);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(t->length - length_p > minsize) // 分割
|
|
|
|
|
{
|
|
|
|
|
temp = (MEMORY_BLOCK*)malloc(LEN);
|
|
|
|
|
temp->name = -1;
|
|
|
|
|
temp->flag = 0;
|
|
|
|
|
temp->length = t->length - length_p;
|
|
|
|
|
temp->address = t->address + length_p;
|
|
|
|
|
|
|
|
|
|
t->name = name;
|
|
|
|
|
t->flag = 1;
|
|
|
|
|
t->length = length_p;
|
|
|
|
|
|
|
|
|
|
temp->next = t->next;
|
|
|
|
|
t->next = temp;
|
|
|
|
|
}
|
|
|
|
|
else // 直接分配
|
|
|
|
|
{
|
|
|
|
|
t->name = name;
|
|
|
|
|
t->flag = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|