You are on page 1of 14

RING #include<stdio.h> #include<malloc.h> #include<stdlib.

h> FILE *fp; int co =-1; struct process { int pid; struct process *next; }; struct process *temp,*head; int search(int pid); void bubblesort(); void initiate_election(int pid); void fileread(); void arrival_process(int pid); void departure_process(int pid); void display_sorted();

void initiate_election(int pid) { struct process *temp1; int ps[15],i,j,k,ptr,count,ps1[15],max; if(search(pid) && pid != co) { temp1 = head; for(i = 0 ; temp1 != NULL;i++,temp1 = temp1->next) { ps[i] = temp1->pid; } count = i; if(count != 0) { i = 0; while(pid != ps[i]) i++; ptr = i; j = 0; fp = fopen("op.txt","a+"); do { ps1[j++] = ps[ptr]; fprintf(fp,"\nprocess %d send message ---------[ ",ps[ptr]); for(k = 0 ; k < j; k++) { fprintf(fp,"%d,",ps1[k]);

} fprintf(fp,"\b]------> to process %d",ps[((ptr+1 )%count)]); ptr = (ptr+1)%count; }while(ps[ptr] != pid); fclose(fp); max = ps1[0]; for(k = 0 ; k < j;k++) { if(max < ps1[k]) max = ps1[k]; } co = max; fp = fopen("op.txt","a+"); fprintf(fp,"\npid = %d is new co ordinator.",co); fclose(fp); } else printf("\nNo node exist in the distributed system"); } else printf("\nEither the pid is already co ordinator or\nThe pid is not exist"); } void fileread() { char ch; fp = fopen("op.txt","r"); ch = fgetc(fp); while(ch != EOF) { printf("%c",ch); ch = fgetc(fp); } fclose(fp); } void departure_process(int pid) { int p; struct process *temp1,*temp2,*temp3; if(search(pid)) { temp1 = head; if(temp1->pid == pid) { temp2 = temp1; head = temp1->next; free(temp2); } else { temp3 = temp1; while(temp1->next != NULL) { if(temp1->next->pid==pid) {

temp2=temp1->next; temp1->next=temp1->next->next; free(temp2); break; } temp3 = temp1; temp1 = temp1->next; } if(temp3->next->pid == pid) { temp3->next = NULL; free(temp1); } } } else { printf("\nEntered pid is not exist in distributed system "); return; } if(pid == co) { co = -1; do{ printf("\nEnter the pid which initiates the election:"); scanf("%d",&p); }while(!search(p)); initiate_election(p); } } void display_sorted() { struct process *temp1; temp1 = head; printf("\nPID in sorted order"); while(temp1 != NULL) { printf("\n%d",temp1->pid); temp1 = temp1->next; } } int search(int pid) { struct process *temp1; temp1 = head; while(temp1 != NULL) { if(temp1->pid == pid) return 1; temp1 = temp1->next; } return 0; } void arrival_process(int pid) { struct process *p; if(!search(pid)) {

if(head == NULL) { head = (struct process *)malloc(sizeof(struct process)); head->pid = pid; head->next = NULL; } else { p = head; while(p->next != NULL) { p = p->next; } p->next = (struct process *)malloc(sizeof(struct process)); p = p->next ; p->pid = pid; p->next = NULL; } bubblesort(); } else printf("\nPID is already exist...."); if(pid > co) { initiate_election(pid); } } /* Only pid in the ascending order & not the process nodes*/ void bubblesort() { int a; struct process *temp1,*temp2; for(temp1 = head;temp1->next != NULL;temp1 = temp1->next) { for(temp2 = temp1->next; temp2 != NULL;temp2 = temp2->next) { if(temp1->pid > temp2->pid) { a = temp1->pid; temp1->pid = temp2->pid; temp2->pid = a; } } } } int main() { int pid,choice; head = NULL; fp = fopen("op.txt","w+"); fclose(fp); do{ printf("\nMENU\n\t1>Arrival process" "\n\t2>Departure Process" "\n\t3>Display Communication log"

"\n\t4>Display co-ordinator" "\n\t5>Exit" "\nEnter Your Choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the pid :"); scanf("%d",&pid); arrival_process(pid); display_sorted(); break; case 2: printf("\nEnter the pid :"); scanf("%d",&pid); departure_process(pid); break; case 3: fileread(); break; case 4: printf("Current co ordinator is : %d",co ); break; case 5: break; default: printf("\nPlease enter the valid choice"); } }while(choice != 5); return 0; } ******************************************************************************* BULLY #include<stdio.h> int co; struct process { int pid; int live; struct process *next; }; struct process *head,*temp; int search(int pid) { struct process *temp1; temp1 = head; while(temp1 != NULL) { if(temp1->pid == pid && temp1->live == 1) return 1; temp1 =temp1->next; } return 0; }

