Guest User

Untitled

a guest
Jan 8th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <cstring>
  2. #include <cerrno>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <iostream>
  6. #include <memory>
  7.  
  8. #include <fcntl.h>
  9. #include <sys/mman.h>
  10. #include <sys/random.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13.  
  14. using namespace std;
  15.  
  16. size_t n_children = 120;
  17.  
  18. typedef int64_t Konto;
  19.  
  20. Konto *konto = nullptr;
  21.  
  22. int main() {
  23. int fd = shm_open("/bank", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  24. if ((fd == -1) || (ftruncate(fd, sizeof(Konto)) == -1)) {
  25. cerr << strerror(errno) << endl;
  26. exit(EXIT_FAILURE);
  27. }
  28. konto = static_cast<Konto *>(mmap(nullptr, sizeof(Konto),
  29. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
  30. if (konto == MAP_FAILED) {
  31. cerr << strerror(errno) << endl;
  32. exit(EXIT_FAILURE);
  33. }
  34. *konto = 0;
  35.  
  36. while (fork() && --n_children)
  37. ;
  38.  
  39. unsigned int transaktionen = 100;
  40. while (transaktionen--) {
  41. int8_t value;
  42. if (getrandom(&value, sizeof(value), 0) == sizeof(value))
  43. *konto += value;
  44. }
  45.  
  46. cout << "Mein Kontostand betraegt: " << *konto << endl;
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment