Last active
September 4, 2019 18:38
-
-
Save say4n/3c15d512b28c42a7337f7374e26b9294 to your computer and use it in GitHub Desktop.
semaphore implementation in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Executing
./procA.outand./procB.outshould produce: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.