×

Multithreading in C interview questions

Q1. What exactly is a thread?

Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process.

Q2. Can we create applications with several threads in C?

Ans: No, in contrast to Java, the C language standard does not support multithreading. The POSIX standard for threads is called POSIX Threads (or Pthreads). Only with the gcc compiler, pthread implementation is provided.

A C program to show multiple threads with global and static variables.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
 
// Make a global variable so we can modify it in threads.
int m = 0;
 
// The procedure that each thread will run
void *myThreadKam(void *vargp)
{
    // Specify the value of the thread's argument.
    int *myid = (int *)vargp;
 
    // To track changes in a static variable, let's construct one
    static int g = 0;
 
    // Change static and global variables
    ++g; ++m;
 
    // Print the argument, static and the global variables
    printf("Thread ID: %d, Static: %d, Global: %d\n", *myid, ++g, ++m);
}
 
int main()
{
    int i;
    pthread_t tid;
 
    // Let us create three threads
    for (i = 0; i < 3; i++)
        pthread_create(&tid, NULL, myThreadKam, (void *)&tid);
 
    pthread_exit(NULL);
    return 0;
}

Output

Q3. Describe multithreading.

Ans: Executing multiple threads at once is known as multithreading.

Q4. Why use multiple threads?

Ans: A common method for enhancing applications through parallelism is to use threads. For instance, many tabs in a browser could represent various threads. Multiple threads are used by MS Word; one thread is used to format the text, another to handle inputs, etc.

Q5. What are the thread's associated states?

Ans: State: ready, running, waiting, dead.

Q6. Explain about the Thread Life Cycle

Ans: A thread's life cycle is comparable to the processes that make up an operating system. Throughout its lifetime, the thread has the ability to change states at any time. But that depends on the procedure carried out on it.

Q7. What states are there in a thread?

Ans: New: A newly created thread is in its initial state. The thread enters the ready state after using some method regarding this. The thread scheduler then moves it to the runnable state.

Runnable: A thread that is prepared to run.

Running: A thread in running state is one that is actively working.

Blocked: This state has a blocked thread that is expecting a monitor lock. This can also happen when a thread changes states while performing an I/O operation.

Waiting: It is a thread that is holding up activity while another thread does the required action.

Timed_waiting: It's a thread that is awaiting action from another thread.

Terminated: This state describes a terminated thread.

Q9. What key distinctions exist between Thread and Process?

Ans: Process is a subset of the thread. There may be several threads in the process. The memory space that the process uses can vary, but all threads share the same memory area.

Q10. What is deadlock?

Ans: Deadlocks occur when two threads are waiting for object locks that one has already obtained while the other is waiting for a lock that the first thread already has.

Q11. What is LiveLock?

Ans: Due to the lack of necessary resources and the absence of unblocked threads, livelock occurs when all threads are blocked and unable to execute.

Q12. What does multi-busy threading's spin term mean?

Ans: Concurrent programmers use the busy spin technique to force a thread to wait until a specific condition is met. This differs significantly from conventional techniques like wait() and sleep(), which all involve giving up CPU control. Instead of turning off the CPU, this technique just executes the empty loop.

Q13. What does multi-threading context switch entail?

Ans: The process of saving and recovering CPU state is what it entails. This makes it easier to pick up thread execution from the same place at a later time. It is one of the key components of an operating system that supports multiple tasks at once and a multi-threaded environment.

Q14. Why does Thread behaviour fluctuate?

Ans: Because the execution of threads is dependent on the thread scheduler, we might conclude that thread behaviour is unpredictable. One should remember that each thread scheduler has a unique implementation on various operating systems, such as Windows, Unix, etc.

Q15. What does the term "Thread Priority" mean?

Ans: Each thread is given a priority. But in terms of execution, a greater priority also takes precedence. However, it also depends on how the OS implements the Thread Scheduler. Although the priority of the thread can be changed, there is no guarantee that a higher priority thread will be performed first.


Related Topics

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.