void bubblesort() { struct process *temp1,*temp2; int t; for(temp1 = head;temp1->next != NULL;temp1= temp1->next) { for(temp2 = temp1->next;temp2->next !=NULL; temp 2 = temp2->next) { if(temp2->pid > temp2->next->pid ) { t = temp2->pid ; temp2->pid = tem p2->next->pid temp2->next->pid = t; t = temp2->live ; temp2->live = te mp2->next->live temp2->next->liv e = t; } } } } void initiate_election(int pid) { struct process *temp1,*temp2,*temp3; if(search(pid)) { temp1 = head; while(temp1 != NULL) { if(temp1->pid == pid) break; temp1 = temp1->next; } if(temp1->next == NULL) { fp = fopen("op.txt","a+"); fprintf(fp,"\npid = %d is new co ordinator.",pid); fclose(fp); co = temp1->pid; } else { fp = fopen("op.txt","a+"); for(temp2 = temp1;temp2->next!=NULL && temp2->live == 1; temp2 = temp2->next) { fprintf(fp,"\n\npid = %d initiate the election. ",temp2->pid); for(temp3 = temp2->next;temp3 != NULL && temp3->

live == 1;temp3 = temp3->next) { fprintf(fp,"\n\npid = %d ------------ELE CTION message-------> pid= %d",temp2->pid,temp3->pid); fprintf(fp,"\npid = %d --------------OK message-----------> pid= %d",temp3->pid,temp2->pid); } } fprintf(fp,"\n\npid = %d initiate the election. ",temp2>pid); fprintf(fp,"\npid = %d do not get reply from anyone . ", temp2->pid); fprintf(fp,"\npid = %d declare itself to as a coordinato r. ",temp2->pid); co = temp2->pid; fclose(fp); } } else printf("\nEither the pid is already co ordinator or\nThe pid is not exist"); } void fileread() { char ch; fp = fopen("op.txt","r"); ch = fgetc(fp); while(ch != EOF) { printf("%c",ch); ch = fgetc(fp); } fclose(fp); } int main() { int choice,pid,n,flag=0; head = NULL; co = -1; printf("How many process are communicating in distributed system"); scanf("%d",&n); for(i = 1; i <= n;i++) { printf("\nEnter the process id:"); scanf("%d",&pid); if(head == NULL) { head = (struct process*)malloc(sizeof(st ruct process)); head->pid = pid; head->live = 1; temp = head; } else

{ temp->next = (struct process*)malloc(siz eof(struct process)); temp = temp->next; temp->pid = pid; temp->live = 1; } temp->next = NULL; } bubblesort(); temp = head; while(temp->next != NULL) temp = temp->next; printf("\nCo ordinator is %d",temp->pid); co = temp->pid; do { printf("\nMenu" "\n\t1>Display the communicating processes" "\n\t2>Process to be crash" "\n\t3>Recover a process" "\n\t4>Display coordinator" "\n\t5>Exit" "\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\nFollowing proc esses in distributed system :\n"); for(temp = head;temp!= N ULL;temp=temp->next) { if(temp->live == 1) printf(" \n\tprocess id = %d is live ",temp->pid); else printf(" \n\tprocess id = %d is crashed",temp->pid); } break; case 2: printf("\nEnter the pid to be crash : "); scanf("%d",&pid); for(temp = head;temp!= N ULL;temp=temp->next) { if(temp->live == 1 && temp->pid == pid) { temp->li ve = 0; flag = 1 ; } } if(flag != 1)

{ printf(" \nNo such pid exist in the distributed system."); } else { printf(" \nprocess crashed successfully"); } if(pid == co) { printf("\nCo ord inator process failed"); do{ printf(" \nPid should belong to distributed proceses \nAnd should not be coordinator\nEnt er the pid who initiates election:"); scanf("% d",&pid); }while(!search(p id)); initiate_electio n(pid); } break; } }while(choice!=5); return 0; } ******************************************************************************** **************** Vector #include<stdio.h> #include<stdlib.h> struct log { int data; struct log *next; }; struct masterlog{ int p_id; struct log *head; struct masterlog *next; }; struct masterlog *ml_head = NULL,*ml_temp,*ml_temp1; struct log *log_head = NULL,*log_temp,*log_temp1; int search( int pid) { ml_temp=ml_head; while(ml_temp != NULL) { if(ml_temp->p_id == pid) { return 1;

} ml_temp=ml_temp->next; } return 0; } int main() { int count=0,i,j,k,l,pid1,pid2,choice,temp_data; printf("\n\tVector Algorithm for simple clock"); do { printf("\n\tMenu :::::\n" "\n\t1>Arrival of Process" "\n\t2>Make Communication" "\n\t3>Display communication Details" "\n\t4>Departure of Process" "\n\t5>Exit" "\n\tEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1: log_head=NULL; count=0; j=0; printf("\nEnter the process Id for arrival::"); scanf("%d",&pid1); j=search(pid1); if(j==1) { printf("\n Process with ID %d is already exist . .Enter another ID...\n",pid1); break; } if(ml_head == NULL) { ml_head = (struct masterlog*)malloc(sizeof(struc t masterlog)); ml_head->p_id = pid1; log_head = (struct log*)malloc(sizeof(struct log )); log_head->data = 0; log_head->next = NULL; ml_head->head = log_head; ml_head->next = NULL; } else { ml_temp1=ml_head; if(ml_temp1 ->next == NULL) { log_temp1 = ml_temp1->head; count++; log_temp1->next = (struct log*)malloc(si zeof(struct log)); log_temp1 = log_temp1->next; log_temp1->data = 0;

log_temp1->next = NULL; } else { while(ml_temp1 ->next!= NULL) { log_temp1 = ml_temp1->head; while(log_temp1->next != NULL) { log_temp1 = log_temp1->n ext; } log_temp1->next = (struct log*)m alloc(sizeof(struct log)); log_temp1 = log_temp1->next; log_temp1->data = 0; log_temp1->next = NULL; ml_temp1 = ml_temp1->next; } log_temp1 = ml_temp1->head; while(log_temp1->next != NULL) { log_temp1 = log_temp1->next; count++; } count++; log_temp1->next = (struct log*)malloc(si zeof(struct log)); log_temp1 = log_temp1->next; log_temp1->data = 0; log_temp1->next = NULL; } ml_temp1->next = (struct masterlog *)malloc(size of(struct masterlog)); ml_temp1 = ml_temp1->next; ml_temp1->p_id = pid1; for(i=0;i<=count;i++) { if(log_head == NULL) { log_head = (struct log*)malloc(s izeof(struct log)); log_head->data=0; log_temp=log_head; } else { log_temp->next = (struct log*)ma lloc(sizeof(struct log)); log_temp = log_temp->next; log_temp->data = 0; } } ml_temp1->head = log_head; ml_temp1->next = NULL; } break; case 2: j=0,k=0; printf("\nEnter the communication DETAILS [pid1] [pid2]

="); scanf("%d%d",&pid1,&pid2); j=search(pid1); if(j==0) { printf("\n Process with ID %d is does not exist ..Enter existing proces ID...\n",pid1); break; } k=search(pid2); if(k==0) { printf("\n Process with ID %d is does not exist ..Enter existing proces ID...\n",pid2); break; } l=0; ml_temp=ml_head; while(ml_temp!=NULL) { if(ml_temp->p_id == pid1) { for(i=0,log_temp = ml_temp->head;log_tem p!=NULL,i<l;i++,log_temp = log_temp->next); log_temp->data+=1; break; } l++; ml_temp=ml_temp->next; } l=0; ml_temp1=ml_head; while(ml_temp1!=NULL) { if(ml_temp1->p_id == pid2) { for(i=0,log_temp1 = ml_temp1->head;log_t emp1!=NULL,i<l;i++,log_temp1 = log_temp1->next); temp_data=log_temp1->data; for(log_temp = ml_temp->head,log_temp1 = ml_temp1->head;log_temp1!=NULL;log_temp = log_temp->next,log_temp1 = log_temp1>next) { if(log_temp1->data>log_t emp->data) continue; log_temp1->data=log_temp ->data; } for(i=0,log_temp1 = ml_temp1->head;log_t emp1!=NULL,i<l;i++,log_temp1 = log_temp1->next); log_temp1->data=temp_data+1;

break; } l++; ml_temp1=ml_temp1->next; } break; case 3: printf("\nCommunication details:::::::::"); ml_temp = ml_head; while(ml_temp != NULL) { log_temp1 = ml_temp->head; printf("\nProcess ID %d ::",ml_temp->p_id); while(log_temp1 != NULL) { printf("\t%d",log_temp1->data); log_temp1 = log_temp1->next; } ml_temp = ml_temp->next; } break; case 4: j=0; k=0; printf("\nEnter the process Id for departure::"); scanf("%d",&pid1); j=search(pid1); if(j==0) { printf("\n Process with ID %d is does not exist ..Enter existing proces ID...\n",pid1); break; } ml_temp=ml_head; if(ml_temp->p_id == pid1) { ml_head=ml_temp->next; free(ml_temp); k++; } else { while(ml_temp->next->p_id != pid1) { ml_temp = ml_temp->next; k++; } k+=2; ml_temp1 = ml_temp->next; ml_temp->next = ml_temp->next->next;

free(ml_temp1); } printf("Node to be deleted::%d",k); ml_temp=ml_head; while(ml_temp != NULL) { log_temp=ml_temp->head; i=1; if(k==1) { ml_temp->head=log_temp->next; free(log_temp); } else { while(i<(k-1)) { log_temp=log_temp->next; i++; } log_temp1 = log_temp->next; log_temp->next = log_temp->next->next; free(log_temp1); } ml_temp = ml_temp->next; } break; case 5 : exit(0); } }while(choice !=5); return 0; }

You might also like