01:198:214 Lecture Notes - Lecture 6: Context Switch, Gnu Compiler Collection, Tangled
//-------Notes For Systems Programming 10/19/17--------\\
//-------Synchronization-------\\
mutex: Mutal Exclusion Device
TA: TB:
lock(mutexA); lock(mutexA);
shared += 2; shared-=2;
unlock(mutexA); unlock(mutexA);
...
shared = 1;
who locks?
User: - User hase no control of scheduler if it is a kernel thread
- User can always be interrupted
System:
- Inefficient becuase you need to context switch everytime you call lock
(unload code and load in os call). Super expensive in terms of cost in time.
(We need special hardware instruction);
TA TB
shared = 1; shared = 1;
int lock(int mutexNum)
{while(mutexNum != 0){
mutexNum = 1;
}
return 1
}
//-------Compiler Directives-------\\
anything with: "__"in front is usually a gcc compiler directive (command to
compiler)
__File__ = Name of the current file in a string format
__LINE__ = Name of the line currently accessed
Atomic sets are called via double underbars
//-------Implementing Synchronization-------\\
test-and-set
compare-and-swap
fetch-and-incr
TA TB
lock(mutexA); lock(mutexA);
shared += 2; share -= 2;
other = 1; unlock(mutexA);
.
stuff=1;
thing = 4.1+8+mc;
.
.
.
.
unlock(mutexA);
pthread_exit(0);
PROBLEM : Thread one will run and then unlock and then thread B will run this
defeats the purpose of threadding because all we are doing is waiting for TA to
finish.
Usually : You have one mutex per shareed data item
solution :
TA TB
lock(mutexA); lock(mutexA);
shared += 2; share -= 2;
other = 1; unlock(mutexA);
unlock(mutexA);
lock(mutexB);
stuff=1;
thing = 4.1+8+mc;
.
.
.
.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
User hase no control of scheduler if it is a kernel thread. Inefficient becuase you need to context switch everytime you call lock (unload code and load in os call). Super expensive in terms of cost in time. (we need special hardware instruction); Tb int lock(int mutexnum) while(mutexnum != 0){ mutexnum = 1; return 1. //-------compiler directives-------\ anything with: __in front is usually a gcc compiler directive (command to compiler) __file__ = name of the current file in a string format. __line__ = name of the line currently accessed. Ta lock(mutexa); shared += 2; other = 1; stuff=1; thing = 4. 1+8+mc; unlock(mutexa); pthread_exit(0); Problem : thread one will run and then unlock and then thread b will run this defeats the purpose of threadding because all we are doing is waiting for ta to finish. Usually : you have one mutex per shareed data item solution :