This demonstrates the cache-misses introduced due to multi-core system
This implementation is based on multi-threading because the threads share same address space as compared to processes which inherently have different address space.
Before going to discuss the contents of the repository, I have to discuss some supplement code and concepts:
So consider this example where we want to change the global variable msg
by using function ModifyMessage
.
The supplement code works as follows:
- There are two function , namely
ShowMessage()
andModifyMessage()
which were to behave as per their name suggests. - The
ShowMessage()
hassleep(2)
, so the functionShowMessage()
starts withprintf
then sleeps for 2 seconds. - During this time the
ModifyMessage()
can jump in and modify the variablemsg
and return back the control toShowMessage()
. - Finally the other
printf
with modified message get excecuted and we are done.
#include <stdio.h>
#include <unistd.h>
char msg[50] = "uninitialized";
void *ShowMessage() {
printf("Address of msg is %p and msg:'%s'\n", &msg, msg);
sleep(2);
printf("A few seconds later msg = '%s'\n", msg);
}
void *ModifyMessage() {
//This does work in theory, in reality let see further.
strcpy(msg, "I'm a noob, live with it.");
}
int main() {
ShowMessage();
ModifyMessage();
}
rohan@hackerspace-$ g++ Demo1.cpp -o Manipulator
rohan@hackerspace-$ ./Manipulator
Address of msg is 0000000000403020 and msg:'uninitialized'
A few seconds later msg = 'uninitialized'
So this doesn't work in reality, the variable msg
doen't get modified.
-
The function
ShowMessage()
hassleep(2)
which basicaly makes entire conrol of the code sleep for 2 seconds. -
The main function has
ShowMessage()
&ModifyMessage()
arranged serially such that the functionModifyMessage()
only executes when functionShowMessage()
has done executing. -
Because of the above reason, the
msg
won't get modified before it reaches the lastprintf
in functionShowMessage()
.
- This time, we will make a thread for function
ShowMessage()
, for this code we will call this function asvoid *Child_Thread(void *tid)
, wheretid
is Thread ID. - Because threads share a common address space, the threads ChildThread and ParentThread are oblivious to each others share of memory.
- Threads are used to share the CPU time of application by leveraging the use of modern hardware i.e the threads can execute concurrently on single/multiple CPUs.
- Because the threads can run in parallel, the function
ParentThread
executes concurrently with threadChildThread
.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
char msg[100] = "uninitialized";
void *Child_Thread(void *tid) {
printf("In child thread at %p: '%s':\n", &msg, msg);
sleep(2);
printf("In child thread at %p: '%s':\n", &msg, msg);
}
void Parent_Thread() {
sleep(1);
strcpy(msg, "I know threads, duhh aa");
printf("In parent thread at %p: '%s':\n", &msg, msg);
}
int main() {
pthread_t thr;
pthread_create(&thr, NULL, Child_Thread, NULL); // Spawn thread
Parent_Thread();
pthread_exit(NULL);
}
rohan@hackerspace-$ g++ Demo2.cpp -lpthread
rohan@hackerspace-$ ./a.out
In child thread at 0x601080: 'uninitialized':
In parent thread at 0x601080: 'I know threads, duhh aa':
In child thread at 0x601080: 'I know threads, duhh aa':
Given that the threads share memory or they are oblivious to each others share of memory. In supplement code #2, while thread Child_thread
sleeps for 2 seconds; the Parent_Thread
jumps in and modify the global variable msg
.
So above is the example to show that threads can modify each other memory. Now coming to multi-core system, the threads can be dispacthed to a specific cpu core, but this has to be decided at programming time. This repository shows false-sharing in caches due to a multicore sytem.
False-sharing is done on the thread by sharing of thread's local data.
This demonstration shows eviction of cache line from L1 cache of other CPU core because of write-updation in one L1 cache.