User talk:KUNDALIPOWER

int n,m;//n for no of process and m is for no of resources int alloc[10][10],max[10][10],need[10][10]; int available[10],total[10],work[10],finish[10],seq[10],req[10]; int pno; void accept { int i,j; printf("Enter no of process:"); scanf("%d",&n); printf("Enter the no of resource type:"); scanf("%d",&m); printf("Enter total instances of each resource type\n"); for(j=0;j<m;j++) { printf("%c:",65+j); scanf("%d",&total[j]); } printf("Enter the allocation\n"); for(i=0;i<n;i++) { printf("p%d:\n",i); for(j=0;j<m;j++) { printf("%c:",65+j); scanf("%d",&alloc[i][j]); } } printf("Enter max need:\n"); for(i=0;i<n;i++) { printf("p%d:\n",i); for(j=0;j<m;j++) { printf("%c:",65+j); Page 1 Untitled scanf("%d",&max[i][j]); } } } void calc_avail { int i,j,s; for(j=0;j<m;j++) { s=0; for(i=0;i<n;i++) s += alloc[i][j]; available[j]=total[j]-s; } } void calc_need { int i,j; for(i=0;i<n;i++) for(j=0;j<m;j++) need[i][j]=max[i][j]-alloc[i][j]; } void print { int i,j; printf("\n\tAllocation\t\tMax\t\tNeed\n\t"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%3c",65+j); printf("\t"); } for(i=0;iwork[j]) break; if(j==m) return i; } } return -1; } void bankers { int i,j,k=0; for(i=0;i<n;i++) finish[i]=0; for(j=0;j<m;j++) work[j]=available[j]; while((i=find)!=-1) { printf("\nNeed%d(",i); for(j=0;j<m;j++) printf("%d,",need[i][j]); printf("\b)<=work("); for(j=0;j<m;j++) printf("%d,",work[j]); printf("\b)"); finish[i]=1; for(j=0;j<m;j++) work[j] +=alloc[i][j]; printf("\nwork("); for(j=0;j<m;j++) printf("%d,",work[j]); printf("\b)\nFinish("); for(j=0;jneed[pno][j]) break; if(j==m) { for(j=0;javailable[j]) break; if(j==m) { for(j=0;j

FCFS ---/ int main { int RQ[100],i,n,TotalHeadMoment=0,initial; printf("Enter the number of Requests\n"); scanf("%d",&n); printf("Enter the Requests sequence\n"); for(i=0;i
 * 2) include

ssft int main { int RQ[100],i,n,TotalHeadMoment=0,initial,count=0; printf("Enter the number of Requests\n"); scanf("%d",&n); printf("Enter the Requests sequence\n"); for(i=0;id) { min=d; index=i; } } TotalHeadMoment=TotalHeadMoment+min; initial=RQ[index]; // 1000 is for max // you can use any number RQ[index]=1000; count++; } printf("Total head movement is %d",TotalHeadMoment); return 0; }
 * 1) include
 * 2) include

scan main { int nBlocks,headDirection,th,temp, currentPosition,nRequest,i,j; int *diskRequestString; printf("\tEnter Number of Request : "); scanf("%d",&nRequest); printf("\tEnter Number of Blocks : "); scanf("%d",&nBlocks); printf("\tHead Direction : "); scanf("%d",&headDirection); printf("\tCurrent Position : "); scanf("%d",&currentPosition); diskRequestString = (int*) malloc(sizeof(int)*nRequest); for(i=0; i diskRequestString[j]) { temp = diskRequestString[i]; diskRequestString[i] = diskRequestString[j]; diskRequestString[j] = temp; } } } if( headDirection == 1 ) { th = (nBlocks-currentPosition) + (nBlocks-diskRequestString[0]); for(i=0; i= currentPosition) break; } temp = i; printf("%d",currentPosition); for(i%d",diskRequestString[i]); printf("->%d",nBlocks-1); for(i=temp-1; i>=0; i--) printf("->%d",diskRequestString[i]); } else { th = currentPosition + diskRequestString[nRequest-1]; for(i=0; i currentPosition) break; } temp = i; printf("%d",currentPosition); for(i--; i>=0; i--) printf("->%d",diskRequestString[i]); Page 9 Untitled printf("->0"); for(i=temp; i%d",diskRequestString[i]); } printf("\ntotal head movements observed : %d\n",th); }
 * 1) include 
 * 2) include 

