Question

In: Computer Science

Thread Programming (C Programming) Objective Develop threads: thread and main. Functionality of Threads: The main program...

Thread Programming (C Programming)

Objective

Develop threads: thread and main.

Functionality of Threads:

The main program accepts inputs from the user from the console. The user can provide the following two types of input:

  1. add num1 num2
  2. mult num1 num2

Here, num1 and num2 are two integer numbers.

E.g., the user may input add 1 2.

The threads receive the command along with the number, and performs the appropriate arithmetic operation and returns the results to main program. The main program then prints the results to the console. (The main program spawns 2 threads. The input from the user is then sent to the threads. One thread will do addition operation and the other one will do multiplication operation. The results will be sent back to the main program and print them out.)

Example of the threads usage.

Read Input Data for calculator Program

Create New Thread 0 for arithmetic operation.

Thread Started ID 0xb7ddab90

Lock Results for new operation:

Unlock Results after finishing new operation.

Result of add 1 and 2 =       3.00

Arithmetic operation Thread Stopped

Create New Thread 1 for arithmetic operation.

Thread Started ID 0xb75d9b90

Lock Results for new operation:

Unlock Results after finishing new operation.

Result of mult 3 and 4 =      12.00

Arithmetic operation Thread Stopped

Solutions

Expert Solution

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

void *sum_thread(void *arg)
{
    int *args_array;
    args_array = (int *)arg;
    int n1, n2, res;
    n1 = args_array[0];
    n2 = args_array[1];
    res = n1 + n2;
    int *p = (int *)malloc(sizeof(int));
    *p = res;
    pthread_exit(p);
}

void *mult_thread(void *arg)
{
    int *args_array;
    args_array = (int *)arg;
    int n1, n2, res;
    n1 = args_array[0];
    n2 = args_array[1];
    res = n1 * n2;
    int *p = (int *)malloc(sizeof(int));
    *p = res;
    pthread_exit(p);
}

int main()
{
    pthread_t tid_sum, tid_mult;
    char s[100];
    int input[2];
    scanf("%s", &s);
    scanf("%d", &input[0]);
    scanf("%d", &input[1]);
    void *res;
    if (s[0] == 'a' || s[0] == 'A')
    {
        pthread_create(&tid_sum, NULL, sum_thread, input);
        pthread_join(tid_sum, &res);
    }
    else
    {
        pthread_create(&tid_mult, NULL, mult_thread, input);
        pthread_join(tid_mult, &res);
    }
    int *output = (int *)res;
    printf("result of %s %d and %d = %d", s, input[0], input[1], *output);
    return 0;
}

Output:


Related Solutions

Write a C program that creates 5 threads sends the thread index as an argument to...
Write a C program that creates 5 threads sends the thread index as an argument to the thread execution procedure/function. Also, the main process/thread joins the newly created threads sequentially one after the other. From the thread procedure print “I am a thread and my index is “ [print the correct index number]. From the main thread after the join print “I am the main thread and just completed joining thread index “ [print the correct index].
Write a C code to let the main thread create N child threads, where each created...
Write a C code to let the main thread create N child threads, where each created thread will randomly generate an integer between 0 to 10 and put it into a global array variable. After that, the main thread will calculate the sum of all the generated integers. N should be input as a command line argument. Complete the following C code by filling all “???”s in the code sketch. NOTE: when you compile the code, you need to add...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT