Pergunta

I am writing a program that interfaces with a particular serial device. The serial device has two channels, and a hardware rx and tx buffer for each channel. Basically, at any given time , you can read/write to either channel on the device.

I am trying to read data from a channel, validate it (and perhaps use some of the data), and then transmit it. Reads are accomplished with iotctl calls to the device, while writes are accomplished with a call to the write() system call.

The main issue I have is with data throughput. I'd like to have an individual thread handle reading and writing for each channel (i.e., a read thread and write thread for each of the two channels). However, I have hit a snag. Everything on the device, from Linux's perspective is accessed via one single device, and I'm not sure that Linux notes that the device has multiple channels.

As a result, currently I open a single file descriptor to the device and perform my reads and writes serially. I'd like to go to the threaded approach, but I'm wondering if concurrent ioctl() and write() calls would cause issues. I understand that read() and write() and not thread safe, but I was wondering if there's any way around that (perhaps calling open() twice, one with read privileges, one with write privileges).

Thanks for your help. Also, I want to avoid having to write my own driver, but that may be an inevitable conclusion...

Also, as a side note, I'm particularly concerned that the device has extremely small hardware buffers. Is there any way to determine how much space the OS uses for a software buffer for data? That is, can I determine whether or not the OS has it's own buffer that is used to prevent overflow of the hardware buffer? The device in question is an I2C UART Bridge.

Foi útil?

Solução

You can use semaphore to make a mutual exclusion between read/write thread

sem_t sync_rw;

/*init semaphore */
err=sem_init(&sync_rw,0,1); /* shared between thread and initialized with 1 */
if( err != 0 )
{
    perror("cannot init semaphore \n");
    return -1;
}

in thread write function you do this :

sem_wait(&sync_rw);
write(...)
sem_post(&sync_rw);

same for thread reader :

sem_wait(&sync_rw);
iotctl(...)
sem_post(&sync_rw);

finally :

sem_destroy(&sync_rw);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top