Question

In: Computer Science

write C program to create 4 threads for summing the numbers between 1 and 40 where...

write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following.
The sum from 1 to 10 = 55
The sum from 11 to 20 = 155
The sum from 31 to 40 = 355
The sum from 21 to 30 = 255
Thread 0: 55
Thread 1: 155
Thread 2: 255
Thread 3: 355
Total = 820
All programs must be run under the department’s Unix server.
To compile your code in Unix, you need to include pthread library as follows >> gcc Thread.c –pthread

Solutions

Expert Solution

Solution:

Calculating sum using Arithmatic Progression.

Sum of n terms in AP = (n/2)*(2*a + (n-1)*d)

here n = 10 , a is the start value , d = 1

On simplification............

Sum = 5 * ( 2*a + 9 )

Look at the code and comments for better understanding......

Screenshot of the code:

Output:

Code to copy:

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
void *calculateSum(void *args)
{
        //getting the start value
        int start=*((int *)args);
        int *sum=(int *)malloc(sizeof(int));
        //calculating the sum stating with value start
        // using the formula sum=5*(2*start+9)
        *sum = 5*(start*2+9);
        printf("The sum from %d to %d = %d\n",start,start+9,*sum);
        //returning the sum
        pthread_exit(sum);
}
int main()
{
        //creating 4 thread variables
        pthread_t t1,t2,t3,t4;
        //assingning the start value
        int start1=1,start2=11,start3=21,start4=31;
        //creating 4 threads
        pthread_create(&t1,NULL,calculateSum,(void *)&start1);
        pthread_create(&t2,NULL,calculateSum,(void *)&start2);
        pthread_create(&t3,NULL,calculateSum,(void *)&start3);
        pthread_create(&t4,NULL,calculateSum,(void *)&start4);
        //getting the sum value from thread and storing it in ret variable
        void *ret_value;
        pthread_join(t1,&ret_value);
        int ret1=*((int *)ret_value);
        pthread_join(t2,&ret_value);
        int ret2=*((int *)ret_value);
        pthread_join(t3,&ret_value);
        int ret3=*((int *)ret_value);
        pthread_join(t4,&ret_value);
        int ret4=*((int *)ret_value);
        //printing the thread return values
        printf("Thread 0 :%d\n",ret1);
        printf("Thread 1 :%d\n",ret2);
        printf("Thread 2 :%d\n",ret3);
        printf("Thread 3 :%d\n",ret4);
        //calculatin total value
        int total_sum=ret1+ret2+ret3+ret4;
        printf("Total = %d\n",total_sum);
}

I hope this would help.........................:-))


Related Solutions

{ /* Write a program in C++ to find the perfect numbers between 1 and 500....
{ /* Write a program in C++ to find the perfect numbers between 1 and 500. The perfect numbers between 1 to 500 are: 6 28 496*/    int sum = 0;    int i = 1;    int j = 1;    for (i ; i <= 100; i++)    {        for (j; j <= 100; j++)        {            if (j < i)//Line 55            {                  ...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
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...
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...
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
Write a multithreaded program that tests your solution to HW#1. You will create several threads –...
Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination). On UNIX and Linux systems, sleeping is...
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT