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 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/ 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.
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].
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT