//用信号量实现磁盘调度 SCAN 算法
#define N 100
typedef int semaphore;
semaphore s = 0; //互斥信号量
int headpos = 0; //磁盘磁头当前位置
int headmax = 256; //磁盘磁头最大位置
int direction = 1; //1 为 up,0 为 down.
int i = 0;
struct pos
{
int pos;
semaphore s;
}a[N];
main()
{
cobegin
move(); //磁盘移动进程
access(); //磁盘数据存取进程
coend
}
access()
{
int t;
获得磁盘访问位置"position";
p(s);
t = i; //使结构数组中磁盘位置和信号量同步所设置的临时变量
a[i++].pos = position; //进程所需要访问的磁盘位置为临界资源,将此位置存入临界
区
v(s);
p(a[t].s);
访问"position"所指示的磁盘区; //磁盘一次只能访问一个磁盘位置,属于临界区
}
move()
{
p(s);
对所有访问磁盘的进程位置由小到大进行排序; //为防止排序时有新的进程进入队列
而设立的临界区
v(s);
int j;
if (direction == up) //如果方向向上
{
for (j = 0; headpos>=a[j].pos || headpos>=headmax; j++)
{
if (headpos == a[j-1].pos)
评论1