C语言---指针优先级

int i = 0 ;
int *j = &i;
(*j)++;
printf("%d", i);// 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优先级调度算法是一种常用的进程调度算法,它根据进程的优先级来决定下一个执行的进程。在C语言中,我们可以使用指针来实现优先级队列。 以下是一个简单的优先级调度算法的C语言实现: ```c #include <stdio.h> #include <stdlib.h> #define MAX_PROCESS 10 typedef struct { int pid; int priority; } process; process* ready_queue[MAX_PROCESS]; int queue_size = 0; void add_process(int pid, int priority) { process* new_process = (process*)malloc(sizeof(process)); new_process->pid = pid; new_process->priority = priority; // 找到新进程应该插入的位置 int i; for (i = queue_size - 1; i >= 0; i--) { if (ready_queue[i]->priority >= priority) { break; } ready_queue[i+1] = ready_queue[i]; } ready_queue[i+1] = new_process; queue_size++; } process* get_next_process() { if (queue_size == 0) { return NULL; } process* next_process = ready_queue[0]; // 将队列中的进程向前移动一个位置 int i; for (i = 0; i < queue_size-1; i++) { ready_queue[i] = ready_queue[i+1]; } queue_size--; return next_process; } int main() { // 添加一些测试进程 add_process(1, 2); add_process(2, 1); add_process(3, 4); add_process(4, 3); // 输出进程的执行顺序 process* next_process; while ((next_process = get_next_process()) != NULL) { printf("Process %d (priority %d) is executing\n", next_process->pid, next_process->priority); free(next_process); } return 0; } ``` 在上面的代码中,我们使用了一个指针数组 `ready_queue` 来保存就绪队列中的进程,`queue_size` 记录队列中的进程数。`add_process` 函数用于向队列中添加新进程,它会根据进程的优先级找到新进程在队列中的插入位置。`get_next_process` 函数用于获取下一个应该执行的进程,它会弹出队列中的第一个进程并返回。在 `main` 函数中,我们添加了一些测试进程并输出它们的执行顺序。 希望这个例子可以帮助你理解如何使用指针来实现优先级调度算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值