Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 268145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:39:23+00:00 2026-05-11T23:39:23+00:00

Currently the way /usr/sbin/alsa in Debian knows the processes using the sound card looks

  • 0

Currently the way /usr/sbin/alsa in Debian knows the processes using the sound card looks like:

echo $( \
    lsof +D /dev -F rt \
    | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' \
    | cut -c 2- \
    | uniq \
)

Which is rather ugly and depends on lsof. I am looking for a POSIX solution without lsof, perhaps using /proc.

    time for i in /proc/*/fd/*; do readlink $i | grep -q /dev/snd/pcm && echo $i | awk -F '/' '{print $3}'; done | uniq

Unfortunately seems to take twice as long as the lsof based snippet above. Can you make it faster, in order to make it a viable replacement?

Update I rewrote the above as:

#!/bin/sh
for i in /proc/[0-9]*/fd/*
do
        if readlink $i | grep -q /dev/snd/pcm
        then
                IFS=/; set -- $i; unset IFS; echo $3
        fi
done

But it seems to have the same performance as my earlier snippet. I suspect grep is the culprit.

Update: I have opened a Debian bug on the topic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T23:39:23+00:00Added an answer on May 11, 2026 at 11:39 pm

    You start a lot of processes here. Instead you can try doing in a similar way to the lsof script you posted… but replacing lsof by a shell for loop:

    If you want to avoid launching lots of grep processes, start only one:

    #!/bin/sh
    for i in /proc/[0-9]*/fd/*
    do
        echo ${i%/fd/*} $(readlink $i)
    done | grep -q /dev/snd/pcm
    

    This takes now 4.5s on my desktop, compared to 7.5s when there’s one grep process for each opened file.

    But… your grep is not necessary here, I think. If you care so much, you can try:

    #!/bin/sh
    for i in /proc/[0-9]*/fd/*
    do
        var="$(readlink $i)"
        if test x"$var" != x"${var#/dev/snd/pcm}"
        then
            echo $i
        fi
    done
    

    This is even faster for me (test is almost always a shell builtin), but I guess this is more because of bad testing methods. Try yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently the way I store strings is by using Localizable.strings, for example English: URL
I'm currently revamping the way our shop does version-control. We will be using a
This is the way I am currently doing it. <?php //show footer echo <script
Currently on my site I'm using statements like: include 'head.php'; include '../head.php'; include '../../head.php';
The way I understand gcc, /usr/bin/gcc (and other bits related to gcc, like ld)
Is there currently a way to subscribe to an event of a wrapped set,
I'm currently on my way to write a web application implementing the MVC and
Currently I'm checking it in the following way: if (Environment.UserInteractive) Application.Run(new ServiceControllerForm(service)); else ServiceBase.Run(windowsService);
Currently I am filtering incoming mails by a .forward in the following way: if
I am currently considering the best way of building a relatively complex page in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.