Posts

Showing posts from 2016

grep command tutorial

What is grep? grep stands for G lobally search a R egular E xpression and P rint. Created by Ken Thompson, the author of B Programming Language. It is a command-line utility to search a particular string in the files. Usage 1: Search a particular word in file To search a particular word in a file. $ grep <word> <file to search> Eg. grep hello words.txt The above command searches for word "hello" in the file "words.txt" and prints the matching lines in which the word "hello" is present in the file . The above command by default performs case sensitive search, this means if there is "Hello" present in the words.txt file it won't be detected by the search. Usage 2: Case Insensitive word search in file $grep -i <word> <file to search> Eg: grep -i hello words.txt The above command performs case insensitive search and gives you "hello"  "Hello" or other variants in the search. Usa

U.S. Visa B1/B2 Interview Experience

FingerPrint: July 28 2016 Hyderabad Reach there before half an hour . Items to Take: DS160 Confirmation Page Appointment Letter Passport You cannot carry mobile phone and purse as well Once you reach there, you have to stand in a queue. At the starting of the queue, a person will be verifying the name and passport number on your DS-160 Form as well as on the passport. Once matched, they will allow you inside. After going inside, there will be a security Check. And you have to go to the counter where they will again verify your documents and attach a sticker to the passport. They will give you a token number and allow you to go inside the other room. Check the television to find out your counter. Once you reach your counter, the person will ask you for your documents. Then ask for the purpose of the visit, and then ask you to read a paragraph present on the right side. After reading, they will take your photograph and then the fingerprints and then give

cron tutorial 2 logging

Image
We have seen in our first part of cron tutorial how to schedule a particular job at a specified time. In this part, we will see how we can perform cron logging By default the cron jobs are logged in /var/log/syslog file If you want the cron logs to store it in a separate log file ( /var/log/cron.log) . Uncomment the line that starts with #cron*. /etc/rsyslog.d/50-default.conf If there is no such file present in /etc/rsyslog.d, create 50-default.conf file and add the following entry cron*.    /var/log/cron.log Restart rsyslog by running the following command : sudo service rsyslog restart Now all the cron logs will be stored in the /var/log/cron.log file You can also redirect the output of individual cronjobs to their own log files . */1 * * * * root /home/jamal/test.sh >> /var/log/test.log If you want to log both standard output as well as errors then use the following syntax */1 * * * * root /home/jamal/test.sh >> /var/log/test.log 2>&am

What is a Linux Distribution

A Linux distribution also called as Linux distro is a set of packages that together make up a operating system.  Each of the package provides a specific function to the system. Example of packages include: Linux Kernel: provides operating system features for example memory management, process management, multitasking etc X Server and the desktop environment GNU Shell Utilities: the terminal Others include : System Libraries, web servers, databases, e-mail utilities and more. There are different distros for different purposes.  Desktop Environment: Ubuntu, Fedora Server: Red Hat Embedded Systems: Buildroot, OpenWRT Installing software in these distros varies as each of them uses a different packaging mechanism. For example, software in ubuntu comes in <sw>.deb and installing it uses dpkg package manager. deb stands for debian.

How to change the password in ubuntu

Image
There are two ways by which you can change your existing password: Command Line: Linux provides passwd utility to change the user password 1.Open terminal ( CTRL+ALT+T) 2. Type passwd 3. Enter your current password 4. Enter the new password which you want to set 5. Re-Enter the new password to confirm GUI 1. In the Dash Home , type "System Settings" and open it 2. Click on User Accounts present in the bottom right and then click on "Unlock" 3. Type your current password to unlock and now the password tab is editable. You can modify it by clicking on the password text box.

cron tutorial

Image
What is cron? cron is a daemon available in UNIX based systems which runs in background and automatically executes scripts or commands  at the specified time mentioned by the user. Example Scenarios where cron can be useful Connecting to the Internet and downloading E-Mail Cleaning up personal temporary directory How to check whether cron is running or not On  most of the Linux distributions, cron is already installed and is started by the startup scripts. jamal@jamal-Lenovo-G580:~$ ps ax| grep "cron"  1356 ?        Ss     0:00 cron  3793 pts/0    S+     0:00 grep --color=auto cron The top line shows that the cron is running and the bottom is the one we have searched How to schedule a job in cron There are different ways to achieve this: 1. User specific 2. System Level System Level: In the /etc, there are subdirectories called cron.hourly, cron.monthly, cron.weekly and cron.daily. Place your script in any of these director

Test your microphone in linux

First check the list of sound devices: cat /proc/asound/cards displays the list of sound cards present in the system In order to test the microphone use arecord command line utility arecord -d 10 /tmp/test.wav This command records your voice for 10 seconds In order to play the voice use following command aplay /tmp/test.wav

MultiThreading (pthread) Interview Question -2

