Solved

Program getting too high of a priority

Posted on 2004-08-19
7
203 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
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now