Lecture Notes: 16 Data Races
··3 mins
Sum101
#include <stdio.h>
#include <assert.h>
// A billion.
const long TOP = 1000000000;
int
main(int _ac, char* _av[])
{
printf("Summing numbers divisible by 101 from 0 to %ld.\n", TOP - 1);
long sum = 0;
for (long ii = 0; ii < TOP; ++ii) {
if (ii % 101 == 0) {
sum += ii;
}
}
printf("Sum = %ld\n", sum);
return 0;
}
Parallel version
#include <stdio.h>
#include <assert.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int
main(int _ac, char* _av[])
{
// A billion.
const long TOP = 1000000000;
const long NPP = TOP / 10;
printf("Summing numbers divisible by 101 from 0 to %ld.\n", TOP - 1);
long* sum = mmap(0, sizeof(long), PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
pid_t kids[10];
for (int pp = 0; pp < 10; ++pp) {
if ((kids[pp] = fork())) {
// do nothing
}
else {
int i0 = NPP*pp;
int iN = NPP*pp + NPP;
for (int ii = i0; ii < iN; ++ii) {
if (ii % 101 == 0) {
*sum += ii;
}
}
munmap(sum, sizeof(long));
exit(0);
}
}
for (long pp = 0; pp < 10; ++pp) {
waitpid(kids[pp], 0, 0);
}
printf("Sum = %ld\n", *sum);
munmap(sum, sizeof(long));
return 0;
}
Add a lock:
#include <stdio.h>
#include <assert.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
void*
malloc_shared(size_t size)
{
return mmap(0, size, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
}
void
free_shared(void* ptr, size_t size)
{
munmap(ptr, size);
}
int
main(int _ac, char* _av[])
{
// A billion.
const long TOP = 1000000000;
const long NPP = TOP / 10;
printf("Summing numbers divisible by 101 from 0 to %ld.\n", TOP - 1);
long* sum = malloc_shared(sizeof(long));
sem_t* lock = malloc_shared(sizeof(sem_t));
sem_init(lock, 1, 1);
// Semaphores?
pid_t kids[10];
for (int pp = 0; pp < 10; ++pp) {
if ((kids[pp] = fork())) {
// do nothing
}
else {
int i0 = NPP*pp;
int iN = NPP*pp + NPP;
for (int ii = i0; ii < iN; ++ii) {
// try wait here
if (ii % 101 == 0) {
sem_wait(lock);
*sum += ii;
sem_post(lock);
}
// and post here
}
free_shared(sum, sizeof(long));
free_shared(lock, sizeof(sem_t));
exit(0);
}
}
for (long pp = 0; pp < 10; ++pp) {
waitpid(kids[pp], 0, 0);
}
printf("Sum = %ld\n", *sum);
free_shared(sum, sizeof(long));
free_shared(lock, sizeof(sem_t));
return 0;
}
Real work
#include <stdio.h>
#include <assert.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <semaphore.h>
void*
malloc_shared(size_t size)
{
return mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
}
void
free_shared(void* ptr, size_t size)
{
munmap(ptr, size);
}
int
main(int _ac, char* _av[])
{
sem_t* locks = malloc_shared(2 * sizeof(sem_t));
sem_t* aa = &(locks[0]);
sem_t* bb = &(locks[1]);
sem_init(aa, 1, 1);
sem_init(bb, 1, 1);
int cpid;
if ((cpid = fork())) {
printf("In parent\n");
sem_wait(aa);
sleep(1);
sem_wait(bb);
printf("Doing real work in parent...");
sem_post(bb);
sem_post(aa);
waitpid(cpid, 0, 0);
}
else {
printf("In child\n");
sem_wait(bb);
sleep(1);
sem_wait(aa);
printf("Doing real work in child...\n");
sem_post(bb);
sem_post(aa);
}
free_shared(locks, sizeof(long));
return 0;
}