#include <pthread.h> #include <stdio.h> #include <stdlib.h> int glob = 0; void *threadFunc(void *arg) {     int j;     int loops = *((int *) arg);     printf("\n %s: \n",__func__);     for (j = 0; j < loops; j++)     { glob++;     }     return NULL; } int main() {     pthread_t t1, t2;     int loops=1000000, ret;     ret = pthread_create(&t1, NULL, threadFunc, &loops);     if (ret != 0)         perror("Pthread Create :   ");     ret = pthread_create(&t2, NULL, threadFunc, &loops);     if (ret != 0) perror("Pthread Create:    ");     ret = pthread_join(t1, NULL);     if (ret != 0) perror("Pthread Join:     ");     ret = pthread_join(t2, NULL);     if (ret != 0) perror("Pthread Join :    ");     printf("glob = %d\n", glob);     return 0; } What is the expected output of the program? Ans: 2*loops If you say the above answer it is wrong, becau

Multithreading (pthread) Interview Question -1

Write a C Program to print even numbers in one thread and odd numbers in another thread? #include <stdio.h> #include <pthread.h> #include <semaphore.h> pthread_t oddThread,evenThread; sem_t semEven,semOdd; int counter = -1; void *oddFn(void *arg) { while (counter < 15) { sem_wait(&semOdd); printf("Counter:%d\n",++counter); sem_post(&semEven); } pthread_exit(NULL); } void *evenFn(void *arg) { while (counter < 15) { sem_wait(&semEven); printf("Counter:%d\n",++counter); sem_post(&semOdd); } pthread_exit(NULL); } int main(int argc, char *argv[]) { int ret; sem_init(&semEven, 0, 1); sem_init(&semOdd, 0, 0); ret = pthread_create(&evenThread, NULL, evenFn, NULL); if (ret != 0) { perror("Error in creating Thread\n"); exit(1); } ret = pthread_create(&oddThread, NULL, oddFn, NULL); if (ret != 0) { perror("Error in creatin

C program to allocate a 2-D array using malloc ( C Interview question - 5)

The question is to allocate a 2 dimensional array using malloc. It should be accesible similar to arrays as a[i][j] Before going to the code I will just remind the notation of a[i][j]= *(*(a + i) + j) #include <stdio.h> int main() {      int r = 3, c= 3;      int **arr=(int **)malloc(sizeof(int)*r);  //allocate memory for rows      int i = 0, j =0;      for ( i = 0; i < r; i++)      {              arr[i] = (int *)malloc(sizeof(int)*c); //allocate memory for columns       }       //store values       for (i = 0; i < r; i++)       {              for(j = 0; j<c;j++)             {                 arr[i][j]=i;              }        }        //print values         for (i = 0; i < r; i++)       {              for(j = 0; j<c;j++)             {                 printf("%d\n",arr[i][j]);              }        }       for ( i = 0; i <r; i++)       {              free(arr[i]); //free columns first        }         free(arr); /

Difference between dangling pointer and memory leak ( C Interview Question -4)

What is memory leak in C? Memory leak occurs when you allocated memory using malloc or calloc, but forget to free the allocated memory i.e., forget to call free() This will cause a serious issue in programs like daemons or servers which will be running for a long amount of time. Example: #include<stdio.h> void function() {     int *ptr = (int *)malloc(1000);     /* Perform Some work*/     /* Returned without calling free*/ } int main() {          while(1)          {                func();          }          return 0; } In order to avoid memory rule, you have to free the allocated memory after you complete your work of using it. In order to find out if there is any memory leak occurring in the program, you can use tools like valgrind. What is a dangling pointer? Dangling pointers are the pointers which are pointing to the memory location which is already freed. If you try to access that particular memory location, it may lead to segmentation faul

C program to print 1 to 100 without using any loop or conditional operator ( C Interview question - 3)

Write a C program to print numbers from 1 to 100? Conditions : 1 . You cannot use loops(for, while)                      2.  You cannot use conditional operator                      3.  No goto statement Solution : #include <stdio.h> void print() {       static int num = 0;       num++;       printf("%d\n", num);      if ( num < 100)         print(); } int main() {        print();        return 0; }

Storage Classes in C (C Interview Question - 2)

What is Storage Class: Storage class determines the scope and lifetime of the variable What is Scope? Scope or visibility tells you about the part of the program that can access the variable. What is lifetime? Each variable in C is assigned some physical location where it can store some value. The lifetime defines how long the value will be present in that particular location. A variable may have local lifetime or global lifetime. A variable that has global lifetime,its value will be persisted throughout the program, whereas a variable with local lifetime,value will persist in the block which it is defined. There are four storage classes in C 1. auto 2. register 3. static 4. extern 1. auto: Default storage class. If we don't specify any storage class  while defining the variable then by default storage class will be taken as auto Scope: Auto can only be used within functions. If you try to specify a global variable with auto storage class then you will get