×

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

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

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.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.