Tag Archives: C programming

Threads usage in C programming

Threads usage in C programming

If you want to write tables from two – five at the same time and using pencils and papers we need at least four writing hands ( four people), four pencils and four papers one for each to write a table. This method is called parallelism. In this method, we obtain the result in a short time. If one person does the same task it takes four times longer. (To understand threads)

In computer C programming, this process is called threads. By using these we get efficiency in programs to solve complex issues. In the Linux environment, POSIX threads have appeared. These are called pthreads and having a library named pthread.

Types of threads:

These are generally two types.

1. Joinable threads
2. Detachable threads

Joinable threads need to join them, whereas Detachable threads run their self. In every program main function, itself is the main thread.

To create pthread in C program we using phtread_create() function. In this function, it takes four arguments.

1. thread id
2. attribute
3. function to call
4. only argument to calling function

Example 1:

Here is an example C program to demonstrate joinable.

To compile below program
gcc thread.c -o thread -lpthread

To execute the program
./thread

/* thread.c */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void* second_thread(void* arg)
{
    int i;

    printf("second_table\n");
    for (i = 1; i <= 10; i++) {
        printf("2 X %d = %d\n", i, i*2);
        sleep(1);
    }
    pthread_exit(NULL);
}

void* third_thread(void* arg)
{
    int i;

    printf("\t\t\tthird_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t3 X %d = %d\n", i, i*3);
        sleep(1);
    }

    pthread_exit(NULL);
}

void* fourth_thread(void* arg)
{
    int i;

    printf("\t\t\t\t\t\tfourth_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t\t\t\t4 X %d = %d\n", i, i*4);
        sleep(1);
    }

    pthread_exit(NULL);
}

void* fifth_thread(void* arg)
{
    int i;

    printf("\t\t\t\t\t\t\t\t\tfifth_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t\t\t\t\t\t\t5 X %d = %d\n", i, i*5);
        sleep(1);
    }

    pthread_exit(NULL);
}

int main()
{
    pthread_t tid[4];

    printf("It is a pthread demo program!\n");
    pthread_create(&tid[0], NULL, second_thread, NULL);
    pthread_create(&tid[1], NULL, third_thread, NULL);
    pthread_create(&tid[2], NULL, fourth_thread, NULL);
    pthread_create(&tid[3], NULL, fifth_thread, NULL);

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_join(tid[2], NULL);
    pthread_join(tid[3], NULL);
    printf("Finished threds execution!\n");
    return 0;

}

The result is almost like this:

result of thread.c

result of thread.c For more information on the pthread_create function refer the below link

https://linux.die.net/man/3/pthread_create

Example 2:

Here is another program to demonstrate the detachable type.

To compile below program
gcc thread_detach.c -o thread_detach -lpthread

To execute the program
./thread_detach

/* thread_detach.c */

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void* second_thread(void* arg)
{
    int i;

    printf("second_table\n");
    for (i = 1; i <= 10; i++) {
        printf("2 X %d = %d\n", i, i*2);
        sleep(1);
    }
    pthread_exit(NULL);
}

void* third_thread(void* arg)
{
    int i;

    printf("\t\t\tthird_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t3 X %d = %d\n", i, i*3);
        sleep(1);
    }

    pthread_exit(NULL);
}

void* fourth_thread(void* arg)
{
    int i;

    printf("\t\t\t\t\t\tfourth_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t\t\t\t4 X %d = %d\n", i, i*4);
        sleep(1);
    }

    pthread_exit(NULL);
}

void* fifth_thread(void* arg)
{
    int i;

    printf("\t\t\t\t\t\t\t\t\tfifth_table\n");
    for (i = 1; i <= 10; i++) {
        printf("\t\t\t\t\t\t\t\t\t5 X %d = %d\n", i, i*5);
        sleep(1);
    }

    pthread_exit(NULL);
}

int main()
{
    pthread_t tid[4];
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    printf("It is a pthread detachble demo program!\n");

    pthread_create(&tid[0], &attr, second_thread, NULL);
    pthread_create(&tid[1], &attr, third_thread, NULL);
    pthread_create(&tid[2], &attr, fourth_thread, NULL);
    pthread_create(&tid[3], &attr, fifth_thread, NULL);

    sleep(10);
    printf("Finished threds execution!\n");
    return 0;

}

The result is almost like this:

thread_detach.c

For more information on the pthread_detach function refer the below link:

https://linux.die.net/man/3/pthread_detach

 

Compiling a C program using gcc in Linux

“This post explains about using gcc to compile C program on Linux”

Compile  C program using gcc:

  • What is a compiler:

Compiler is just like translator between programing language and machine language. It converts source written in programing language to executable instructions file for computer. For different programing languages different compilers are available. Compilers differs from operating system to operating system.

  • Open text editor:

Compiling C program start with a text editor to write our C program like VI . It is generally inbuilt in Linux operating systems. By opening terminal in our system we start from there.screenshot-from-2016-12-05-13-14-11

  • Write code:

In terminal type: vi sample.c  and Enter key. Then we enter in to vi text editor with our filename given. Now type a or i key to go in to insert mode. Then type our C program in it. After typing C program press Esc key,colon(:),w,q and Enter key  respectively to save and exit from VI. Here is figure showing source code.

screenshot-from-2016-12-05-13-18-02

  • Compile using gcc command:

Now we are in terminal again.Here we type ls command to see our saved file in the list. Then type gcc sample.c -o sample and enter key. Now the gcc compiler compiles our C file and gives the output as filename we given that is executable. screenshot-from-2016-12-05-13-20-29

  • Execute output file:

If there are any mistakes or errors in the program the compiler gives warnings and error messages with line number to find out them easily, after correcting them compile once again. If it compiles successfully it gives executable file.  To check that file we use ls command and see if it is. If it is, now type ./sample in terminal to execute it. we see the result on terminal.screenshot-from-2016-12-05-13-22-54

Actually in compiling process preprocessor adds the necessary files those are included in C libraries. That we are listed in first of our program like <stdio.h> and some other files also generated by compiler . One of  those file is object file.