|
|
|
@ -0,0 +1,153 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#define NUM 10
|
|
|
|
|
#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 = 2;
|
|
|
|
|
PCB *runqueue;
|
|
|
|
|
PCB *top = NULL, *tail = NULL, *temp;
|
|
|
|
|
int i;
|
|
|
|
|
srand((int)time(0));
|
|
|
|
|
|
|
|
|
|
// 1. 初始化进程链表(创建 10 个进程,构建单链表)
|
|
|
|
|
for (i = 0; i < NUM; i++) {
|
|
|
|
|
temp = (PCB *)malloc(LEN);
|
|
|
|
|
temp->name = i + 1;
|
|
|
|
|
temp->runtime = rand() % 15;
|
|
|
|
|
temp->runedtime = 0;
|
|
|
|
|
temp->killtime = rand() % 20 + 1;
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
// 2. 时间片轮转调度核心逻辑
|
|
|
|
|
while (top != NULL) {
|
|
|
|
|
runqueue = top;
|
|
|
|
|
// 分配时间片,执行进程
|
|
|
|
|
runqueue->runedtime += timeslice;
|
|
|
|
|
|
|
|
|
|
if (runqueue->runedtime >= runqueue->killtime) {
|
|
|
|
|
// 进程执行完成
|
|
|
|
|
printf("process name %d, runtime=%d, runedtime=%d, killtime=%d ... END ...\n",
|
|
|
|
|
runqueue->name, runqueue->runtime, runqueue->runedtime, runqueue->killtime);
|
|
|
|
|
// 从链表移除
|
|
|
|
|
top = top->next;
|
|
|
|
|
free(runqueue);
|
|
|
|
|
} else {
|
|
|
|
|
// 时间片用完,移到链表尾部继续等待调度
|
|
|
|
|
printf("process name %d, runtime=%d, runedtime=%d, killtime=%d\n",
|
|
|
|
|
runqueue->name, runqueue->runtime, runqueue->runedtime, runqueue->killtime);
|
|
|
|
|
// 从头部移除
|
|
|
|
|
top = top->next;
|
|
|
|
|
// 挂到尾部
|
|
|
|
|
tail->next = runqueue;
|
|
|
|
|
runqueue->next = NULL;
|
|
|
|
|
tail = runqueue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
// 计算斐波那契数列(前10项)
|
|
|
|
|
void calculate_fibonacci() {
|
|
|
|
|
int n = 10;
|
|
|
|
|
int fib[10];
|
|
|
|
|
fib[0] = 0;
|
|
|
|
|
fib[1] = 1;
|
|
|
|
|
// 补全:递推计算斐波那契
|
|
|
|
|
for (int i = 2; i < n; i++) {
|
|
|
|
|
fib[i] = fib[i - 1] + fib[i - 2];
|
|
|
|
|
}
|
|
|
|
|
// 输出结果
|
|
|
|
|
printf("Fibonacci sequence (first 10 terms): ");
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
printf("%d ", fib[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算5的阶乘
|
|
|
|
|
int calculate_factorial() {
|
|
|
|
|
int n = 5;
|
|
|
|
|
int result = 1;
|
|
|
|
|
// 补全:循环计算阶乘
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
result *= i;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
pid_t son_pid, daughter_pid;
|
|
|
|
|
int son_status, daughter_status;
|
|
|
|
|
|
|
|
|
|
// 创建儿子进程
|
|
|
|
|
son_pid = fork();
|
|
|
|
|
if (son_pid < 0) {
|
|
|
|
|
perror("Fork failed");
|
|
|
|
|
exit(1);
|
|
|
|
|
} else if (son_pid == 0) {
|
|
|
|
|
// 儿子进程:执行斐波那契
|
|
|
|
|
calculate_fibonacci();
|
|
|
|
|
printf("I am son. My PID: %d, Father PID: %d\n", getpid(), getppid());
|
|
|
|
|
exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
// 创建女儿进程
|
|
|
|
|
daughter_pid = fork();
|
|
|
|
|
if (daughter_pid < 0) {
|
|
|
|
|
perror("Fork failed");
|
|
|
|
|
exit(1);
|
|
|
|
|
} else if (daughter_pid == 0) {
|
|
|
|
|
// 女儿进程:执行阶乘
|
|
|
|
|
int result = calculate_factorial();
|
|
|
|
|
printf("Factorial of 5: %d\n", result);
|
|
|
|
|
printf("I am daughter. My PID: %d, Father PID: %d\n", getpid(), getppid());
|
|
|
|
|
exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
// 父进程:输出自身信息
|
|
|
|
|
printf("I am father. My PID: %d, Son PID: %d, Daughter PID: %d\n",
|
|
|
|
|
getpid(), son_pid, daughter_pid);
|
|
|
|
|
|
|
|
|
|
// 等待儿子进程结束
|
|
|
|
|
wait(&son_status);
|
|
|
|
|
// 等待女儿进程结束
|
|
|
|
|
wait(&daughter_status);
|
|
|
|
|
|
|
|
|
|
// 输出子进程退出状态
|
|
|
|
|
printf("Son exit with status: %d\n", WEXITSTATUS(son_status));
|
|
|
|
|
printf("Daughter exit with status: %d\n", WEXITSTATUS(daughter_status));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|