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 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++)...
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....
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT