Mail Archives: cygwin/2011/10/20/04:22:35
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.
How to test:
Run the following code ;)
TestSharedMemProc is the process that should be spawned by TestSharedMem (yes, the name is misleading, but it's just a test...).
First I had shm_open under investigation for my problem, because sometimes I got a "Bad Address" (EFAULT, 14) error, but I couldn't reproduce it with a small program and I'm not sure if the problem is somehow connected to the serial port problem. In my original program I got it when opening the shared memory in the spawned (child) process. The shared memory was created in the parent process first and did exist in /dev/shm.
shm_open() returning EFAULT seems to be cygwin specific; what could have caused it? Maybe with a hint in the right direction I might be able to reproduce the error as well.
Thanks,
Manuel
/*
============================================================================
Name : TestSharedMem.c
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define COMPORT_NAME "/dev/com1"
#define SPAWN_FILE_NAME "./TestSharedMemProc.exe"
int serialportHandle = -1;
int SC_Close()
{
if( serialportHandle >= 0 )
{
fprintf(stdout, "Closing handle %d.\n", serialportHandle);
if( close(serialportHandle) < 0 )
{
fprintf(stdout, "Couldn't close COM-Port: %s :%d \n",sys_errlist[errno],errno);
return 3;
}
}
serialportHandle = -1;
return 0;
}
int SC_Open()
{
// Close any already open connections first
if( serialportHandle >= 0)
{
fprintf(stdout, "Open called before closing the port. Closing the port now.\n");
SC_Close();
}
// Call open to open a port
serialportHandle = open(COMPORT_NAME, O_RDWR | O_NONBLOCK );
if( serialportHandle < 0 )
{
fprintf(stdout, "Couldn't open COM-Port (%s): %s :%d \n",COMPORT_NAME,sys_errlist[errno],errno);
return 4;
}
fprintf(stdout, "Opened port %s (handle %d).\n", COMPORT_NAME, serialportHandle);
return 0;
}
int main(void) {
puts("TestSharedMem");
int error;
error = SC_Open();
if (error != 0)
{
printf("first open(COM port %s) failed.\n", COMPORT_NAME);
}
// Spawn
//int pid = spawnl(_P_WAIT, SPAWN_FILE_NAME, "TestSharedMemProc", NULL);
int pid = spawnl(_P_NOWAITO, SPAWN_FILE_NAME, "TestSharedMemProc", NULL);
if (pid == -1)
{
printf( "Failed to create process: %s %d\n", sys_errlist[errno],errno);
}
//sleep(2); // If the process closes before the next SC_Open, it works.
error = SC_Open();
if (error != 0)
{
printf("second open(COM port %s) failed.\n", COMPORT_NAME);
}
SC_Close();
puts("Done (TestSharedMem).");
return EXIT_SUCCESS;
}
/*
============================================================================
Name : TestSharedMemProc.c
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
puts("TestSharedMemProc");
sleep(1); // This process must be running when
// opening the COM port the second time
// -> maybe you need to increase the time
puts("Done (TestSharedMemProc).");
return 0;
}
--
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
- Raw text -