死锁避免算法设计来源: 发布时间:星期三, 2008年12月17日 浏览:184次 评论:0
/*------------------------------------------------------------------------
* University: UESTC * Student: 鲁立虹 * Number: 2406202002 * Create Date: 2007-6-21 * Program Name: deadLock * Description: deadLock Algorithm. * *------------------------------------------------------------------------ */ # <stdio.h> # M 3 //资源种类数 # N 4 //进程数 reg[N],r1[N]; count,c; /*------------------------------------------------------------------------ *画线 *------------------------------------------------------------------------ */ void line{ prf("------------------------------------------------------------------------\n"); } /*------------------------------------------------------------------------ *输入各数据,max,alloc,res *------------------------------------------------------------------------ */ void input( max[N] , alloc[N], res){ i,j; //输入各资源数目 prf("Enter the amount of each res:\n"); for(i=0; i<M; i) scanf("%d",&res ); line; //输入各进程需求各资源最大数 prf("Enter the biggest number of each res that each process want:\n"); for(i=0; i<N; i) for(j=0; j<M; j) scanf("%d",&max [j]); line; //输入各进程已经得到资源数 prf("Enter the number of each res that each process get:\n"); for(i=0; i<N; i) for(j=0; j<M; j) scanf("%d",&alloc [j]); line; } /*------------------------------------------------------------------------ *计算数据ar *------------------------------------------------------------------------ */ void cal( max[N] , alloc[N], res, ar){ i,j; pr ={M*0}; //进程占有各资源数 //计算pr for(i=0; i<N; i) for(j=0; j<M; j) pr[j] alloc [j]; //计算ar for(i=0; i<M; i) ar = res - pr ; } /*------------------------------------------------------------------------ *显示进程资源状态 *------------------------------------------------------------------------ */ void show( max[N] , alloc[N], ar){ i,j; prf("Available Res:\n"); prf("R1\tR2\tR3\t\n"); for(i=0; i<M; i) prf("%d\t",ar ); prf("\n"); line; prf("\t\tMax\t\t|\tAllocation\n"); prf("\tR1\tR2\tR3\t|\tR1\tR2\tR3\n"); for(i=0; i<N; i){ prf("P%d\t",i+1); for(j=0; j<M; j) prf("%d\t",max [j]); prf("|\t"); for(j=0; j<M; j) prf("%d\t",alloc [j]); prf("\n"); } } /*------------------------------------------------------------------------ * a<b?1:0 *------------------------------------------------------------------------ */ small( a , b){ i,sign; sign = 0; for(i=0; i<M; i) (a <=b ) sign; (sign M) 1; 0; } /*------------------------------------------------------------------------ *在reg中搜索dig,有返回1无返回0 *------------------------------------------------------------------------ */ look( reg[N], dig){ i; sign; sign = 0; for(i=0; i<N; i) (reg dig) sign; (!sign) 0; 1; } /*------------------------------------------------------------------------ *检查计算机系统是否安全,如果安全返回1不安全返回0 *------------------------------------------------------------------------ */ safe( max[N] , alloc[N], ar){ i,j; sign; need[N] ; //need [j],进程Pi需要资源Rj数 temp ; sign = 0; for(i=0; i<N; i) for(j=0; j<M; j) need [j] = max [j] - alloc [j]; (c < N) for(i=0; i<N; i){ (!look(r1,i)) (small(need ,ar)){ r1[c] = i; for(j=0; j<M; j) temp[j] = alloc [j]+ar[j]; sign = safe(max,alloc,temp); (sign) reg[count] = i; r1[--c] = -1; } } for(i=0; i<N; i){ (!look(reg,i)) (small(need ,ar)){ reg[count] = i; sign = 1; } } sign; } /*------------------------------------------------------------------------ *检查安全 *------------------------------------------------------------------------ */ checkSafe( max[N] , alloc[N], ar){ i; sign; count = 0; c = 0; for(i=0; i<N; i){ reg = -1; r1 = -1; } sign = safe(max,alloc,ar); (sign){ prf("The system is safe.\n"); prf("The safe route:"); for(i=N-1; i>0; i--) prf("P%d->",reg +1); prf("P%d\n",reg +1); line; 1; } { prf("The system is unsafe. Input again.\n"); line; 0; } } /*------------------------------------------------------------------------ * *------------------------------------------------------------------------ */ { i,j; p; sign; max[N] ; //max [j],进程Pi需要资源Rj最大数 alloc[N] ; //alloc [j],进程Pi已得到资源Rj数 alloc1[N] ; res ; //各资源数目 ar ; //系统现有各资源数 ar1 ; request ; //某进程请求资源数 do{ input(max,alloc,res); //输入各数据 cal(max,alloc,res,ar); //计算need,ar show(max,alloc,ar); //显示进程资源状态 sign = checkSafe(max,alloc,ar); //检查安全 }while(!sign); //输入发出请求进程号,请求资源 prf("P request R, Enter the process and each res, then check it.\n"); prf("If you want to end, evaluate P=0\n"); for( ; ; ){ line; for(i=0; i<N; i) for(j=0; j<M; j) alloc1 [j] = alloc [j]; prf("Enter P(1-%d):",N); scanf("%d",&p); (!p) ; prf("Enter request res:"); for(i=0; i<M; i) scanf("%d",&request ); (small(request,ar)){ for(i=0; i<M; i){ alloc1[p-1] = request + alloc1[p-1] ; ar1 = ar - request ; } sign = checkSafe(max,alloc1,ar1); (sign){ prf("Request Successfully. After requesting:\n"); show(max,alloc1,ar1); } } prf("Unsuccessfully request."); } } 0
相关文章读者评论发表评论 |
|