## Posted By

Feb30th1712 on 09/03/08

# second chance algorithm

/ Published in: C

Second chance algorithm a.k.a clock algorithm implementation.

`/* filename: sca.c* topic: page table replacement* algorithm: second chance algorithm (clock algorithm)* language: ansi c* version: 1.0.0* status: beta* programmer: Lyfia C. Vaex* contact: http://en.wikipedia.org/wiki/User:Feb30th1712* license: GNU General Public License 2** release date: Jan 26, 2006* last updated: n/a* update by: n/a** development kit: Cgywin, Vim and GCC** known issues:* 1. used Hungariun notation for variable naming*/#include#define FN 4 //frame number#define RB 0 //reference bit#define PN 1 //page numberint main(void){	//hp = hand pointer, in = input, pt = page table	int rgPT[FN][2] = {0}, nHP = 3, nIn, iPT; 	printf("Enter: ");	while ( scanf("%d", &nIn) )	{		getchar(); 		//search page table		for (iPT = 0; rgPT[iPT][PN] != nIn && iPT < FN; iPT++); 		//failed to retrieve		if (rgPT[iPT][PN] != nIn)		{			//search for a victim			do {				nHP = (nHP + 1) % FN;			} while ( !( rgPT[nHP][RB] == 1 ? rgPT[nHP][RB] = 0 : 1 ) );			//update the page table			rgPT[nHP][RB] = 1;			rgPT[nHP][PN] = nIn;		} 		//retrieved		else			rgPT[iPT][RB] = 1; 		//show page table state		puts("page table:");		for (iPT = 0; iPT < FN; iPT++)			printf("%s %d, %d.\n", iPT == (nHP + 1) % 4 ? ">": " ", rgPT[iPT][RB], rgPT[iPT][PN]);		putchar('\n'); 		printf("Enter: ");	}	puts("May the Roses bloom upon your Cross. "); 	return 0;}`