Question

program in C with one parent thread and two child threads. Both child threads work on...

program in C with one parent thread and two child threads.
Both child threads work on the single global variable sum. Each of them
implements one for loop, and the for loop is designed to complete 10000 iterations.

• One child thread add 1 to sum in each iteration of the for loop. starting with 0 and finishing with 10000.
• The other child thread, decrements on 1 the value of sum
in each iteration of the for loop.

Homework Answers

Answer #1

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
int sum=0;
// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(int vargp)
{
if(vargp==2){
for(int i=0;i<10000;i++){
sum=sum+1;
printf("Sum: %d",sum);
}

}
else if(vargp==3){
for(int i=0;i<10000;i++){
sum=sum-1;
printf("Sum: %d",sum);
}
}
}

int main()
{
pthread_t thread_id;
pthread_t thread_id1;
pthread_t thread_id2;
pthread_create(&thread_id, NULL, myThreadFun,1);
pthread_create(&thread_id1, NULL, myThreadFun,2);
pthread_create(&thread_id2, NULL, myThreadFun,3);
pthread_join(thread_id, NULL);
pthread_join(thread_id1, NULL);
pthread_join(thread_id2, NULL);
pthread_exit(NULL);
exit(0);
}

SUMMARY:

The above code implements multithreading in C. 3 threads have been created as parent, child1 and child2. We pass 1,2 and 3 as argument to the function while creating the thread. If the thread executing the function is 2 than it increments the global variable sum and if the thread executing is 3 than it decrements the global variable sum. The parent thread does not perform any function as it was not instructed.

Note: If you have any queries than let me know in the comments. If you find it helpful then a Like would be appreciated

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
A parent is now planning a savings program to put a daughter through college. She is...
A parent is now planning a savings program to put a daughter through college. She is 13 and plans to enroll in college in 5 years, and she should graduate 4 years later. Currently, the annual cost for college is $15,000 and is expected to increase 4% each year. The college requires that the costs be paid at the start (hint: beginning) of each year. The child now has $7,500 saved for college in an account and is expected to...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit,...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Following steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Your assignment is to write a c++ function that can calculate values for any 5 th...
Your assignment is to write a c++ function that can calculate values for any 5 th order polynomial function. ?(?) = ?0 + ?1? + ?2? 2+?3? 3 + ?4? 5 + ?5? 5 Your function should accept only two parameters, the first parameter should be the value of x at which to calculate y(x). The second parameter should be an array with 6 elements that contain the coefficients a0 to a5. The output of your function should be the...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT