Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <cerrno>
- #include <algorithm>
- #include <functional>
- #include <iostream>
- #include <memory>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <sys/random.h>
- #include <sys/types.h>
- #include <unistd.h>
- using namespace std;
- size_t n_children = 120;
- typedef int64_t Konto;
- Konto *konto = nullptr;
- int main() {
- int fd = shm_open("/bank", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
- if ((fd == -1) || (ftruncate(fd, sizeof(Konto)) == -1)) {
- cerr << strerror(errno) << endl;
- exit(EXIT_FAILURE);
- }
- konto = static_cast<Konto *>(mmap(nullptr, sizeof(Konto),
- PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
- if (konto == MAP_FAILED) {
- cerr << strerror(errno) << endl;
- exit(EXIT_FAILURE);
- }
- *konto = 0;
- while (fork() && --n_children)
- ;
- unsigned int transaktionen = 100;
- while (transaktionen--) {
- int8_t value;
- if (getrandom(&value, sizeof(value), 0) == sizeof(value))
- *konto += value;
- }
- cout << "Mein Kontostand betraegt: " << *konto << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment