## An overview of the PC Real Time Clock (RTC)

### Introduction

Have you ever thought how the computer is able to display the correct time after you power on the system? There is a component called the RTC in the computer which stores this date and time information when the computer is turned off.

I will talk about how to read/write the date and time and use other features of the RTC using command-line tools, Linux RTC driver and also a low level direct access to the RTC internal registers.

## A generic Fisher-Yates Shuffle

It’s been a long time I have done any activity in this blog. I was going through some old stuffs and thought to post something. Today I will post a generic implementation for Fisher-Yates Shuffle.

Although you can get the Fisher-Yates algorithm from wiki, still I am briefly explaining it.

Let us assume that there is a array arr of length n. We need to find a uniformly random permutation of the elements of the array. One of the variations of the algorithm is as follows.

```arr is an array of length n, where indexing starts from 0
i=n-1
while i>0
{
r = generate a random number between 0 and i (both inclusive)
swap the array elements arr[r] and arr[i]
i = i - 1
}
arr is now uniformly permuted
```

## 2014 in review

This is pretty embarrassing. I promised to post several posts in last year and now ended up posting two posts in the last year, also none of those posts were actually a post. The worst year ever. Let’s see this year.

The WordPress.com stats helper monkeys prepared a 2014 annual report for this blog.

Here’s an excerpt:

The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 92,000 times in 2014. If it were an exhibit at the Louvre Museum, it would take about 4 days for that many people to see it.

## Blogging update

I promised (myself and you all) to post atleast 2 posts per month this year. Which clearly did not work at all. This is because this post is the second post after the “promise post”. What is going on then. The fact is I am not that much busy that I cannot put up a new post on the blog. The fact is that right now there are at least three different things I need to handle and this context switch every day or multiple times a week does not let me actually click the publish button (you all know how hard it is to click that button).

Recently (more than a year now) I was working on a cluster analysis algorithm and previously I was learning while working with some basic feed forward neural networks. Along with this I also keep in touch with *nix system programming and Linux OS kernel architecture (and a bit of Solaris recently). Therefore things are diverse. The job I do in the daytime (for which I get paid) is another domain (though similar to the system programming thing, but not exactly). Therefore the contexts are pretty diverse for me therefore the focus keeps shifting.

There are quite a few good ideas, for which there are drafts written, but as usual, it stays at on the disk. Why I am posting this? I am posting this because I need to press the “Publish” button and try again start the posting.

Therefore let’s see.

## Respect the ~ files

It has been a long time since a post in this blog. I thought to break this silence with a short post.

Several text editors in Linux environment like KWrite and GEdit that will store automatic temporary files ending with a ~ (tilde) character. Generally it is a default setting in most of the major distributions. Initially this feature annoyed me, as now you have twice the number of files in your directory, and often used rm *~ to clear out the files. Although it was annoying at the beginning, there are several cases it saved me. Continue reading

## 2013 in review

Like last year here are some stats of this blog for this year from wordpress.com. Worse this year, especially post count is 16, not enough compared to last year post count 24. Lesser visits this year, but some new relatively new posts have caught attention. Hoping to keep the post count at least 2 posts per month on an average on 2014.

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 75,000 times in 2013. If it were an exhibit at the Louvre Museum, it would take about 3 days for that many people to see it.

Posted in Writings | Tagged | 1 Comment

## Generate the process tree of a Linux system

This is a quick post on how to generate a process tree Linux (and *nix) operating systems.

The idea is the same, as in the previous posts: Finding overall and per core CPU utilization and Find process IDs of a running process by name. Read the information present in the /proc/ directory. To get which processes are running we can read the directories with numbers as their names in the /proc/ directory. To generate a process tree we need to establish a process child relationship within the running processes. Each process has a parent (the first generated process is an exception), and it is stored in the process table entry of that process. We need to fetch the parent process id for each running process inorder to establish the tree. Here’s the plan. Continue reading

## 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.

Posted in Computer Science, Linux Programming | | 3 Comments

## 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: http://en.wikipedia.org/wiki/Procfs. Let’s get in.

Posted in Computer Science, Linux Programming | | 7 Comments

## 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

Posted in C Language, Computer Science | Tagged , , | 8 Comments