CENG/BILM 341 FINAL REVIEW Dr. Esma Yıldırım
Ques@on 1 Ques@on: An array of threads are working for filling and emptying a buffer that can hold mul@ple elements. There are two types of threads in the system. The P type is responsible for filling up the buffer while the C type is emptying it. If the buffer is full P threads have to wait and if it is empty C threads have to wait. The code implemented with semaphores for both types of threads are given below. What are the possible problems in the code? How should we update it to make it correct?(birden fazla eleman tutabilen bir bufferı doldurmak ve boşaltmak için birden fazla thread çalışmaktadır. Sistemde iki @p thread vardır. P @pi bufferı doldurmak ile görevli iken, C @p threadler boşaltmakla görevlidir. Eğer buffer dolu ise P threadleri yer boşalana kadar beklemeli boş ise C threadleri eleman olana kadar beklemelidir. Her iki thread @pi için kod semaforlar ile yazılmış olup aşağıdaki gibi verilmiş@r. Oluşabilecek problemler nelerdir ve nasıl kod doğru çalışabilir hale gelebilir ) P C Wait(S); while(temp!=- 1) Put(); Wait(S); Post(S); temp = Get(); Post(S);
Answer Threads can never understand if the buffer is empty or full. (Threadler bufferın boş yada dolu olduğunu anlayamazlar) P Wait(empty) Wait(S); Put(); Post(S); Post(Full) C while(temp!=- 1) Wait(full); Wait(S); temp = Get(); Post(S); Post(Empty);
Ques@on 2 For a memory system that has physical memory of 100Frames, any memory allocator thread allocates 10Frames at a @me, how would you update your semaphore implementa@on. (100 çerçevelik fiziksel hafzaya sahip olan bir hafza sisteminde hafzada yer ayırtmak için her seferinde 10 çerçeve istenmektedir. Buna göre semafor kodu nasıl değiş@rilmelidir)
typedef struct Sem_t { int value; pthread_cond_t cond; pthread_mutex_t lock; } Sem_t; // only one thread can call this void Sem_init(Sem_t *s, int value) { s- >value = value; Cond_init(&s- >cond); Mutex_init(&s- >lock); } void Sem_wait(Sem_t *s) { Mutex_lock(&s- >lock); while (s- >value <= 0) Cond_wait(&s- >cond, &s- >lock); s- >value- - ; Mutex_unlock(&s- >lock); } void Sem_post(Sem_t *s) { Mutex_lock(&s- >lock); s- >value++; Cond_signal(&s- >cond); Mutex_unlock(&s- >lock); } Ques@on 2 cont.
Answer Typedef struct{ int memorysize; int allocsize; pthread_cond_t cond; pthread_mutex_t lock; }Sem_t; void Sem_init(Sem_t *s, int value1,int value2){ s- >memorysize = value1; s- >allocsize = value2; Cond_init(&s- >cond); Mutex_init(&s- >lock); } void Sem_wait(Sem_t *s){ mutex_lock(&s- >lock); while(s- > memorysize < =0) Cond_wait(&s- >cond, &s- >lock); s- >memorysize - =s- >allocsize; mutex_unlock(&s- >lock); } void Sem_post(Sem_t *s){ mutex_lock(&s- >lock); s- >memorysize +=s- >allocsize; Cond_signal(&s- >cond); mutex_unlock(&s- >lock); }
Answer int main() { Sem_t *s; Sem_init(s,100,10); } void allocate(sem_t *s) { Sem_wait(s); } void deallocate(sem_t *s) { Sem_post(s); }
Ques@on 3 How else would you solve the deadlock problem in the Dining Philosophers Problem besides breaking the circular wait? What other problem other than deadlock could occur in this case? (Hint: Allow Preemp@on)(Yemekteki Filozoflar problemi dairesel beklemeyi kırma dışında nasıl çözülebilir? Bu çözümle başka ne gibi problemler ortaya çıkabilir?)
Answer Do{ pickuplerfork //sol çatalı al if(trypickuprightfork(l2)==- 1)//sağ çatalı almaya çalış putdownlerfork //sol çatalı bırak sleep for a while //uyu }while(true) Problem Livelock
Ques@on 4 Assume S, R and T are binary semaphores, and X, Y, Z are threads. (S,R,T binary semafor, X,Y,Z ise 3 adet threaddir) X consists of the following four statements(x threadi aşağıdaki 4 operasyondan oluşur): Post(S); Post(R);Post(T); Wait(T);Wait(R); Wait(S); Y consists of the following statements(y threadi aşağıdaki 4 operasyondan oluşur): Post(T); Post(S); Wait(T); Post(R); Wait(R); Wait (S); And, thread Z consists of the following statements(z threadi aşağıdaki 4 operasyondan oluşur): Post(R); Post(T); Post(S); Wait(R);Wait(S); Wait(T) Would it be safer to run X and Y together or to run X and Z together? Please jus@fy your answer.(x ve Y nin mi yoksa X ve Znin mi aynı anda çalışması daha güvenli olur)
Answer X and Y are safer because they lock the semaphores in the same order(x ve Y daha güvenlidir çünkü semaphorların kilitlenme sırası aynıdır.)
Ques@on 5 Rota@on speed : 1 msec per 1 degree (360degree in total) (dönme hızı 1ms de 1 derece- toplam 360 derece) 12 sectors per track (track başı 12 sektör) Head is on sector 6 (ilk olarak başlık 6. sektörde) Seek @me for transfer between tracks: 40msec (trackler arası arama süresi 40ms) Transfer @me from start of the sector to the end is 30 msec (rota@on @me per sector) (Transfer süresi sektör başı 30ms) For layout of the disk, run./disk.py G (disk şekli için./disk.py programını çalışyrın.) Calculate the rota@on, seek and transfer @mes for the following requests sector 7, 30, 8 for FIFO, SSTF, SPTF algorithms (Sektör 7,30,8 e olan istekler için FIFO, SSTF ve SPTF algoritmaları uygulanırsa arama, dönme ve transfer sürelerini hesaplayın)
Answer FIFO REQUESTS ['7', '30', '8'] Block: 7 Seek: 0 Rotate: 15 Transfer: 30 Total: 45 Block: 30 Seek: 80 Rotate:220 Transfer: 30 Total: 330 Block: 8 Seek: 80 Rotate:310 Transfer: 30 Total: 420 TOTALS Seek:160 Rotate:545 Transfer: 90 Total: 795 SSTF Block: 7 Seek: 0 Rotate: 15 Transfer: 30 Total: 45 Block: 8 Seek: 0 Rotate: 0 Transfer: 30 Total: 30 Block: 30 Seek: 80 Rotate:190 Transfer: 30 Total: 300 TOTALS Seek: 80 Rotate:205 Transfer: 90 Total: 375 SPTF Block: 7 Seek: 0 Rotate: 15 Transfer: 30 Total: 45 Block: 8 Seek: 0 Rotate: 0 Transfer: 30 Total: 30 Block: 30 Seek: 80 Rotate:190 Transfer: 30 Total: 300 TOTALS Seek: 80 Rotate:205 Transfer: 90 Total: 375
Ques@on 6 Draw the layout for the files consis@ng of 4, 12, 2, 8 blocks in a RAID Level 0 system with 6 disks and a chunk size of 2 blocks? (4, 12,2, ve 8 bloktan oluşan 4 dosya için 6 diskten oluşan ve grup büyüklüğü 2 blok olan RAID 0 sisteminde dosya bloklarının yerlerini gösteriniz)
Answer Disk 0 Disk 1 Disk 2 Disk 3 Disk 4 Disk 5 F1 (1) F1(3) F2(1) F2(3) F2(5) F2(7) F1(2) F1(4) F2(2) F2(4) F2(6) F2(8) F2(9) F2(11) F3(1) F4(1) F4(3) F4(5) F2(10) F2(12) F3(2) F4(2) F4(4) F4(6) F4(7) F4(8)
Ques@on 7 Write a command to change the name of the file X that resides in the root directory to Y inside the current working directory? Write the corresponding system call? (Root klasöründe yer alan X dosyasının adını o anki dizinde yer alan Y dosyasına çeviren komutu ve bu komutun çağırdığı sistem çağrısını yazın) Answer: mv /X./Y rename( /X,./Y );
Ques@on 8 How many reads/writes are necessary to open /X/Z/Y.txt?(inode+data block) (/X/Z/Y.txt dosyasını açmak için kaç tane okuma ve/veya yazma gereklidir?
Answer Read root inode Read root data Read X inode Read X data Read Z inode Read Z data Read Y.txt inode