Solved

Program getting too high of a priority

Posted on 2004-08-19
7
215 Views
Last Modified: 2010-04-22
I have a packet sniffer that uses libpthread and libpcap. There is one while(1) loop in one thread, but I call a sleep(300) at the beginning of the loop. The next thread basically just sets up the callback for libpcap. I noticed that when doing a 'top' listing, my threads are all listed at the top of the list. I am using the default scheduling for the threads. I noticed that when doing a tcpdump, it almost never gets put at the top of the list. How can I reduce the priority of my threads without dropping packets?

-ryan
0
Comment
Question by:dignified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11849489
>How can I reduce the priority of my threads without dropping packets?

Unless you are facing some problems due to this issue ,i.e. unless it is affecting your functionality, I would suggest that you leave it alone.

Also, if you did not explicitly set the scheduling and priority of your threads, they have the same default priority as almost every other process.
0
 

Author Comment

by:dignified
ID: 11849551
That is what I thought too. I just don't understand why they would be having such a higher priority than tcpdump when they do almost the same thing. and use the same library.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11849560
It could be that tcpdump sets itself at a lower priority but most likely there is no priority isse involved here. What you see in top table is periodic snapshot and the sleeping task is anyway never in runnable queue so it will not execute at all !!! System may keep a tab on time at which to wake it up and updating a counter, but thats about all.
0
 
LVL 1

Expert Comment

by:DJSara
ID: 11913782
Instead of using 'sleep' in the while(1) loop, use 'select' or 'pselect' system call on the socket descriptor. That way you can make sure that you will not drop packets.
 It is not because of higher priority, but because of the infinite loop, your process uses a lot of CPU time even when there is no packets arriving and hence listed above tcpdump in top.  By using 'select' or 'pselect', your process will be blocked waiting for packets in the socket descriptor and hence won't utilize the CPU time.
0
 
LVL 2

Accepted Solution

by:
gysbert1 earned 500 total points
ID: 11987383
I you're using pcap (presumably opened with pcap_open_live), if you have your main loop as follows:
      const u_char *uchar;
      struct pcap_pkthdr packet;
      while ((uchar = pcap_next(pcap, &packet))) {
      //... process packet
      }
it does not use up any CPU unless you're getting LOTS of ethernet packets and doing heavy processing on them in that loop. pcap_next effectively does a select internally, so the call only returns when a packet arrives to 'wake' your process in the linux kernel process scheduler.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question