Posts

Showing posts from February, 2016

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 :    "...

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;              }   ...

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

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