Solved

Program getting too high of a priority

Posted on 2004-08-19
7
206 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

18 Experts available now in Live!

Get 1:1 Help Now