Homework 3-4 Sarah Diesburg Operating Systems COP 4610
1. All Possible Execution Orders Thread AThread B x = 3;y = 2; x = y - 1;y = x + 1; x = 3y = 2x = y - 1y = 2 y = x + 1 x = y - 1y = x + 1 x = y - 1 x = 3y = x + 1 x = 3 x = y - 1 (x = ?, y = ?) (x = 3, y = ?) (x = ?, y = ?) (x = ?, y = 2) (x = ?, y = ?) (x = 3, y = 2) (x = ?, y = 2) (x = ?, y = ?) (x = 1, y = 2) (x = 3, y = 4) (x = 3, y = ?) (x = ?, y = ?)
2. Why does disabling interrupts not work on multi-processors? Interrupts are disabled on a per-CPU basis A thread running on another CPU could enter the critical area
3. Too Much Milk w/ Busy Waits //define the busy-wait locks value = 0; Lock::Acquire() { // while the previous value is BUSY, loop while (test_and_set(value) == 1); } Lock::Release() { value = 0; }
3. (cont.) //Dumb Lock::Acquire(value) if(no milk) { //go get milk Lock::Release(value) //Dumber Lock::Acquire(value) if(no milk) { //go get milk Lock::Release(value)
4. Does this work? // consumer waits on 0 semaphore nLoadedBuffers = 0; // producer waits on 0 semaphore nFreeBuffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(nFreeBuffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(nLoadedBuffers); 5. V(mutex); } Consumer() { 6. P(nLoadedBuffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(nFreeBuffers); }
4. Does this work? // consumer waits on 0 semaphore nLoadedBuffers = 0; // producer waits on 0 semaphore nFreeBuffers = N; // N >= 2 // one thread waits when another thread is // modifying the buffer semaphore mutex = 1; Producer() { 1. P(nFreeBuffers); 2. P(mutex); 3. // put 1 item in the buffer 4. V(nLoadedBuffers); 5. V(mutex); } Consumer() { 6. P(nLoadedBuffers); 7. P(mutex); 8. // take 1 item from the buffer 9. V(mutex); 10. V(nFreeBuffers); }
4. Does this work? Actually, yes. Even if producer thread hits #4 and wakes up a consumer sleeping on #6, the consumer will not be able to enter the critical section until the producer comes back and hits #5
Additional Things
Race Conditions – An anology Suppose there is a bag of chips in the kitchen at home and I want to make “walking tacos” for dinner
Race Conditions – An anology Suppose there is a bag of chips in the kitchen at home and I want to make “walking tacos” for dinner If my husband gets home first, the bag of chips will be consumed before dinner My dinner plans are foiled! If I get home first, I can use the bag of chips for dinner
$PATH My $PATH: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games Shell (or exec call) searches every path for the name of the program you just called It then executes the first matching program found Adding a :. to the end of your path tells the shell that you want to execute programs found in your current directory automatically
Bash Files.bash_history Contains a history of all your bash commands.bash_logout Script that is executed when you log out Current script clears your console Can add anything here
Bash Files.bash_profile and.bashrc Both contain scripts to set up environment when logging in .bash_profile actually calls.bashrc Also calls.firstlogin – what do you think that is for? Modifies $PATH as well .bashrc Non-login prompts Sets some variables, shell prompt, aliases (shortcuts)
Shell Files So where does $PATH actually come from? On this system, /etc/profile /etc directory holds system-wide configurations The profile file is universal to any shell you run /bin/bash, /bin/sh, /bin/ksh, …
Shell Files So, where would you want to modify path if you wanted to Change it for all users? Change it for just yourself?