Find process IDs of a running process by name

In this post I will talk about a procedure to find the process IDs of a running process by name, which can then be used to send signals or do other stuffs. For example if you have multiple instances of bash opened, this should be able to get you the list of process IDs (PIDs) of the bash instances.

Firstly, a shell utility is already available called pidof which is a part of the sysvinit-tools package. There are a whole bunch of tools in this package which lets you query PID based on different requirements, send signals to set of processes, etc. Just check out the stuff.

I will only mention the outline of how this is done and post the sourcecodes to do it. After that this can be extended to have many features just like the tools of sysvinit-tools package or more.
Continue reading “Find process IDs of a running process by name”

Finding overall and per core CPU utilization

Today I will post about how to monitor CPU usage by processor in Linux. As you might have expected this will simply access the CPU time information from /proc pseudo-filesystem and report the results.

The proc filesystem or the Procfs is a special filesystem which gives you a view into the kernel data. No files in procfs exists actually in the disk. There is no disk inodes and thus storage related to the files. Instead of going into procfs I will redirect you to wikipedia: Let’s get in.
Continue reading “Finding overall and per core CPU utilization”

C Q&A #6: An interesting difference between the C and C++ conditional operator

int main (void)
  int a = 5, b = 10, c;

  c = a > 3 ? b = 5 : a = 3;
  printf ("%d %d %d\n", a, b, c);
  //cout << a << " " << b << " " << c << endl;
  return 0;

Have a look at the above code, what will be the outcome if the code was in C language and if in C++ Language (replace with printf with cout)?

Directly going to the solution. Compilation error for C, and works perfect (no warnings) with C++. I saw this problem is a C question and answer book which didn’t bother to explain, so here is the explanation. Continue reading “C Q&A #6: An interesting difference between the C and C++ conditional operator”

Find pairs of numbers in an array with difference ‘k’

The problem statement is, an long array is given with n elements, we need to find all the pairs of numbers in this long array which have a constant difference k. I will post three methods. The first method is the brute force method and has a runtime complexity O (n2), the next method has runtime complexity O (n*log (n)), and the last one will have the runtime complexity O (n).

Continue reading “Find pairs of numbers in an array with difference ‘k’”

Jumble word solver again

I have already posted a jumbled word solver written in C language, although what I posted is actually become old, as I have changed some of the things in the code. I will update the post with this (hope to update!) with the new changes, but before it I would like to post the the same stuff in other languages and with different datastructure. Recently I am learning Perl and brushing up C++, therefore I will post jumble word solver written in C++ and Perl in this post.
Continue reading “Jumble word solver again”

Add vertical scrollbar in sourcecode blocks

Sometimes I post sourcecode accompanying my posts which are long and makes the length of the post unnecessarily long. The sourcecode shortcode has an option collapse which you can make “true” to collapse the sourcecode box when the page loads. If you post a long code, when the code box is expanded it will be inconvenient to scroll through the page and read. Also sometimes you might not want to collapse the codebox when the page loads. For this I have a solution. Add vertical bars to the codebox using the div tags.
In this way the length of the code portion in the page remains fixed and to read the code one requires just to scroll in the code box. Checkout what I am talking about in some of the code posts, for example in the posts here and here. Here is how it is done. Continue reading “Add vertical scrollbar in sourcecode blocks”

Implement queue using stack

The puzzle is to implement basic queue operations with using only basic stack operations. That is, a stack object is given, we need construct a wrapper for the queue functions, insert, remove, which will only use the stack object as its storage, and naturally will have to use the stack operations. I have already posted the opposite task in the post Implement stack using a queue

This can be done using two stack objects. We call these the first stack and the second stack. Although either the insert or the remove complexity will no more be O(1).

I have discussed the process gradually. I added the last solution when it clicked in my mind while reviewing this post.

Continue reading “Implement queue using stack”