Question

In c++ how would you alternate running between programs ran at the same time ran like...

In c++ how would you alternate running between programs ran at the same time ran like program1 & program1 using semaphores? The first program would cout something followed by the second program cout something.

Homework Answers

Answer #1
// C program to demonstrate working of Semaphores 
#include <stdio.h> 
#include <pthread.h> 
#include <semaphore.h> 
#include <unistd.h> 

sem_t mutex; 

void* thread(void* arg) 
{ 
        //wait 
        sem_wait(&mutex); 
        printf("\nEntered..\n"); 

        //critical section 
        sleep(4); 
        
        //signal 
        printf("\nJust Exiting...\n"); 
        sem_post(&mutex); 
} 


int main() 
{ 
        sem_init(&mutex, 0, 1); 
        pthread_t t1,t2; 
        pthread_create(&t1,NULL,thread,NULL); 
        sleep(2); 
        pthread_create(&t2,NULL,thread,NULL); 
        pthread_join(t1,NULL); 
        pthread_join(t2,NULL); 
        sem_destroy(&mutex); 
        return 0; 
} 
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
2. Alex and Charlie start running around a circular track at the same time. The track...
2. Alex and Charlie start running around a circular track at the same time. The track has a radius of 160 meters. Alex starts at the westernmost point and runs counterclockwise, taking 200 seconds to run one lap. Charlie runs clockwise at 3 meters per second and passes Alex for the first time after they have been running for 25 seconds. (a) From when he begins running, how long does it take Charlie to reach the northern- most point of...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
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...
How would the time comparison change if you dropped a 1-kilogram steel sphere from the same...
How would the time comparison change if you dropped a 1-kilogram steel sphere from the same location at the same time the bullet was fired?
You ran a study at your business examining how many new versus established employees joined your...
You ran a study at your business examining how many new versus established employees joined your company's retirement program based on 2 incentive plans. Your data look like this: Incentive 1 Incentive 2 New 40   70 Established   160 100 You want to know if the incentives were more/less effective for the groups of employees. Which group differed most from what was expected? A. Established/Incentive 2 B. New/Incentive 1 C. New/Incentive 2 D. Established/Incentive 1
The second meiotic division is similar to mitosis. How would you explain the difference between the...
The second meiotic division is similar to mitosis. How would you explain the difference between the first meiotic division and mitosis
. At a local university, the professor of sleep would like to know the mean time...
. At a local university, the professor of sleep would like to know the mean time a college student sleeps per nite. Ten randomly selected students were asked how long they slept at night. The mean time they slept was 7.1 hours, and the standard deviation was 0.78 hour. a) Find the best point estimate for the true mean time: ______________ b) Find the 95% confidence interval for the population mean: ______________________ c) A second professor believes that the mean...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
Convert your algorithm into a C++ program. Your program should output the value of the longest...
Convert your algorithm into a C++ program. Your program should output the value of the longest side of the right-angled triangle. Note, to answer the question, you will need to be able to do square root. To do square root, follow these 2 steps. 1) include the cmath library that allows square root using the statement #include <cmath> 2) To perform a square root, use the syntax sqrt(a double variable or number goes in here); For example cout << sqrt(25.0);   ...
Suppose that you are a researcher who would like to investigate the difference between sex influences...
Suppose that you are a researcher who would like to investigate the difference between sex influences class room participation. What type of research study would be best suited for this task, and how would you implement it?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT