Linux - 使用互斥锁同步串口

标签 linux serial-port synchronization mutex

我正在为 Linux 操作系统编写 C 程序。
程序可以启动一个定时器:主程序和定时器都可以在串口上发送和接收字符。
我的尝试是通过在开头初始化的全局结构中的互斥锁来序列化串行端口访问:

if (pthread_mutex_init( &pED->lockSerial, NULL) != 0)
{   
    lwsl_err("lockSerial init failed\n");
}

我保护了所有在端口上发送数据的函数如下:

ssize_t cmdFirmwareVersion(EngineData *pED)
{
  if (pED->fdSerialPort==-1)
    return -1;

  LOCK_SERIAL;
  unsigned char cmd[] = { 0x00, 0x00, 0x7F };
  write( pED->fdSerialPort, cmd, sizeof(cmd));
  int rx = read ( pED->fdSerialPort, rxbuffer, sizeof rxbuffer);
  dump( rxbuffer, rx);
  UNLOCK_SERIAL;
  return rx;
}

在哪里

#define LOCK_SERIAL if (0!=pthread_mutex_lock(&pED->lockSerial)) {printf("Err lock");return 0;}
#define UNLOCK_SERIAL   pthread_mutex_unlock(&pED->lockSerial);

运行程序并启动计时器,我看到请求是有规律的。当我以其他方式(从 rx websocket 函数)触发其中一个调用时,程序挂起,我需要终止它。

为什么整个程序停止了??

最佳答案

如果进程挂起,可能是因为循环等待互斥量或持有互斥量并试图再次锁定它。这可能会导致死锁。

如果正在等待资源,ps 输出会将线程的状态显示为 D 或 S。它会在进程挂起时出现。

               D    uninterruptible sleep (usually IO)
               S    interruptible sleep (waiting for an event to complete)

我创建了一个线程来保存互斥量并尝试再次锁定它。 ps 输出和 GDB 显示主线程和子线程处于 sleep 状态。

xxxx@virtualBox:~$ ps -eflT |grep a.out
0 S root      3982  3982  2265  0  80   0 - 22155 -      20:28 pts/0    00:00:00 ./a.out
1 S root      3982  3984  2265  0  80   0 - 22155 -      20:28 pts/0    00:00:00 ./a.out

(gdb) info threads 
  Id   Target Id         Frame 
* 1    Thread 0x7ffff7fdf740 (LWP 4625) "a.out" 0x00007ffff7bbed2d in     __GI___pthread_timedjoin_ex (
    threadid=140737345505024, thread_return=0x0, abstime=0x0, block=    <optimized out>) at pthread_join_common.c:89
  2    Thread 0x7ffff77c4700 (LWP 4629) "a.out" __lll_lock_wait ()
    at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135

请查看博客Tech Easy有关线程的更多信息。

关于Linux - 使用互斥锁同步串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538662/

相关文章:

c - 将命令行 Linux 转换为 makefile

linux - 如何使用控制台访问和 saml 创建 aws iam 角色

php - $output 是否出错

c++ - 如何在成员函数线程中调用回调?

mysql - 如何同步多个 Fe​​dora 系统的时钟

linux - 如何从文件中的匹配行中提取子字符串?

c++ - 将整数转换为 BYTE 值

c# - 将许多变量从 Unity 发送到 Arduino

ios - 在NSURLSession之后同步执行方法

iphone - infoDictionary 内部版本号与 Plist 不同步