Web lists-archives.com

Re: problem with mmap and fork()




On Fri, 22 Feb 2019 at 17:01, Glyn Gowing <> wrote:
> I have a program (attached) that works correctly on my mac but does
> not work with Cygwin on Windows 10. I'm running the latest version of

> What happens in the buggy execution is that the child obtains a lock
> before the parent releases it. I'm using mmap and a pthread_mutex_t
> object along with fork(). Again, this exact code works correctly on my
> Mac running Mojave.

On further analysis, the call to
> pthread_mutexattr_setpshared(attr, PTHREAD_PROCESS_SHARED);
is failing. The error is EINVAL (22) Invalid argument.

This suggests that pthread mutexes cannot be shared between processes
by using shared memory in cygwin. I have not attempted to determine if
this is working as expected, or if this is a bug, or a limitation in
the Windows environment.

I have attached my files:
smtest2.c - my heavily modified version of OP's original.
smtest2.txt - output from running this version.showing error.
smtest3.c - my version which uses 2 pthreads in a single process
smtest3.txt - output from running this version, showing OP's expected results.

HTH
Doug
gcc -std=c11 -pthread -Wall -Wextra -Werror -o smtest2 smtest2.c && ./smtest2
22 Invalid argument
pthread_mutexattr_setpshared: No error
make: *** [Makefile:30: smtest2] Error 1

Attachment: smtest3.c
Description: Binary data

gcc -std=c11 -pthread -Wall -Wextra -Werror -o smtest3 smtest3.c && ./smtest3
1550925866 - both  : attr, lock, lock->next: 0x600000430 0x600000410 0x600000470
1550925866 - Child : checking  lock (0x600000470) - Before sleeping 5 seconds.
1550925866 - Parent: acquiring lock (0x600000470)
1550925866 - Parent: acquired  lock (0x600000470) - Before sleeping 10 seconds.
1550925871 - Child : acquiring lock (0x600000470) - After  sleeping 5 seconds.
1550925876 - Parent: unlocking lock (0x600000470) - After sleeping 10 seconds.
1550925876 - Parent: unlocked  lock (0x600000470) - Before sleeping 5 seconds to let child catch up.
1550925876 - Child : acquired  lock (0x600000470) - Before sleeping 3 seconds.
1550925879 - Child : unlocking lock (0x600000470) - After sleeping 3 seconds.
1550925879 - Child : unlocked  lock (0x600000470).
1550925881 -                                      - After  sleeping 5 seconds to let child catch up.

Attachment: smtest2.c
Description: Binary data

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple