Wednesday, 21 June 2017

Life is Not a Bed of Roses Neither Stones:


Life is neither bed of roses nor bed of thorns. A person is himself/herself responsible for making his or her life either the bed of roses or the bed of thorns. Success in every field of life never comes to you on its own. You have to strive hard to get to it. As an example of climbing the ladder says to reach the top you have to climb every single step of the ladder, same as the case with the success of life. Success is not the ladder which can be climbed with your hands in the pocket.
Happy and sad moments are the part of life. If sad moments are not faced, one can never realise the worth of the happy moments. Adversity is that great part of life which leads you towards the successful future. Adversity makes you stronger and enough capable to bear the entire crisis you face through the different stages of life in order to make you stronger both mentally and physically. Hard times are like washing machines, they twist, turn and knock us around but in the end we come out cleaner, brighter and better than before.
Life never waits for anyone, you have to come up and get it.
Never think that, If your are facing pain or unsuccessful in life It doesn't mean useless. As we say after every dark night there is a morning that comes up with full of light that brightens up your day. If you face crises than think that the success is making its way to you. All you have to do is to urge harder to get through all the situations which surely will bring you closer to a better future. Never lose the hope as we say tomorrow never dies and always remember beautiful pictures are always developed from the negatives in the dark room.

Have a Great Day!
Thejeswara Reddy R









(copied from: Link)


Friday, 9 June 2017

Multithreading in C


What is a Thread? 
A thread is a single sequence stream within in a process. Because threads have some of the properties of processes, they are sometimes called lightweight processes.
What are the differences between process and thread?
Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section and OS resources like open files and signals. But, like process, a thread has its own program counter (PC), a register set, and a stack space.
Why Multithreading?
Threads are popular way to improve application through parallelism. For example, in a browser, multiple tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs, etc.
Threads operate faster than processes due to following reasons:
1) Thread creation is much faster.
2) Context switching between threads is much faster.
3) Threads can be terminated easily
4) Communication between threads is faster.
Can we write multithreading programs in C?
Unlike Java, multithreading is not supported by the language standard. POSIX Threads (or Pthreads) is a POSIX standard for threads. Implementation of pthread is available with gcc compiler.
A simple C program to demonstrate use of pthread basic functions
Please not that the below program may compile only with C compilers with pthread library.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
// A normal C function that is executed as a thread when its name
// is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}
  
int main()
{
    pthread_t tid;
    printf("Before Thread\n");
    pthread_create(&tid, NULL, myThreadFun, NULL);
    pthread_join(tid, NULL);
    printf("After Thread\n");
    exit(0);
}
In main() we declare a variable called thread_id, which is of type pthread_t, which is an integer used to identify the thread in the system. After declaring thread_id, we call pthread_create() function to create a thread.
pthread_create() takes 4 arguments.
The first argument is a pointer to thread_id which is set by this function.
The third argument is name of function to be executed for the thread to be created.
The fourth argument is used to pass arguments to thread.
The pthread_join() function for threads is the equivalent of wait() for processes. A call to pthread_join blocks the calling thread until the thread with identifier equal to the first argument terminates.
How to compile above program?
To compile a multithreaded program using gcc, we need to link it with the pthreads library. Following is the command used to compile the program.
gfg@ubuntu:~/$ gcc multithread.c -lpthread
gfg@ubuntu:~/$ ./a.out
Before Thread
Printing GeeksQuiz from Thread 
After Thread
gfg@ubuntu:~/$ 
A C program to show multiple threads with global and static variables
As mentioned above, all threads share data segment. Global and static variables are stored in data segment. Therefore, they are shared by all threads. The following example program demonstrates the same.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
// Let us create a global variable to change it in threads
int g = 0;
 
// The function to be executed by all threads
void *myThreadFun(void *vargp)
{
    // Store the value argument passed to this thread
    int myid = (int)vargp;
 
    // Let us create a static variable to observe its changes
    static int s = 0;
 
    // Change static and global variables
    ++s; ++g;
 
    // Print the argument, static and global variables
    printf("Thread ID: %d, Static: %d, Global: %d\n", myid, ++s, ++g);
}
 
int main()
{
    int i;
    pthread_t tid;
 
    // Let us create three threads
    for (i = 0; i < 3; i++)
        pthread_create(&tid, NULL, myThreadFun, (void *)i);
    
    pthread_exit(NULL);
    return 0;
}
gfg@ubuntu:~/$ gcc multithread.c -lpthread
gfg@ubuntu:~/$ ./a.out
Thread ID: 1, Static: 1, Global: 1
Thread ID: 0, Static: 2, Global: 2
Thread ID: 2, Static: 3, Global: 3
gfg@ubuntu:~/$ 
Please note that above is simple example to show how threads work. Accessing a global variable in a thread is generally a bad idea. What if thread 2 has priority over thread 1 and thread 1 needs to change the variable. In practice, if it is required to access global variable by multiple threads, then they should be accessed using a mutex.