Thread Synchronization – Mutex Explained With Simple Example

A Mutex is basically a lock that is locked before accessing any critical section of code to ensure only one thread at a time can access it. Mutex is unlocked when the thread is done processing. Thus, mutex can also be thought as “Mutually exclusive flag” to ensure all other threads are blocked to a certain section of code when one thread is already accessing it.

(more…)
Thread Synchronization – Mutex Explained With Simple Example Read More

Inter Process Communications – Pipes Explained With Simple Example

Pipes are one of the oldest and simplest form of Inter-Process communications. Typically, a pipe is half-duplex way of transmitting data between two processes which means data can be passed in one direction only. It can be used only between processes having common parent.
Normally pipes are used between parent and child processes.

(more…)
Inter Process Communications – Pipes Explained With Simple Example Read More

Difference Between Process and Threads

A program in execution is called a process. There can be multiple instances of a program running in the system which are identified by a unique id called process id. Operating System ensures that each process has a unique id which is always a non-negative integer number.

A thread is a lightweight process which can only be used inside a process. Like process, thread also has a unique identifier to identify threads which are called thread ids. Unlike process ids, thread ids are only unique to a process and one process threads are unknown to other process.

(more…)
Difference Between Process and Threads Read More

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

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