



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The implementation of a cache algorithm using c language. It includes functions such as start(), read(), write(), commit(), and abort(). The cache algorithm manages cache entries, disk reads and writes, and transaction management.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!
lastTrans = 1; ... int start() { if (lastTrans < 0) return - else { lastTrans = -(++lastTrans); return lastTrans; } }
7/23/2013 CSEP 545 Docsity.com
int read(int blockAddr, int tId, int block) { / find the cache element e containing the block whose disk address is blockAddr / if (there is such a cache element e) { / the disk block at blockAddr is in cache / Cache(e).tId = tId; block = &Cache(e).newBlock; / &Cache(e).newBlock = address of Cache(e).newBlock / return 0; } else { / pick a cache entry e, where Cache(e).tId = 0. If there is no such entry, then return -1 */ diskRead(blockAddr, &Cache(e).oldBlock); Cache(e).newBlock = Cache(e).oldBlock; block = &Cache(e).newBlock; Cache(e).blockAddr = blockAddr Cache(e).tId = tId; return 0; }
}
int commit(int tId) { for (each cache entry e where Cache(e).tId == tId) { status = diskWrite(Cache(e).blockAddr, &Cache(e).newBlock); Cache(e).tId = - tId; if (status == -1) { Abort(tId); return -1; } Cache(e).oldBlock = Cache(e).newBlock }; for (each cache entry e where Cache(e).tId == -tId) Cache(e).tId = 0; lastTrans = -lastTrans;
return 0; }
int abort(int tId) { for (all cache entries e, where Cache(e).tId == -tId) { repeat { status = diskWrite(Cache(e).blockAddr, &Cache(e).oldBlock) }until (status == 0); /* Of course, this will not terminate if diskWrite keeps failing, but ignore that issue */
Cache(e).newBlock = Cache(e).oldBlock; }; for (all cache entries e) { Cache(e).tId = 0 }; lastTrans = -lastTrans; return 0; }