Hallo Corinna, thanks for the response. Yes, you are right, I missed the fact that the child process inherits the file descriptors.
I took a look at the documentation of exec and just to be clear on the descriptor/handler issue: - file descriptor are passed down (basically always when calling the 'open' function) - socket descriptors are *not* passed down (not sure, wasn't in the documentation) - handles of semaphores, mutexes, memory maps, message queues are *not* passed down. - signal mask, scheduling policies (at leat when using SCHED_FIFO, SCHED_RR or SCHED_SPORADIC) and directory streams are passed down Is that correct? Thanks Manuel > -----Original Message----- > From: *** On Behalf Of Corinna Vinschen > Sent: Friday, October 21, 2011 12:50 PM > To: *** > Subject: Re: 1.7.9: spawn brakes reopening of serial port > > On Oct 20 10:19, Manuel Wienand wrote: > > Hallo, > > > > it seems that spawning a process brakes the reopening of a serial > port > > (when done during the execution of that process). The result of the > > open() function is "Permission denied (13)" in that case. > > I had a look into your testcase. What you see is expected behaviour. > > On Windows, a serial port can only be opened once at a time, for > exclusive access. You could have tried that without starting a second > process: > > open ("/dev/ttyS0", O_RDWR); // succeeds > open ("/dev/ttyS0", O_RDWR); // 2nd call fails with EACCES. > > Now I hear you say that you close the descriptor before trying to open > it the second time, but you're missing the fact that on spawn/exec the > child process inherits the open file descriptors from the parent > process. > So, even if you close the descriptor in the parent, it's still open in > the child. > > Apparently you don't want the child process to inherit this descriptor, > so you must set its "close-on-exec" flag. There are basically two ways > to do that > > - fd = open ("/dev/ttyS0", O_RDWR | O_CLOEXEC); > > - fd = open ("/dev/ttyS0", O_RDWR); > fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC); > > I guess it goes without saying that the close-on-exec flag has to be > set before calling spawn/exec. > > And, btw., please use /dev/ttyS0 rather than /dev/com1, etc. These are > the "official" names for the serial ports, while the "dev/comX" syntax > is only supported for backward compatibility. > > > Corinna > > -- > Corinna Vinschen Please, send mails regarding Cygwin > to > Cygwin Project Co-Leader cygwin AT cygwin DOT com > Red Hat > > -- > 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 > > > -- > This message has been scanned for viruses and > dangerous content by MailScanner, and is > believed to be clean.