I am developing a parallel port C++ application (on Ubuntu 18.04). Whenever a USB is plugged into the PC while the application is running, it causes one of the following ioctl calls to hang forever. This hang typically happens 2-5 seconds after the USB is inserted, or removed. It is always the ioctl (typically PPCLAIM) that hangs.

Can anyone see something that I am doing wrong in the following example, or is this something I will have to work around?

#include <iostream>
#include <thread>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <linux/parport.h>
#include <linux/ppdev.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>

bool terminate = false;

void native_poll_thread()
    // Create the port fd
    int fd = open("/dev/parport0", O_RDONLY, O_EXCL);

    // Claim and setup the port
    int ioctl_return = 0;
    ioctl_return != ioctl(fd, PPCLAIM);
    ioctl_return != ioctl(fd, PPWDATA, 0);
    ioctl_return != ioctl(fd, PPSETMODE, IEEE1284_MODE_BYTE);
    ioctl_return != ioctl(fd, PPDATADIR, 1);
    int flags = fcntl(fd, F_GETFL, 0);
    ioctl_return != fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    ioctl_return != ioctl(fd, PPRELEASE);
    assert(ioctl_return == 0);

        // Retrieve the data elements
        unsigned char data(0);
        unsigned char status(0);
        unsigned char control(0);
        ioctl_return |= ioctl(fd, PPCLAIM);
        ioctl_return |= ioctl(fd, PPRDATA, &data);
        ioctl_return |= ioctl(fd, PPRSTATUS, &status);
        ioctl_return |= ioctl(fd, PPRCONTROL, &control);
        ioctl_return |= ioctl(fd, PPRELEASE);
        assert(ioctl_return == 0);

        // Print status
        boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
        std::cerr <<  to_simple_string(now) << ": " << (int)data << ", " << (int)status << ", " << (int)control << std::endl;


void using_native()
    std::thread t(&native_poll_thread);


    terminate = true;

int main(int argc, char *argv[])


  • Is it a parallel-port providing USB thingy? Please edit question with the dmesg output after you put in the USB thingy, whatever it is. My best guess so far is that the USB thingy provides a new /dev/parport0, which renders the old filedescriptor invalid (but that's a wild guess). – dirkt Mar 14 at 8:52
  • It's actually a dedicated parallel port PCI-E card – S90210 Mar 14 at 13:05
  • If it's a PCI-E card, why do you write "USB" = universal serial bus? And I'm serious about the dmesg output - that's the first step to figure out what is going on. – dirkt Mar 14 at 13:53
  • Thanks for your suggestion. I meant that when I insert any other USB device (such as a memory stick) into the PC it causes the parallel port application to freeze. I will reword the question and add the dmesg as soon as I get back to my PC. – S90210 Mar 14 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.