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 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...
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);...
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...
VPython Physics 1211k 1. Here is a somewhat more sophisticated program that recreates one of your...
VPython Physics 1211k 1. Here is a somewhat more sophisticated program that recreates one of your experiments – a cart rolling up a ramp and back down. Particularly pay attention to the components of the acceleration. from visual import * #control the dimensions of the animation window. Turn off autoscaling. Then set the window to show 20 meters #in each direction. Then set the actual physical size of the animation window on the screen scene.autoscale = False scene.range = 20...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
Case Study Career Customization for Everyone: Deloitte’s Career/Life Program “ ‘Deloitte’ is the brand under which...
Case Study Career Customization for Everyone: Deloitte’s Career/Life Program “ ‘Deloitte’ is the brand under which tens of thousands of dedicated professionals in independent firms throughout the world collaborate to provide audit, consulting, financial advisory, risk management and tax services to selected clients. These firms are members of Deloitte Touche Tohmatsu Limited (DTTL), a UK private company limited by guarantee.” Like any other company, Deloitte seeks to be productive, to be profitable, and to growin short, to succeed. For Deloitte...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT