# Sharing a serial port between two processes in Linux

**URL:** <https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910>\
**Category:** Factual Questions\
**Created:** [November 27, 2007, 3:13pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910 "2007-11-27T15:13:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [November 27, 2007, 3:13pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/1 "2007-11-27T15:13:21Z")

</div>

I’m working on a Linux project to route data between devices attached to a computing platform via serial lines. There is one app that maintains a socket and is responsible for writing socket data to the serial port. The other app reads the serial port, determines which socket(s) the date needs to go to, and sends the data strings on their respective merry ways. Each process, 2 to a port, is unidirectional.

When I was actively debugging, this worked quite well. Now that I’m trying to get it to work IRL for a demonstration, I’m having all kinds of trouble with port contention, and it’s not working. My frustration is mounting.

Anyone know how I can get this to work?

---

<div class="post-metadata">

**Author:** ![beowulff](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/beowulff/32/542_2.png) [@beowulff](https://boards.straightdope.com/u/beowulff)\
**Post date:** [November 27, 2007, 3:28pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/2 "2007-11-27T15:28:02Z")

</div>

Why is this two different apps?

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [November 27, 2007, 3:37pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/3 "2007-11-27T15:37:23Z")

</div>

[QUOTE=beowulff]  
Why is this two different apps?  
[/QUOTE]

It seems simpler in my mind to implement, along with a heaping helping of VMS experience where I could explicitely share the ports in this manner. It never occured to me that there were no similar options in Linux.

I’m mulling right now the mechanics of combining the two into one process without imposing significant latency in the end-to-end delivery times.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [November 27, 2007, 4:20pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/4 "2007-11-27T16:20:54Z")

</div>

Can’t you just have one application running two threads?

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [November 27, 2007, 4:54pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/5 "2007-11-27T16:54:28Z")

</div>

[QUOTE=Rysto]  
Can’t you just have one application running two threads?  
[/QUOTE]

That’s where I’m headed…

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [November 28, 2007, 1:42pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/6 "2007-11-28T13:42:39Z")

</div>

Different, but related, questions.

It appears to me that a large part of the problem is that the serial ports are left open when I kill the processes, and when I restart, the processes fail because they can’t open a zombie port.

Q1: Is there a way I can seize a zombie port and slam it shut during startup, and before I actually start the processes?

Q2: Is there a function similar to get\_pid() that can get me a pid based on a known name of a process?

---

<div class="post-metadata">

**Author:** ![Digital\_Stimulus](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Digital\_Stimulus](https://boards.straightdope.com/u/Digital_Stimulus)\
**Post date:** [November 28, 2007, 5:37pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/7 "2007-11-28T17:37:58Z")

</div>

[QUOTE=VunderBob]  
Q2: Is there a function similar to get\_pid() that can get me a pid based on a known name of a process?  
[/QUOTE]

We always used the _ps_ command (possibly with a “-Af” switch), piped through _grep_ to get a particular line, then picked out what was needed from that line (e.g., using _cut_).

It’s been awhile since I’ve done low-level serial port stuff, so I’m looking into it. If I find anything, I’ll post it, but I have to admit that my programming was much higher level – solutions concerning hardware were generally _ad-hoc_ and done using command-line scripts called from within a program. I’m not sure if this’ll help, but you might want to look at _setserial_ to close/reset the port (someone in our lab used it to figure out that the default settings to open a laser range finder was rendering it inoperable, then reset the port configuration). Perhaps that’ll give you a place to start looking, perhaps not.

---

<div class="post-metadata">

**Author:** ![Digital\_Stimulus](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Digital\_Stimulus](https://boards.straightdope.com/u/Digital_Stimulus)\
**Post date:** [November 28, 2007, 5:58pm UTC](https://boards.straightdope.com/t/sharing-a-serial-port-between-two-processes-in-linux/427910/8 "2007-11-28T17:58:49Z")

</div>

It looks like [socat](http://www.dest-unreach.org/socat/) might provide the functionality you need (descriptive article from [linux.com](http://linux.com) [here](http://www.linux.com/articles/61025)).

I’d never heard of it before, but it’s already installed on my Debian system (that is, I didn’t explicitly install it), so my guess is that it’s pretty widely available.
