Process Cleanups Via Exit Handlers Registered Using atexit function

Any normal Unix process can terminate in following ways:

  • Graceful exit – Normal termination of process. For eg:
    • Returning from main, calling exit etc.
  • Ungraceful exit – Abnormal termination of process. For eg:
    • Abort signal. Due to some fatal error etc.

_exit, _EXIT and exit functions can be used to terminate the process normally. Usually exit functions do the cleanup but if needed a programmer can also define its own exit handler functions to do some specific cleanup actions needed by the program.

(more…)
Process Cleanups Via Exit Handlers Registered Using atexit function Read More

Find Minimum Operations Required To Convert String1 To String2

Write a program to find out the minimum number of operations required to convert source string to target string.
To convert source string to target string, only insert and delete character operation are allowed.
For eg:

  • Source = “ABCD”, Target = “ACDB” – Operations = 2 (Delete B and insert B at end)
  • Source = “ABCDEF”, Taget = “ACDEF” – Operations = 1 (Delete B)
(more…)
Find Minimum Operations Required To Convert String1 To String2 Read More

Find 3 Peak And Valley In An Array Separated By N Indices

Given an array of size n, we have to find 3 peak values and 3 trough values in the array such that each peak values are separated by another peak value by n entries.
Similarly, each trough value must be n days separated from the other trough values. For eg:

int arr[] = {1, 5, 6, 4, 3, 3, 10, 2, 8, 6, 11, 4, 10};
int distance = 2;

In above example, peak values are – 6, 10 and 11 (which are 2 indices apart).

valley values are – 1, 3 and 2 (which are 2 indices apart).

(more…)
Find 3 Peak And Valley In An Array Separated By N Indices Read More

Connected Graph Property Explained With Simple Example

Graph is a data structure which consists of a set of vertices which is called as Node, together with a set of collection of pair of vertices which is called as an Edge.
A graph data structure can be represented as a pair (V, E) where V is a set of nodes called vertices and E is a collection of pairs of vertices called edges. Graphs are used to solve many real-life problems such as fastest ways to go from A to B etc.

A Graph is called connected graph if each of the vertices of the graph is connected from each of the other vertices which means there is a path available from any vertex to any other vertex in the Graph.

Before going ahead have a look into Graph Basics.

(more…)
Connected Graph Property Explained With Simple Example Read More

Topological Sort Explained With Simple Example

Topological sort is a method to sort the vertices in directed acyclic graph in which each node comes before all the nodes to which it has edges going to. Topological sort is mainly used in cases where a certain node can be visited if and only if certain nodes has been visited before.

A Directed Acyclic graph or DAG is a graph which doesn’t have any cycle.

All pairs of consecutive vertices in topological sorted order are connected by edges which forms a directed Hamiltonian Path.

(more…)
Topological Sort Explained With Simple Example Read More

fork () Function Explained With Simple Example

Fork () is a system call in unix which can be used by a process to create a new process. This is a special function which is called once but it returns twice, one for parent process who called the fork () and second for the child process which is created by fork (). Fork () returns process id of the new process in parent process whereas it returns 0 for child process.

Syntax of fork () function call is as follows:

#include <unistd.h>
pid_t fork(void);   /* Returns: 0 in child, process ID of child in parent, −1 on error */
(more…)
fork () Function Explained With Simple Example Read More

Dynamic Memory Allocation Explained With Simple Example

Dynamic memory allocation is the process of allocating memory at run time by the program based on the need of the program. Dynamic Memory allocation is done from the heap memory available in the system and handling of memory (creation and deletion) is totally handled by the programmer. In case, programmer forgets to cleanup the allocated dynamic memory block, it will lead into memory leak and this memory block will be blocked for further use until program restarts.

Methods available to allocate dynamic memory at run time are

  • malloc () – allocates one memory block of requested size.
  • calloc () – allocates multiple blocks of memory and initialize it with 0.
  • realloc () – increases or decreases the size of previously allocated memory block.
  • free () – frees the memory allocated by above functions.
(more…)
Dynamic Memory Allocation Explained With Simple Example Read More