cscan int main { int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move; printf("Enter the number of Requests\n"); scanf("%d",&n); printf("Enter the Requests sequence\n"); for(i=0;i<n;i++) scanf("%d",&RQ[i]); printf("Enter initial head position\n"); scanf("%d",&initial); printf("Enter total disk size\n"); scanf("%d",&size); printf("Enter the head movement direction for high 1 and for low 0\n"); scanf("%d",&move); // logic for C-Scan disk scheduling /*logic for sort the request array */ for(i=0;i<n;i++) { for( j=0;j<n-i-1;j++) { if(RQ[j]>RQ[j+1]) { int temp; temp=RQ[j]; RQ[j]=RQ[j+1]; RQ[j+1]=temp; } } } int index; for(i=0;i<n;i++) { if(initial<RQ[i]) {
 * 1) include<stdio.h>
 * 2) include<stdlib.h>

index=i; break; } } // if movement is towards high value if(move==1) { for(i=index;i<n;i++) { TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i]; }

TotalHeadMoment=TotalHeadMoment+abs(size-RQ[i-1]-1);

TotalHeadMoment=TotalHeadMoment+abs(size-1-0); initial=0; for( i=0;i<index;i++) { TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i]; } } // if movement is towards low value else { for(i=index-1;i>=0;i--) { TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i]; } // last movement for min size TotalHeadMoment=TotalHeadMoment+abs(RQ[i+1]-0); /*movement min to max disk */ TotalHeadMoment=TotalHeadMoment+abs(size-1-0); initial =size-1; for(i=n-1;i>=index;i--) { TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial); initial=RQ[i]; } } printf("Total head movement is %d",TotalHeadMoment); return 0; }

Contiguous File Allocation -*/

struct freelist { 	int start; int size; struct freelist *next; };
 * 1) include<stdio.h>
 * 2) include<string.h>
 * 3) include<stdlib.h>

struct usedlist { 	char fname[10]; int fstart; int fsize; };

struct freelist *head=NULL,*new=NULL,*temp,*prev,*temp1;

struct usedlist dir_ent[10];

int dir_index=0;

