使用C语言完成进程的创建、终止、阻塞、唤醒、通信和实现先来先服务调度算法
时间: 2023-10-18 09:06:24 浏览: 124
【操作系统课程设计】Linux环境下使用C语言实现先来先服务调度算法.doc
进程的创建:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Error: fork failed.\n");
exit(1);
} else if (pid == 0) {
// 子进程
printf("This is child process.\n");
exit(0);
} else {
// 父进程
printf("This is parent process.\n");
wait(NULL); // 等待子进程结束
exit(0);
}
}
```
进程的终止:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Error: fork failed.\n");
exit(1);
} else if (pid == 0) {
// 子进程
printf("This is child process.\n");
exit(0);
} else {
// 父进程
printf("This is parent process.\n");
wait(NULL); // 等待子进程结束
printf("Child process has terminated.\n");
exit(0);
}
}
```
进程的阻塞和唤醒:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
printf("Error: fork failed.\n");
exit(1);
} else if (pid == 0) {
// 子进程
printf("This is child process.\n");
sleep(5); // 子进程休眠5秒
exit(0);
} else {
// 父进程
printf("This is parent process.\n");
wait(&status); // 等待子进程结束
if (WIFEXITED(status)) {
printf("Child process has terminated.\n");
}
exit(0);
}
}
```
进程的通信:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int fd[2];
pid_t pid;
char buf[256];
if (pipe(fd) < 0) {
printf("Error: pipe failed.\n");
exit(1);
}
pid = fork();
if (pid < 0) {
printf("Error: fork failed.\n");
exit(1);
} else if (pid == 0) {
// 子进程
close(fd[0]); // 关闭读端
write(fd[1], "Hello, parent process!", 23);
exit(0);
} else {
// 父进程
close(fd[1]); // 关闭写端
read(fd[0], buf, 23);
printf("%s\n", buf);
wait(NULL); // 等待子进程结束
exit(0);
}
}
```
先来先服务调度算法:
```c
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id; // 进程ID
int arrive_time; // 到达时间
int service_time; // 服务时间
int start_time; // 开始时间
int finish_time; // 完成时间
int turnaround_time; // 周转时间
int waiting_time; // 等待时间
};
int main() {
int n, i, j;
float avg_turnaround_time = 0, avg_waiting_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n], temp;
for (i = 0; i < n; i++) {
printf("Enter the arrival time and service time of process %d: ", i + 1);
scanf("%d%d", &p[i].arrive_time, &p[i].service_time);
p[i].id = i + 1;
}
// 按到达时间排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (p[j].arrive_time > p[j + 1].arrive_time) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
// 计算开始时间、完成时间、周转时间和等待时间
for (i = 0; i < n; i++) {
if (i == 0) {
p[i].start_time = p[i].arrive_time;
} else {
p[i].start_time = p[i - 1].finish_time;
}
p[i].finish_time = p[i].start_time + p[i].service_time;
p[i].turnaround_time = p[i].finish_time - p[i].arrive_time;
p[i].waiting_time = p[i].turnaround_time - p[i].service_time;
avg_turnaround_time += p[i].turnaround_time;
avg_waiting_time += p[i].waiting_time;
}
// 输出结果
printf("Process\tArrival Time\tService Time\tStart Time\tFinish Time\tTurnaround Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", p[i].id, p[i].arrive_time, p[i].service_time, p[i].start_time, p[i].finish_time, p[i].turnaround_time, p[i].waiting_time);
}
avg_turnaround_time /= n;
avg_waiting_time /= n;
printf("Average turnaround time: %.2f\n", avg_turnaround_time);
printf("Average waiting time: %.2f\n", avg_waiting_time);
return 0;
}
```
阅读全文