Skip to content

Instantly share code, notes, and snippets.

@say4n
Last active September 4, 2019 18:38
Show Gist options
  • Select an option

  • Save say4n/3c15d512b28c42a7337f7374e26b9294 to your computer and use it in GitHub Desktop.

Select an option

Save say4n/3c15d512b28c42a7337f7374e26b9294 to your computer and use it in GitHub Desktop.
semaphore implementation in c++
.PHONY: all
all: procA procB
procA: processA.cpp
mkdir -p build
$(CXX) -std=c++11 processA.cpp -o build/procA.out
procB: processB.cpp
mkdir -p build
$(CXX) -std=c++11 processB.cpp -o build/procB.out
clean:
rm -rf build
rm -f *.buff
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
using namespace std;
bool flag=false;
int x=0, y=0, counter=0;
string semaphore_buff = "semaphore.buff";
string x_buff = "x.buff";
string counter_buff = "counter.buff";
void init() {
ofstream s(semaphore_buff, s.binary);
if (!s.is_open()) {
exit(-1);
}
else {
s.write(reinterpret_cast<char*>(&flag), sizeof(flag));
s.close();
}
ofstream xs(x_buff, xs.binary);
if (!xs.is_open()) {
exit(-1);
}
else {
xs.write(reinterpret_cast<char*>(&x), sizeof(x));
xs.flush();
xs.close();
}
ofstream sc(counter_buff, sc.binary);
if (!sc.is_open()) {
exit(-1);
}
else {
sc.write(reinterpret_cast<char*>(&counter), sizeof(counter));
sc.flush();
sc.close();
}
}
int main()
{
int fd = open(semaphore_buff.c_str(), O_RDONLY);
init();
while (true) {
// obtain lock
int ret = flock(fd, LOCK_EX);
if (ret != 0)
continue;
fstream s(semaphore_buff, s.binary | s.in | s.out);
if (!s.is_open()) {
exit(-1);
}
else {
s.read(reinterpret_cast<char*>(&flag), sizeof(flag));
}
if (!flag) {
// semaphore flag not set
flag = !flag;
s.seekp(0);
s.write(reinterpret_cast<char*>(&flag), sizeof(flag));
s.flush();
fstream x_stream(x_buff, x_stream.binary | x_stream.in | x_stream.out);
if (!x_stream.is_open()) {
exit(-1);
}
else {
x_stream.read(reinterpret_cast<char*>(&x), sizeof(x));
}
fstream c_stream(counter_buff, c_stream.binary | c_stream.in | c_stream.out);
if (!c_stream.is_open()) {
exit(-1);
}
else {
c_stream.read(reinterpret_cast<char*>(&counter), sizeof(counter));
}
counter++;
x = x + 10;
y = y - 5;
flag = !flag;
printf("A :: counter=%d, x=%d, y=%d\n", counter, x, y);
x_stream.seekp(0);
x_stream.write(reinterpret_cast<char*>(&x), sizeof(x));
x_stream.flush();
c_stream.seekp(0);
c_stream.write(reinterpret_cast<char*>(&counter), sizeof(counter));
c_stream.flush();
s.seekp(0);
s.write(reinterpret_cast<char*>(&flag), sizeof(flag));
s.flush();
s.close();
x_stream.close();
c_stream.close();
}
// release lock
flock(fd, LOCK_UN);
// sleep
usleep(100 * 1000);
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
using namespace std;
bool flag;
int x, w, counter;
string semaphore_buff = "semaphore.buff";
string x_buff = "x.buff";
string counter_buff = "counter.buff";
int main()
{
int fd = open(semaphore_buff.c_str(), O_RDONLY);
while (true) {
// obtain lock
int ret = flock(fd, LOCK_EX);
if (ret != 0)
continue;
fstream s(semaphore_buff, s.binary | s.in | s.out);
if (!s.is_open()) {
exit(-1);
}
else {
s.read(reinterpret_cast<char*>(&flag), sizeof(flag));
}
if (!flag) {
// semaphore flag not set
flag = !flag;
s.seekp(0);
s.write(reinterpret_cast<char*>(&flag), sizeof(flag));
s.flush();
fstream x_stream(x_buff, x_stream.binary | x_stream.in | x_stream.out);
if (!x_stream.is_open()) {
exit(-1);
}
else {
x_stream.read(reinterpret_cast<char*>(&x), sizeof(x));
}
fstream c_stream(counter_buff, c_stream.binary | c_stream.in | c_stream.out);
if (!c_stream.is_open()) {
exit(-1);
}
else {
c_stream.read(reinterpret_cast<char*>(&counter), sizeof(counter));
}
counter++;
x = x - 2;
w = w + 3;
flag = !flag;
printf("B :: counter=%d, x=%d, w=%d\n", counter, x, w);
x_stream.seekp(0);
x_stream.write(reinterpret_cast<char*>(&x), sizeof(x));
x_stream.flush();
c_stream.seekp(0);
c_stream.write(reinterpret_cast<char*>(&counter), sizeof(counter));
c_stream.flush();
s.seekp(0);
s.write(reinterpret_cast<char*>(&flag), sizeof(flag));
s.flush();
s.close();
x_stream.close();
c_stream.close();
}
// release lock
flock(fd, LOCK_UN);
// sleep
usleep(100 * 1000);
}
return 0;
}
@say4n
Copy link
Author

say4n commented Sep 4, 2019

Executing ./procA.out and ./procB.out should produce:

$ ./procA.out
A :: counter=1, x=10, y=-5
A :: counter=2, x=20, y=-10
A :: counter=3, x=30, y=-15
A :: counter=4, x=40, y=-20
A :: counter=5, x=50, y=-25
A :: counter=6, x=60, y=-30
A :: counter=7, x=70, y=-35
A :: counter=9, x=78, y=-40
A :: counter=11, x=86, y=-45
A :: counter=13, x=94, y=-50
A :: counter=15, x=102, y=-55
A :: counter=17, x=110, y=-60
A :: counter=19, x=118, y=-65
A :: counter=21, x=126, y=-70
A :: counter=23, x=134, y=-75
A :: counter=25, x=142, y=-80
A :: counter=27, x=150, y=-85
A :: counter=29, x=158, y=-90
A :: counter=31, x=166, y=-95
A :: counter=33, x=174, y=-100
A :: counter=35, x=182, y=-105
A :: counter=37, x=190, y=-110
A :: counter=39, x=198, y=-115
A :: counter=41, x=206, y=-120
A :: counter=43, x=214, y=-125
A :: counter=45, x=222, y=-130
A :: counter=47, x=230, y=-135
A :: counter=49, x=238, y=-140
A :: counter=51, x=246, y=-145
A :: counter=53, x=254, y=-150
A :: counter=55, x=262, y=-155
A :: counter=57, x=270, y=-160
A :: counter=59, x=278, y=-165
A :: counter=61, x=286, y=-170
A :: counter=63, x=294, y=-175
A :: counter=65, x=302, y=-180
A :: counter=67, x=310, y=-185
A :: counter=69, x=318, y=-190
A :: counter=71, x=326, y=-195
A :: counter=73, x=334, y=-200
^C
$ ./procB.out
B :: counter=8, x=68, w=3
B :: counter=10, x=76, w=6
B :: counter=12, x=84, w=9
B :: counter=14, x=92, w=12
B :: counter=16, x=100, w=15
B :: counter=18, x=108, w=18
B :: counter=20, x=116, w=21
B :: counter=22, x=124, w=24
B :: counter=24, x=132, w=27
B :: counter=26, x=140, w=30
B :: counter=28, x=148, w=33
B :: counter=30, x=156, w=36
B :: counter=32, x=164, w=39
B :: counter=34, x=172, w=42
B :: counter=36, x=180, w=45
B :: counter=38, x=188, w=48
B :: counter=40, x=196, w=51
B :: counter=42, x=204, w=54
B :: counter=44, x=212, w=57
B :: counter=46, x=220, w=60
B :: counter=48, x=228, w=63
B :: counter=50, x=236, w=66
B :: counter=52, x=244, w=69
B :: counter=54, x=252, w=72
B :: counter=56, x=260, w=75
B :: counter=58, x=268, w=78
B :: counter=60, x=276, w=81
B :: counter=62, x=284, w=84
B :: counter=64, x=292, w=87
B :: counter=66, x=300, w=90
B :: counter=68, x=308, w=93
B :: counter=70, x=316, w=96
B :: counter=72, x=324, w=99
B :: counter=74, x=332, w=102
B :: counter=75, x=330, w=105
B :: counter=76, x=328, w=108
B :: counter=77, x=326, w=111
B :: counter=78, x=324, w=114
B :: counter=79, x=322, w=117
B :: counter=80, x=320, w=120
B :: counter=81, x=318, w=123
B :: counter=82, x=316, w=126
^C

Note: Since reads of the semaphore flag are not atomic, the above code doesn't guarantee exclusive access. It may so happen that both processes happen to read the semaphore flag as not set simultaneously and then proceed with their own logic. This can be avoided by using file locks while reading. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment