Wednesday, May 13, 2015

Exiting c program right after reading one line or after carriage return while reading input from stdin

Yesterday, we learnt about "C program that copies its input to its output one character at a time" , if you observe the program mentioned in this post, you will observe that we are taking the input from stdin (standard input) and outputting to stdout (each line) till EOF (End of the file) is reached.

Ideally stdin EOF is like forever. So, we end up pressing Ctrl+C to exit the program. Now to modify this program to exit right after one line or after a carriage return, we need to do a simple modification.
Replace EOF with '\n'. Yes its as simple as that.

 #include <stdio.h>  
 int main()  
 {  
   int c;  
   printf("Program exits after new line or carriage return \n");  
   printf("Start your input \n");  
   c = getchar();  
   while(c != '\n')  
   {  
     putchar(c);  
     c = getchar();  
   }  
 }  

Only difference you observe is in the while condition, "while(c != EOF) is changed to while(c != '\n')".

Output will be as below.

 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter Ctrl+C is to exit  
 Start your input   
 just one line input  
 just one line input  
Hope you understood this simple concept !!!

Tuesday, May 12, 2015

C program that copies its input to its output one character at a time

I have been blogging few interesting how-to articles which I learn in my daily professional life. Its been a while I haven't blogged any.So, thought of just adding atleast a program per day (very challenging isn't ?) along with how-to articles.

I will start with C-programming. As everyone know "Kernighan & Ritchie" (K&R) is the best book to learn C programming. I will using examples in this book.


If you want any C snippet meanwhile, just drop me a comment will try and get it done for you.  I cant promise, but will try my best to do code for you.


Frist lets start with "C program that copies its input to its output one character at a time"


 #include <stdio.h>  
 int main()  
 {  
   int c;  
   printf("Enter Ctrl+C is to exit\n");  
   printf("Start your input \n");  
   c = getchar();  
   while(c != EOF)  
   {  
     putchar(c);  
     c = getchar();  
   }  
 }  

Above program is exact copy of a program in K&R, I have just added two printf statements to it.

When you compile and run this program, it accepts whatever the character you input from the keyboard and print them once you press enter (new line / carriage return).

It does this task forever because we have used "While( c != EOF )" in our above code snippet.
This while statements will be true till you reach EOF of stdin (standard input) which is forever.

So, in order to exit from running your program, you need to press "Ctrl+C" on your keyboard together.

Now lets run and see, output will be as below
 mrtechpathi@mrtechpathi:~/Study/C/K_and_R$ ./a.out   
 Enter Ctrl+C is to exit  
 Start your input   
 we are  
 we are  
 trying to print  
 trying to print  
 character by character  
 character by character  
 ^C  
Hope you learnt something new :).

Set auto Indentation , shiftwidth and tab stop in Vim forever !!!

If you are looking for enabling auto indentation and to set tabstop to x characters, you are in right  place.

Open your .vimrc under your "/home/<user>" directory and add below lines
 filetype plugin indent on  
 set tabstop=4  
 set shiftwidth=4  
 set expandtab  
In above settings we have set 4 as tabstop and shiftwidth.
Now open a file with vim and your indentation and tabstop will work as expected.

Wednesday, April 15, 2015

How Linux Memory is Managed - Part 1

To make virtual memory concept simple, we need to remember few basic ground rules.

1. Linux visualizes its physical resource of memory
2. Process never ever directly address physical memory.
3. Instead Kernel associates each process with unique address space.

Memory Management happens in Linux using Page and Paging.

Kernel allocates unique Virtual Address Space to each Process. This Virtual Address Space is composed of Pages.

Two type of Pages we have
1. Valid  Page : Its associated with Physical Page or Secondary storage like disk.
2. Invalid Page : Its unused, unallocated piece of  Address space.

A program cannot use a page in secondary storage. It can use the page if it is associate with a page in physical memory.

Program -> Tries to access a Page on Secondary Storage -> MMU  generate Page fault->  Kernel does Page-in the desired page from secondary storage to Physical memory.

Imagine a Process tries to access a page on secondary storage. 

Since the page is not present, page fault occurs.

The Kernel intervenes and does Page-in the desired page to Physical memory from Secondary storage.


Interrupts and Interrupt handlers

If you are an embedded engineer, you might be dealing with interrupts in your daily work. Though its simple concept, its thought complex.

To put them in layman words, interrupts allow hardware to communicate with processor which in turn informs the Operating System to perform certain task.

Harware -> Interrupt -> Interrupt controller -> Processor -> Operating System ->  Serve Interrupt. 

Interrupt is physically produced by the hardware and directed into input pins on an Interrupt controller.

Once the interrupt is directed towards Interrupt controller, it informs the Processor.

Processor then notifies the underlying Operating System to handle the interrupt appropriately.

Operating system serves each Interrupt with unique handler and distinguishes each interrupt with its unique id.

Just remember above sequence, and it will help you during technical discussions.

Saturday, April 11, 2015

How to configure vi/vim for coding C - Indentation and file detection

How do we make vi-Vim not to use tab and use 4 spaces instead ?
How to make it  automatically indent code after curly brace blocks like Emacs does?
How to save these settings so that we never have to input them again?

I've seen other questions very often from vi/vim users.
 
Its pretty simple, add below lines to your .vimrc file under your home directory.

 filetype plugin indent on  
 set tabstop=4  
 set shiftwidth=4  
 set expandtab  

More information under :  filetype

Wednesday, April 8, 2015

Missing side bar, Missing Menu bar , Missing icons and Missing "close resize and minimize icons" in Ubuntu 14.04 64bit LTS

This issue was a nightmare. I have tried various solutions to solve this issue but nothing worked. I tried reinstalling unity, resetting unity, reinstalling compiz packages etc and ended up without solving the issue.

Many solutions available in net are for 12.04 , 12.10 etc versions but not for Ubuntu 14.04 64 bit version.

Before trying this solution, make sure you have taken backup of your valuable data. I didn't loose any data but on safe side, its always good to take backup before trying which we are not sure about it.

To solve this issue just change your directory to your home directory.

Press Ctrl+Alt+F1 , enter your username and password.

 cd <home-directory>  
 sudo rm -rf .config  
 sudo rm -rf .compiz/compiz-1  

That's it, reboot the machine and now you should see icons back on the screen :)