简体   繁体   English

Linux C编程:对同一文件描述符的并发读/写

[英]Linux C Programming: Concurrent reads/writes to same file descriptor

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. 串行设备有两个通道,每个通道有一个硬件rx和tx缓冲器。 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. 读取是通过对设备进行iotctl调用来完成的,而写入是通过调用write()系统调用来完成的。

The main issue I have is with data throughput. 我遇到的主要问题是数据吞吐量。 I'd like to have an individual thread handle reading and writing for each channel (ie, 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. 从Linux的角度来看,设备上的所有内容都是通过一个设备访问的,我不确定Linux是否注意到该设备有多个通道。

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. 我想转到线程方法,但我想知道并发ioctl()和write()调用是否会导致问题。 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). 我理解read()和write()而不是线程安全,但我想知道是否有任何方法(可能两次调用open(),一个具有读权限,一个具有写权限)。

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. 有问题的器件是I2C UART桥。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM