I am writing to, and reading from, a file called "results.dat", using std::fstream. This is done asynchronously, with the "read" and "write" function running in entirely different C++ programs. I want to check that the file is not currently being read before I write to it.
Suppose that my data is an array of 10 floats, then my "write" function looks like:
std::ofstream file_out("results.dat", std::ios::binary | std::ios::trunc);
file_out.write((char*)&data, 10 * sizeof(float));
file_out.close();
And my "read" function looks like:
std::ifstream file_in("results.dat", std::ios::binary);
file_in.read((char*)&data, 10 * sizeof(float));
file_in.close();
If the file "results.dat" is currently being read, I then want to make my "write" function wait, in a loop, until it is no longer being read. How can I implement this?
Use the LockFile() function on Windows or flock() for Linux/Unix.
For synchronous cases std::ios::tie.
The tie() is used to ensure that output from a tied stream appears before an input from the stream to
which it is tied. For example, cout is tied to cin:
cout << "Please enter a number: ";
int num;
cin >> num;
This code does not explicitly call cout.flush(), so had cout not been tied to cin, the user would see the request for input.
Here is another example:
#include <iostream> // std::ostream, std::cout, std::cin
#include <fstream> // std::ofstream
int main () {
std::ostream *prevstr;
std::ofstream ofs;
ofs.open ("test.txt");
std::cout << "tie example:\n";
*std::cin.tie() << "This is inserted into cout";
prevstr = std::cin.tie (&ofs);
*std::cin.tie() << "This is inserted into the file";
std::cin.tie (prevstr);
ofs.close();
return 0;
}
Further reading here.
For asynchronous cases using flock():
Example:
The lock file creation must be atomic. The pre-standard <fstream.h> library used to have an ios::noshare flag that guaranteed atomic file creation. Sadly, it was removed from the library, which superseded <fstream.h>. As a result, we are forced to use the traditional Unix file I/O interface declared in <fcntl.h> (under Unix and Linux) or <io.h> (Windows) to ensure an atomic operation.
Before a process can write to the data file, it should obtain a lock like this:
#include <fcntl.h> // for open()
#include <cerrno> // for errno
#include <cstdio> // for perror()
int fd;
fd=open("password.lck", O_WRONLY | O_CREAT | O_EXCL)
If the open() call succeeds, it returns a descriptor, which is a small positive integer that identifies the file. Otherwise, it returns -1 and assigns a matching error code to the global variable errno. The O_CREAT flag indicates that if the file doesn't exist, open() should create it. The O_EXCL flag ensures that the call is atomic; if the file already exists, open() will fail and set errno to EEXIST. This way you guarantee that only a single process at a time can hold the lock.
You check the return code of open() as follows:
int getlock() // returns the lock's descriptor on success
{
if (fd<0 && errno==EEXIST){
// the file already exist; another process is
// holding the lock
cout<<"the file is currently locked; try again later";
return -1;
}
else if (fd < 0){
// perror() appends a verbal description of the current
// errno value after the user-supplied string
perror("locking failed for the following reason");
return -1;
}
// if we got here, we own the lock
return fd;
}
Once a process owns the lock, it can write to the data file safely. When it has finished updating the file, it should delete the lock as follows:
remove("password.lck");
At this moment, the data file is considered unlocked and another process may access it.
or using: pthread_rwlockattr
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"
pthread_rwlock_t rwlock;
void *rdlockThread(void *arg)
{
int rc;
printf("Entered thread, getting read lock\n");
rc = pthread_rwlock_rdlock(&rwlock);
checkResults("pthread_rwlock_rdlock()\n", rc);
printf("got the rwlock read lock\n");
sleep(5);
printf("unlock the read lock\n");
rc = pthread_rwlock_unlock(&rwlock);
checkResults("pthread_rwlock_unlock()\n", rc);
printf("Secondary thread unlocked\n");
return NULL;
}
void *wrlockThread(void *arg)
{
int rc;
printf("Entered thread, getting write lock\n");
rc = pthread_rwlock_wrlock(&rwlock);
checkResults("pthread_rwlock_wrlock()\n", rc);
printf("Got the rwlock write lock, now unlock\n");
rc = pthread_rwlock_unlock(&rwlock);
checkResults("pthread_rwlock_unlock()\n", rc);
printf("Secondary thread unlocked\n");
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
pthread_t thread, thread1;
printf("Enter Testcase - %s\n", argv[0]);
printf("Main, initialize the read write lock\n");
rc = pthread_rwlock_init(&rwlock, NULL);
checkResults("pthread_rwlock_init()\n", rc);
printf("Main, grab a read lock\n");
rc = pthread_rwlock_rdlock(&rwlock);
checkResults("pthread_rwlock_rdlock()\n",rc);
printf("Main, grab the same read lock again\n");
rc = pthread_rwlock_rdlock(&rwlock);
checkResults("pthread_rwlock_rdlock() second\n", rc);
printf("Main, create the read lock thread\n");
rc = pthread_create(&thread, NULL, rdlockThread, NULL);
checkResults("pthread_create\n", rc);
printf("Main - unlock the first read lock\n");
rc = pthread_rwlock_unlock(&rwlock);
checkResults("pthread_rwlock_unlock()\n", rc);
printf("Main, create the write lock thread\n");
rc = pthread_create(&thread1, NULL, wrlockThread, NULL);
checkResults("pthread_create\n", rc);
sleep(5);
printf("Main - unlock the second read lock\n");
rc = pthread_rwlock_unlock(&rwlock);
checkResults("pthread_rwlock_unlock()\n", rc);
printf("Main, wait for the threads\n");
rc = pthread_join(thread, NULL);
checkResults("pthread_join\n", rc);
rc = pthread_join(thread1, NULL);
checkResults("pthread_join\n", rc);
rc = pthread_rwlock_destroy(&rwlock);
checkResults("pthread_rwlock_destroy()\n", rc);
printf("Main completed\n");
return 0;
}
Here are few useful resources: 1, 2 and 3.
Linux/Unix? Use a process-shared pthread_rwlock.
If both processes are running on the same machine, then you can use an interprocess mutex. Only one process at a time is allowed to interact with the data file (the resource).
Interprocess communication is different on all OSs, but the boost library provides a uniform interface for all common systems.
The code would look something like this:
writer:
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
using namespace boost::interprocess;
named_mutex mutex(open_or_create, "data_file_access_mutex");
void write_function() {
scoped_lock<named_mutex> lock(mutex);
std::ofstream file_out("results.dat", std::ios::binary | std::ios::trunc);
file_out.write((char*)&data, 10 * sizeof(float));
file_out.close();
}
reader:
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
using namespace boost::interprocess;
named_mutex mutex(open_or_create, "data_file_access_mutex");
void read_function() {
scoped_lock<named_mutex> lock(mutex);
std::ifstream file_in("results.dat", std::ios::binary);
file_in.read((char*)&data, 10 * sizeof(float));
file_in.close();
}
Related
I am trying to read some data from stdin in a separate thread from main thread. Main thread should be able to communicate to this waiting thread by writing to stdin, but when I run the test code (included below) nothing happens except that the message ('do_some_work' in my test code) is printed on the terminal directly instead of being output from the waiting thread.
I have tried a couple of solutions listed on SO but with no success. My code mimics one of the solutions from following SO question, and it works perfectly fine by itself but when coupled with my read_stdin_thread it does not.
Is it possible to write data into own stdin in Linux
#include <unistd.h>
#include <string>
#include <iostream>
#include <sstream>
#include <thread>
bool terminate_read = true;
void readStdin() {
static const int INPUT_BUF_SIZE = 1024;
char buf[INPUT_BUF_SIZE];
while (terminate_read) {
fd_set readfds;
struct timeval tv;
int data;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
tv.tv_sec=2;
tv.tv_usec=0;
int ret = select(16, &readfds, 0, 0, &tv);
if (ret == 0) {
continue;
} else if (ret == -1) {
perror("select");
continue;
}
data=FD_ISSET(STDIN_FILENO, &readfds);
if (data>0) {
int bytes = read(STDIN_FILENO,buf,INPUT_BUF_SIZE);
if (bytes == -1) {
perror("input poll: read");
continue;
}
if (bytes) {
std::cout << "Execute: " << buf << std::endl;
if (strncmp(buf, "quit", 4)==0) {
std::cout << "quitting reading from stdin." << std::endl;
break;
}
else {
continue;
}
}
}
}
}
int main() {
std::thread threadReadStdin([] () {
readStdin();
});
usleep(1000000);
std::stringstream msg;
msg << "do_some_work" << std::endl;
auto s = msg.str();
write(STDIN_FILENO, s.c_str(), s.size());
usleep(1000000);
terminate_read = false;
threadReadStdin.join();
return 0;
}
A code snippet illustrating how to write to stdin that in turn is read by threadReadStdin would be extremely helpful.
Thanks much in advance!
Edit:
One thing I forgot to mention here that code within readStdin() is a third party code and any kind of communication that takes place has to be on its terms.
Also, I am pretty easily able to redirect std::cin and std::cout to either fstream or stringstream. Problem is that when I write to redirected cin buffer nothing really appears on the reading thread.
Edit2:
This is a single process application and spawning is not an option.
If you want to use a pipe to communicate between different threads in the same program, you shouldn't try using stdin or stdout. Instead, just use the pipe function to create your own pipe. I'll walk you through doing this step-by-step!
Opening the channel
Let's create a helper function to open the channel using pipe. This function takes two ints by reference - the read end and the write end. It tries opening the pipe, and if it can't, it prints an error.
#include <unistd.h>
#include <cstdio>
#include <thread>
#include <string>
void open_channel(int& read_fd, int& write_fd) {
int vals[2];
int errc = pipe(vals);
if(errc) {
fputs("Bad pipe", stderr);
read_fd = -1;
write_fd = -1;
} else {
read_fd = vals[0];
write_fd = vals[1];
}
}
Writing a message
Next, we define a function to write the message. This function is given as a lambda, so that we can pass it directly to the thread.
auto write_message = [](int write_fd, std::string message) {
ssize_t amnt_written = write(write_fd, message.data(), message.size());
if(amnt_written != message.size()) {
fputs("Bad write", stderr);
}
close(write_fd);
};
Reading a message
We should also make a function to read the message. Reading the message will be done on a different thread. This lambda reads the message 1000 bytes at a type, and prints it to standard out.
auto read_message = [](int read_fd) {
constexpr int buffer_size = 1000;
char buffer[buffer_size + 1];
ssize_t amnt_read;
do {
amnt_read = read(read_fd, &buffer[0], buffer_size);
buffer[amnt_read] = 0;
fwrite(buffer, 1, amnt_read, stdout);
} while(amnt_read > 0);
};
Main method
Finally, we can write the main method. It opens the channel, writes the message on one thread, and reads it on the other thread.
int main() {
int read_fd;
int write_fd;
open_channel(read_fd, write_fd);
std::thread write_thread(
write_message, write_fd, "Hello, world!");
std::thread read_thread(
read_message, read_fd);
write_thread.join();
read_thread.join();
}
It seems like I have stumbled upon the answer with the help of very constructive responses from #Jorge Perez, #Remy Lebeau and #Kamil Cuk. This solution is built upon #Jorge Perez's extremely helpful code. For brevity's sake I am not including the whole code but part comes from the code I posted and a large part comes from #Jorge Perez's code.
What I have done is taken his approach using pipes and replacing STDIN_FILENO by the pipe read fd using dup. Following link was really helpful:
https://en.wikipedia.org/wiki/Dup_(system_call)
I would really appreciate your input on whether this is a hack or a good enough approach/solution given the constraints I have in production environment code.
int main() {
int read_fd;
int write_fd;
open_channel(read_fd, write_fd);
close(STDIN_FILENO);
if(dup(read_fd) == -1)
return -1;
std::thread write_thread(write_message, write_fd, "Whatsup?");
std::thread threadReadStdin([] () {
readStdin();
});
write_thread.join();
threadReadStdin.join();
return 0;
}
I'm trying to write a writer's preference code to prevent a writer from being starved in the event that it's in a queue and readers skip it due to their priority. The counter checking how many readers have read is protected by a semaphore (readerCount), a try semaphore is used to indicate a reader is trying to enter (psembufT), and the resource semaphore (psembufF).
I need to write to a text file (code written) in one Terminal window and read from the other in another window, whenever I try to read i get Segmentation fault [core dumped] error.
#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <inttypes.h>
using namespace std;
#define SHM_KEY 9876
#define SEMKEY 1234
struct sembuf vsembufR, psembufR, vsembufW, psembufW;
struct sembuf vsembufF, psembufF, vsembufT, psembufT;
int main()
{
union semun{
int val;
struct semid_ds *buf;
ushort myArray[0];
} arg;
string op;
ifstream myFile; // makes an ifstream object to read from myFile
int shmid = shmget(SHM_KEY, 256, 0777|IPC_CREAT);
int *readerCount = (int*)shmat(shmid, 0, 0);
int semid = semget(SEMKEY, 2, 0777|IPC_CREAT); // Creates two semaphores
int pause;
readerCount = 0;
psembufR.sem_num=0; // init reader mutex members
psembufR.sem_op=-1;
psembufR.sem_flg=SEM_UNDO;
vsembufR.sem_num=0;
vsembufR.sem_op=1;
vsembufR.sem_flg=SEM_UNDO;
psembufF.sem_num=1; // resource
psembufF.sem_op=-1;
psembufF.sem_flg=SEM_UNDO;
vsembufF.sem_num=1;
vsembufF.sem_op=1;
vsembufF.sem_flg=SEM_UNDO;
psembufW.sem_num=0; // writer
psembufW.sem_op=-1;
psembufW.sem_flg=SEM_UNDO;
vsembufW.sem_num=0;
vsembufW.sem_op=1;
vsembufW.sem_flg=SEM_UNDO;
psembufT.sem_num=1;
psembufT.sem_op=-1;
psembufT.sem_flg=SEM_UNDO;
vsembufT.sem_num=1;
vsembufT.sem_op=1;
vsembufT.sem_flg=SEM_UNDO;
arg.val = 1;
semctl(semid, 0, SETVAL, arg);
semctl(semid, 1, SETVAL, arg);
while(1){
cout << "Reader1:\n";
pause = getchar();
semop(semid, &psembufT, 1);
semop(semid, &psembufR, 1);
cout << "count inc" << endl;
*readerCount++;
if(*readerCount == 1) // is this first reader
semop(semid, &psembufF, 1); // lok resource from writers if 1st reader
semop(semid, &vsembufR, 1); // unlock reader mutex (for other readers)
semop(semid, &vsembufT, 1); // unlock try mutex (done accessing file)
// Critical Section
myFile.open ("myFile.txt", ios::out | ios::app); // ::app appends the myFile (new line)
if(myFile.is_open()){
while(getline(myFile, op)){
cout << op << endl; // reads
}
myFile.close();
}
semop(semid, &psembufR, 1); // lock reader mutex (avoid race)
*readerCount--;
if(*readerCount == 0) // is this the last reader
semop(semid, &vsembufF, 1); // unlock resource
semop(semid, &vsembufR, 1); // unlock reader mutex
}
I think it's something to do with how I declared readerCount but I can't figure it out.
readerCount = 0; should be *readerCount = 0;
readerCount is an int* so when you do readerCount = 0; you set that pointer to point at address zero. When you later try to update the value at that address, you are most likely to get crashes.
Lets say I have two processes (simulated in this example with two threads) in a producer-consumer set up. That is, one process writes data to a file, the other process consumes the data in the file, then clears said file.
The set up I currently have, based on bits and pieces I've thrown together from various resources online, is that I should use a lock file to ensure that only one process can access the data file at a time. The producer acquires the lock, writes to the file, then releases the lock. Meanwhile, the consumer waits for modify events with inotify at which point it acquires the lock, consumes the data, and empties the file.
This seems relatively straightforward, but the part that's tripping me up is that when I empty the file out in my consumer thread, it triggers inotify modify event again, which sets off the whole flow again, and ends with the data file being cleared again, thus repeating forever.
I've tried a few ways to work around this problem, but none of them seem quite right. I'm worried doing this wrong will introduce potential race conditions or I'll end up skipping modify events or something.
Here is my current code:
#include <fstream>
#include <iostream>
#include <string>
#include "pthread.h"
#include "sys/file.h"
#include "sys/inotify.h"
#include "sys/stat.h"
#include "unistd.h"
const char* lock_filename = "./test_lock_file";
const char* data_filename = "./test_data_file";
int AquireLock(char const* lockName) {
mode_t m = umask(0);
int fd = open(lockName, O_RDWR | O_CREAT, 0666);
umask(m);
bool success = false;
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
close(fd);
return -1;
}
return fd;
}
void ReleaseLock(int fd, char const* lockName) {
if (fd < 0) return;
remove(lockName);
close(fd);
}
void* ConsumerThread(void*) {
// Set up inotify.
int file_descriptor = inotify_init();
if (file_descriptor < 0) return nullptr;
int watch_descriptor =
inotify_add_watch(file_descriptor, data_filename, IN_MODIFY);
if (watch_descriptor < 0) return nullptr;
char buf[4096] __attribute__((aligned(__alignof__(inotify_event))));
while (true) {
// Read new events.
const inotify_event* event;
ssize_t numRead = read(file_descriptor, buf, sizeof(buf));
if (numRead <= 0) return nullptr;
// For each event, do stuff.
for (int i = 0; i < numRead; i += sizeof(inotify_event) + event->len) {
event = reinterpret_cast<inotify_event*>(&buf[i]);
// Critical section!
int fd = AquireLock(lock_filename);
// Read from the file.
std::string line;
std::ifstream data_file(data_filename);
if (data_file.is_open()) {
while (getline(data_file, line)) {
std::cout << line << std::endl;
}
data_file.close();
// Clear the file by opening then closing without writing to it.
std::ofstream erase_data_file(data_filename);
erase_data_file.close();
std::cout << "file cleared." << std::endl;
}
ReleaseLock(fd, lock_filename);
// Critical section over!
}
}
return nullptr;
}
int main(int argv, char** argc) {
// Set up other thread.
pthread_t thread;
int rc = pthread_create(&thread, NULL, ConsumerThread, nullptr);
if (rc) return rc;
// Producer thread: Periodically write to a file.
while (true) {
sleep(3);
// Critical section!
int fd = AquireLock(lock_filename);
// Write some text to a file
std::ofstream data_file(data_filename);
int counter = 0;
if (data_file.is_open()) {
std::cout << "Writing to file.\n";
data_file << "This is some example data. " << counter++ << "\n";
data_file.close();
}
ReleaseLock(fd, lock_filename);
// Critical section over!
}
pthread_exit(NULL);
return 0;
}
One idea I had was to disable tracking of modify events at the start of the consumer thread's critical section with inotify_rm_watch, then re-add it right before leaving the critical section. This doesn't seem to work though. Even with the events disabled, modify events are still getting triggered and I'm not sure why.
I've also considered just using a boolean to see if there was any file contents while consuming the file, and only clearing the file if it wasn't empty. This felt kind of hacky since it's still doing a second unnecessary iteration of the loop, but if I can't find a better solution I might just go with that. Ideally there would be a way to have only the producer thread's modifications trigger events, while the consumer could have it's own file modifications somehow ignored or disabled, but I'm not sure how to achieve that effect.
I have the following code that initializes a shared memory containing 1 mutex and 1 condition variable then forks a process where the parent passes to the child some characters through a pipe and signals the child to go an read it.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <pthread.h>
#ifndef _POSIX_THREAD_PROCESS_SHARED
#error This system does not support process shared mutex
#endif
pthread_cond_t *cvptr; //Condition Variable Pointer
pthread_condattr_t cattr; //Condition Variable Attribute
pthread_mutex_t *mptr; //Mutex Pointer
pthread_mutexattr_t matr; //Mutex Attribute
int shared_mem_id; //shared memory Id
int *mp_shared_mem_ptr; //shared memory ptr -- pointing to mutex
int *cv_shared_mem_ptr; //shared memory ptr -- pointing to condition variable
/* Read characters from the pipe and echo them to stdout. */
void read_from_pipe (int file)
{
printf("read_from_pipe()\n");
FILE *stream;
int c;
stream = fdopen (file, "r");
// Lock mutex and then wait for signal to relase mutex
printf("child mutex lock \n");
pthread_mutex_lock( mptr );
printf("child mutex locked\n");
printf("child wait\n");
pthread_cond_wait( cvptr, mptr );
printf("child condition woke up\n");
while ((c = fgetc (stream)) != EOF)
putchar (c);
fclose (stream);
printf("child mutex unlock\n");
pthread_mutex_unlock( mptr );
}
/* Write some random text to the pipe. */
void write_to_pipe (int file)
{
printf("write_to_pipe()\n");
FILE *stream;
stream = fdopen (file, "w");
fprintf (stream, "hello, world!\n");
fprintf (stream, "goodbye, world!\n");
fclose (stream);
pthread_cond_signal( cvptr );
}
int main (void)
{
int rtn;
size_t shm_size;
/* initialize shared memory segment */
shm_size = 1*sizeof(pthread_mutex_t) + 1*sizeof(pthread_cond_t);
if ((shared_mem_id = shmget(IPC_PRIVATE, shm_size, 0660)) < 0)
{
perror("shmget"), exit(1) ;
}
if ((mp_shared_mem_ptr = (int *)shmat(shared_mem_id, (void *)0, 0)) == NULL)
{
perror("shmat"), exit(1);
}
//Offset to find the location of the condition variable in the shared memory
unsigned char* byte_ptr = reinterpret_cast<unsigned char*>(mp_shared_mem_ptr);
byte_ptr += 1*sizeof(pthread_mutex_t);
mptr = (pthread_mutex_t *)mp_shared_mem_ptr;
cvptr = (pthread_cond_t *)byte_ptr;
// Setup Mutex
if (rtn = pthread_mutexattr_init(&matr))
{
fprintf(stderr,"pthreas_mutexattr_init: %s",strerror(rtn)),exit(1);
}
if (rtn = pthread_mutexattr_setpshared(&matr,PTHREAD_PROCESS_SHARED))
{
fprintf(stderr,"pthread_mutexattr_setpshared %s",strerror(rtn)),exit(1);
}
if (rtn = pthread_mutex_init(mptr, &matr))
{
fprintf(stderr,"pthread_mutex_init %s",strerror(rtn)), exit(1);
}
//Setup Condition Variable
if(rtn = pthread_condattr_init(&cattr))
{
fprintf(stderr,"pthread_condattr_init: %s",strerror(rtn)),exit(1);
}
if(pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED))
{
fprintf(stderr,"pthread_condattr_setpshared %s",strerror(rtn)),exit(1);
}
if(pthread_cond_init(cvptr, &cattr))
{
fprintf(stderr,"pthread_cond_init %s",strerror(rtn)),exit(1);
}
pid_t pid;
int mypipe[2];
/* Create the pipe. */
if (pipe (mypipe))
{
fprintf (stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}
/* Create the child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
printf ("Child Forked!.\n");
/* This is the child process.
Close other end first. */
close (mypipe[1]);
read_from_pipe (mypipe[0]);
return EXIT_SUCCESS;
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
else
{
printf ("Parent Forked!.\n");
/* This is the parent process.
Close other end first. */
close (mypipe[0]);
write_to_pipe (mypipe[1]);
return EXIT_SUCCESS;
}
}
I think I did something wrong with the initialization of the variables, the code does not core dump but somehow only prints:
Parent Forked!.
write_to_pipe()
Any ideas?
It's possible that write_to_pipe is signalling the condition variable before read_from_pipe reaches the pthread_cond_wait. Condition variables don't do any kind of buffering or counting of signals, so it will simply be lost.
For the application I'm developing (under Linux, but I'm trying to maintain portability) I need to switch to shared memory for sharing data across different processes (and threads inside processes). There is a father process generating different children
I need for example to get every process able to increment a shared counter using a named semaphore.
In this case everything is ok:
#include <sys/mman.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <fcntl.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define SEM_NAME "/mysem"
#define SM_NAME "tmp_sm.txt"
int main(){
int fd, nloop, counter_reset;
int *smo;
sem_t *mutex;
nloop = 100;
counter_reset = 1000;
if (fork() == 0) {
/* child */
/* create, initialize, and unlink semaphore */
mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
//sem_unlink(SEM_NAME);
/* open file, initialize to 0, map into memory */
fd = open(SM_NAME, O_RDWR | O_CREAT);
smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
/* INCREMENT */
for (int i = 0; i < nloop; i++) {
sem_wait(mutex);
cout << "child: " << (*smo)++ << endl;
if(*smo>=counter_reset){
(*smo)=0;
}
sem_post(mutex);
}
exit(0);
}
/* parent */
/* create, initialize, and unlink semaphore */
mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
sem_unlink(SEM_NAME);
/* open file, initialize to 0, map into memory */
fd = open(SM_NAME, O_RDWR | O_CREAT);
smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
/* INCREMENT */
for (int i = 0; i < nloop; i++) {
sem_wait(mutex);
cout << "parent: " << (*smo)++ << endl;
if(*smo>=counter_reset){
(*smo)=0;
}
sem_post(mutex);
}
exit(0);
}
So far so good: both semaphore and shared counter are ok (same address in memory) and increment and reset work fine.
The program fails simply by moving child source code into a new source file invoked by exec. Shared memory and named semaphore addresses are different therefore increment fails.
Any suggestion? I used named semaphores and named shared memory (using a file) to try to get the same pointer values.
UPDATE:
as requested by Joachim Pileborg, this is the "server side" improvements respect above original code:
...
if (fork() == 0) {
/* child */
/*spawn child by execl*/
char cmd[] = "/path_to_bin/client";
execl(cmd, cmd, (char *)0);
cerr << "error while istantiating new process" << endl;
exit(EXIT_FAILURE);
}
...
And this is the "client" source code:
#include <sys/mman.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <fcntl.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define SEM_NAME "/mysem"
#define SM_NAME "tmp_ssm.txt"
int main(){
int nloop, counter_reset;
int *smo;
sem_t *mutex;
/* create, initialize, and unlink semaphore */
mutex = sem_open(SEM_NAME, O_CREAT, 0777, 1);
//sem_unlink(SEM_NAME);
/* open file, initialize to 0, map into memory */
int fd = open(SM_NAME, O_RDWR | O_CREAT);
smo = (int *) mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
nloop=100;
counter_reset=1000;
/* INCREMENT */
for (int i = 0; i < nloop; i++) {
sem_wait(mutex);
cout << "child: " << (*smo)++ << endl;
if(*smo>=counter_reset){
(*smo)=0;
}
sem_post(mutex);
}
exit(0);
}
executing this code cause the process to block (deadlock) and waiting for an infinite time. looking at addresses they are tipically found to be:
father semaphore: 0x7f2fe1813000
child semahpore: 0x7f0c4c793000
father shared memory: 0x7f2fe1811000
child shared memory: 0x7ffd175cb000
removing 'sem_post' and 'sem_wait' everything is fine but I need mutual exlusion while incrementing...
Don't unlink the semaphore. it actually removes the semaphore.
From the sem_unlink manual page:
sem_unlink() removes the named semaphore referred to by name. The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it.
This means that once you've created the semaphore in the parent process, you immediately remove it. The child process then will not be able to find the semaphore, and instead creates a new one.