main { 	int ch,i; char filename[10]; create; do { 		printf("\n*****menu*******\n"); printf("1.Create file\n"); printf("2. delete File\n"); printf("3. Show Free and Used Block list\n"); printf("4. exit\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { 			case 1: allocate; break; case 2: deallocate; break; case 3: printf("\nThe free list is"); printf("\nStartBlock\tSize\n"); for (temp=head;temp!=NULL; temp=temp->next) {   					printf("%d",temp->start); printf("\t%d\n",temp->size); }        			printf("The used list is"); printf("\nFilename\tStart\tLength\n"); for(i=0;i<dir_index;i++) {   					printf("%s\t%d\t\t%d\n",dir_ent[i].fname,dir_ent[i].fstart,dir_ent[i].fsize); }        			break; case 4: exit(0); break; } 	}while (ch!=4); }

create { 	int no_of_blocks; printf("enter number of blocks\n"); scanf("%d",&no_of_blocks); new = (struct freelist*)malloc(sizeof(struct freelist)); head=new; new->start=0; new->size=no_of_blocks; new->next=NULL; }

allocate {  	int s, allocated=0; char filename[10]; printf("enter file name \n"); scanf("%s",filename); printf("enter size of a file in blocks\n"); scanf("%d",&s); for(temp=head;temp!=NULL;) {         	if(temp->size < s)            		temp=temp->next; else {          		temp->size-=s; strcpy(dir_ent[dir_index].fname,filename); dir_ent[dir_index].fstart=temp->size; dir_ent[dir_index].fsize=s; dir_index++; allocated=1; break; }        	if (temp==NULL && allocated==0) printf("Disk space not available\n"); } }

deallocate {   	int i=0,flag=0,coalsc=0; int del_start,del_size; char filename[10]; printf("enter file name "); scanf("%s",filename); for(i=0;i<dir_index;i++) {     		if(strcmp(dir_ent[i].fname,filename)==0) {        		printf("file found\n"); strcpy( dir_ent[i].fname,"deleted"); del_start=dir_ent[i].fstart; del_size=dir_ent[i].fsize; dir_ent[i].fstart=-1; dir_ent[i].fsize=-1; for(temp=head;temp!=NULL;temp=temp->next) {          			if (((temp->start+temp->size)==del_start) || ((del_start+del_size)==temp->start)) {                  			temp->size+=del_size; coalsc=1; break; }         		}          		if(coalsc==0) {        			//  printf(" coalsc= %d\n ",coalsc); new=(struct freelist*)malloc(sizeof(struct freelist)); new->start=del_start; new->size=del_size; new->next=NULL; printf("added start %d size %d in free blocks\n",new->start,new->size); // printf("%d", head->start); temp1=head; // printf("%d", temp1->start); prev=temp1; while(temp1->start<del_start && temp1->next!=NULL) { 		 			prev=temp1; temp1=temp1->next; }        			//  printf("%d\n",prev->start); new->next=temp1->next ; prev->next=new; }     			flag=1; break; }  	}    	if(flag==0) printf("file not found\n"); }

Contiguous File Allocation -*/

struct freelist { 	int start; int size; struct freelist *next; };
 * 1) include<stdio.h>
 * 2) include<string.h>
 * 3) include<stdlib.h>

struct usedlist { 	char fname[10]; int fstart; int fsize; };

struct freelist *head=NULL,*new=NULL,*temp,*prev,*temp1;

struct usedlist dir_ent[10];

int dir_index=0;

main { 	int ch,i; char filename[10]; create; do { 		printf("\n*****menu*******\n"); printf("1.Create file\n"); printf("2. delete File\n"); printf("3. Show Free and Used Block list\n"); printf("4. exit\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { 			case 1: allocate; break; case 2: deallocate; break; case 3: printf("\nThe free list is"); printf("\nStartBlock\tSize\n"); for (temp=head;temp!=NULL; temp=temp->next) {   					printf("%d",temp->start); printf("\t%d\n",temp->size); }        			printf("The used list is"); printf("\nFilename\tStart\tLength\n"); for(i=0;i<dir_index;i++) {   					printf("%s\t%d\t\t%d\n",dir_ent[i].fname,dir_ent[i].fstart,dir_ent[i].fsize); }        			break; case 4: exit(0); break; } 	}while (ch!=4); }

create { 	int no_of_blocks; printf("enter number of blocks\n"); scanf("%d",&no_of_blocks); new = (struct freelist*)malloc(sizeof(struct freelist)); head=new; new->start=0; new->size=no_of_blocks; new->next=NULL; }

allocate {  	int s, allocated=0; char filename[10]; printf("enter file name \n"); scanf("%s",filename); printf("enter size of a file in blocks\n"); scanf("%d",&s); for(temp=head;temp!=NULL;) {         	if(temp->size < s)            		temp=temp->next; else {          		temp->size-=s; strcpy(dir_ent[dir_index].fname,filename); dir_ent[dir_index].fstart=temp->size; dir_ent[dir_index].fsize=s; dir_index++; allocated=1; break; }        	if (temp==NULL && allocated==0) printf("Disk space not available\n"); } }

deallocate {   	int i=0,flag=0,coalsc=0; int del_start,del_size; char filename[10]; printf("enter file name "); scanf("%s",filename); for(i=0;i<dir_index;i++) {     		if(strcmp(dir_ent[i].fname,filename)==0) {        		printf("file found\n"); strcpy( dir_ent[i].fname,"deleted"); del_start=dir_ent[i].fstart; del_size=dir_ent[i].fsize; dir_ent[i].fstart=-1; dir_ent[i].fsize=-1; for(temp=head;temp!=NULL;temp=temp->next) {          			if (((temp->start+temp->size)==del_start) || ((del_start+del_size)==temp->start)) {                  			temp->size+=del_size; coalsc=1; break; }         		}          		if(coalsc==0) {        			//  printf(" coalsc= %d\n ",coalsc); new=(struct freelist*)malloc(sizeof(struct freelist)); new->start=del_start; new->size=del_size; new->next=NULL; printf("added start %d size %d in free blocks\n",new->start,new->size); // printf("%d", head->start); temp1=head; // printf("%d", temp1->start); prev=temp1; while(temp1->start<del_start && temp1->next!=NULL) { 		 			prev=temp1; temp1=temp1->next; }        			//  printf("%d\n",prev->start); new->next=temp1->next ; prev->next=new; }     			flag=1; break; }  	}    	if(flag==0) printf("file not found\n"); }

Contiguous File Allocation -*/

struct freelist { 	int start; int size; struct freelist *next; };
 * 1) include<stdio.h>
 * 2) include<string.h>
 * 3) include<stdlib.h>

struct usedlist { 	char fname[10]; int fstart; int fsize; };

struct freelist *head=NULL,*new=NULL,*temp,*prev,*temp1;

struct usedlist dir_ent[10];

int dir_index=0;

main { 	int ch,i; char filename[10]; create; do { 		printf("\n*****menu*******\n"); printf("1.Create file\n"); printf("2. delete File\n"); printf("3. Show Free and Used Block list\n"); printf("4. exit\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { 			case 1: allocate; break; case 2: deallocate; break; case 3: printf("\nThe free list is"); printf("\nStartBlock\tSize\n"); for (temp=head;temp!=NULL; temp=temp->next) {   					printf("%d",temp->start); printf("\t%d\n",temp->size); }        			printf("The used list is"); printf("\nFilename\tStart\tLength\n"); for(i=0;i<dir_index;i++) {   					printf("%s\t%d\t\t%d\n",dir_ent[i].fname,dir_ent[i].fstart,dir_ent[i].fsize); }        			break; case 4: exit(0); break; } 	}while (ch!=4); }

create { 	int no_of_blocks; printf("enter number of blocks\n"); scanf("%d",&no_of_blocks); new = (struct freelist*)malloc(sizeof(struct freelist)); head=new; new->start=0; new->size=no_of_blocks; new->next=NULL; }

allocate {  	int s, allocated=0; char filename[10]; printf("enter file name \n"); scanf("%s",filename); printf("enter size of a file in blocks\n"); scanf("%d",&s); for(temp=head;temp!=NULL;) {         	if(temp->size < s)            		temp=temp->next; else {          		temp->size-=s; strcpy(dir_ent[dir_index].fname,filename); dir_ent[dir_index].fstart=temp->size; dir_ent[dir_index].fsize=s; dir_index++; allocated=1; break; }        	if (temp==NULL && allocated==0) printf("Disk space not available\n"); } }

deallocate {   	int i=0,flag=0,coalsc=0; int del_start,del_size; char filename[10]; printf("enter file name "); scanf("%s",filename); for(i=0;i<dir_index;i++) {     		if(strcmp(dir_ent[i].fname,filename)==0) {        		printf("file found\n"); strcpy( dir_ent[i].fname,"deleted"); del_start=dir_ent[i].fstart; del_size=dir_ent[i].fsize; dir_ent[i].fstart=-1; dir_ent[i].fsize=-1; for(temp=head;temp!=NULL;temp=temp->next) {          			if (((temp->start+temp->size)==del_start) || ((del_start+del_size)==temp->start)) {                  			temp->size+=del_size; coalsc=1; break; }         		}          		if(coalsc==0) {        			//  printf(" coalsc= %d\n ",coalsc); new=(struct freelist*)malloc(sizeof(struct freelist)); new->start=del_start; new->size=del_size; new->next=NULL; printf("added start %d size %d in free blocks\n",new->start,new->size); // printf("%d", head->start); temp1=head; // printf("%d", temp1->start); prev=temp1; while(temp1->start<del_start && temp1->next!=NULL) { 		 			prev=temp1; temp1=temp1->next; }        			//  printf("%d\n",prev->start); new->next=temp1->next ; prev->next=new; }     			flag=1; break; }  	}    	if(flag==0) printf("file not found\n"); }

index array

int files[50], indexBlock[50], indBlock, n; void recurse1; void recurse2; void recurse1{ printf("Enter the index block: "); scanf("%d", &indBlock); if (files[indBlock] != 1){ printf("Enter the number of blocks and the number of files needed for the index %d on the disk: ", indBlock); scanf("%d", &n); }   else{ printf("%d is already allocated\n", indBlock); recurse1; }   recurse2; } void recurse2{ int ch,i,k,j; int flag = 0; for (i=0; i<n; i++){ scanf("%d", &indexBlock[i]); if (files[indexBlock[i]] == 0) flag++; }   if (flag == n){ for (j=0; j<n; j++){ files[indexBlock[j]] = 1; }       printf("Allocated\n"); printf("File Indexed\n"); for (k=0; k<n; k++){ printf("%d --> %d : %d\n", indBlock, indexBlock[k], files[indexBlock[k]]); }   }    else{ printf("File in the index is already allocated\n"); printf("Enter another indexed file\n"); recurse2; }   printf("Do you want to enter more files?\n"); printf("Enter 1 for Yes, Enter 0 for No: "); scanf("%d", &ch); if (ch == 1) recurse1; else exit(0); return; } int main {	int i;   for(i=0;i<50;i++) files[i]=0; recurse1; return 0; }
 * 1) include <stdio.h>
 * 2) include <conio.h>
 * 3) include <stdlib.h>

file allocation

/* Linked File Allacation --*/


 * 1) include<stdio.h>
 * 2) include<stdlib.h>
 * 3) include<string.h>

struct file { char filename[20]; int filesize; int startblock; int endblock; };

typedef struct file f;

struct node { int blockno; struct node *next; }; typedef struct node block;

block *freelist=NULL,*allocate=NULL,*lasta=NULL; f f1[20]; int no=1,d,size,count=1,countfblock=0;

block * getblock { 	block *temp; temp=(block *)malloc(sizeof(block)); temp->blockno=no; no++; temp->next=NULL; return(temp); }

block * addblock(block *ptr) {	 block *temp; temp=(block *)malloc(sizeof(block)); temp->blockno=ptr->blockno; temp->next=NULL; return(temp); }

block * create {  	block *temp,*last=NULL,*list=NULL; int i;  	for(i=0;i<d;i++) {    		temp=getblock; if(list==NULL) {			list=temp; last=temp; }    		else {			last->next=temp; last=temp; }    	}      return(list); }

block * createalloclist {  	block *temp,*ptr=freelist,*prev; int i;  	f1[count].startblock=ptr->blockno; for(i=0;i<f1[count].filesize && ptr!=NULL;i++) {    		temp=addblock(ptr); f1[count].endblock=temp->blockno; prev=ptr; freelist=ptr->next; ptr=ptr->next; free(prev); if(allocate==NULL) {			allocate=temp; lasta=temp; }    		else {			lasta->next=temp; lasta=temp; }    	}        return(allocate); }

void displaylist(block *list1) {  	block *ptr; for(ptr=list1;ptr!=NULL;ptr=ptr->next) printf("%d->",ptr->blockno); }

void countfree(block *list1) {  	block *ptr; for(ptr=list1;ptr->next!=NULL;ptr=ptr->next) countfblock++; }

void acceptfile {

printf("\nEnter the file name:"); scanf("%s",&f1[count].filename); printf("\nEnter file size in blocks:"); scanf("%d",&f1[count].filesize); }

void displayfile {       int i;	printf("\nFile name\t\tFile size\t\tstart block\t\tEnd block"); for(i=1;i<=count;i++) {		printf("\n%s",f1[i].filename); printf("\t\t\t%d",f1[i].filesize); printf("\t\t\t%d",f1[i].startblock); printf("\t\t\t%d",f1[i].endblock); }	//count++; }

int search(char *fname) { 	int i,flag=0; for(i=1;i<=count;i++) {   		if(strcmp(f1[i].filename,fname)==0) {			flag=i; break; }

}   	return flag; }

block* searchpos(int j) { block *ptr2,*prev; for(ptr2=freelist,prev=freelist;ptr2->blockno<j && ptr2!=NULL;prev=ptr2,ptr2=ptr2->next); return prev; }

block* deletefile(int j) { block *ptr,*ptr1,*temp,*prev,*prev1; int i=1; for(ptr=allocate,prev1=allocate;ptr->blockno!=f1[j].startblock;prev1=ptr,ptr=ptr->next); prev=searchpos(ptr->blockno); temp=addblock(ptr); if(prev==freelist) {	 	temp->next=prev; freelist=temp; prev=temp; if(ptr==allocate) {	    		allocate=ptr->next; free(ptr); ptr=allocate; }	 	else {	 		prev1->next=ptr->next; free(ptr); ptr=prev1->next; }	 i++; }   	for(ptr1=ptr;ptr1!=NULL && i<=f1[j].filesize;i++) {      		temp=addblock(ptr1); temp->next=prev->next; prev->next=temp; prev=temp; if(ptr1==allocate) {	    		allocate=ptr1->next; free(ptr1); ptr1=allocate; }      		else {      			prev1->next=ptr1->next; free(ptr1); ptr1=prev1->next; }  	}   return (freelist); }

int main { 	int ch,result; char fname[20]; printf("\nEnter the size of disk in blocks"); scanf("%d",&d); freelist=create; while(1) {   		printf("\n1: Allocate space for newly created file."); printf("\n2: Deallocate space for deleted file."); printf("\n3: Show used and free space on disk."); printf("\n4: Exit"); printf("\nEnter the choice"); scanf("%d",&ch); switch(ch) {     			case 1: acceptfile; countfree(freelist); if(countfblock>=f1[count].filesize) {					allocate=createalloclist; displayfile; count++; }				else printf("\nNo sufficient space to allocate"); break; case 2: printf("\nEnter name of the file to be deleted: "); scanf("%s",&fname); result=search(fname); if(result==0) printf("\nFile not present on disk"); else {		 		     freelist=deletefile(result); strcpy(f1[result].filename,"deleted"); f1[result].startblock=-1; f1[result].endblock=-1; f1[result].filesize=-1;

}                            //   displayfile; break; case 3: printf("\nFree list:"); displaylist(freelist); printf("\nAllocated list: "); displaylist(allocate); break; case 4: exit(0);

} 	} }