Question

In: Computer Science

Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...

Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line.

For example: prompt$: ./vowcon Operating Systems Class at CSUN

The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can also send the phrase or its parts to the threads. The threads should print only the words of the phrase as follows:

The vow thread function should print all the words that start with a vowel

The con thread function should print all the words starting with a consonant.

Note that the main thread (running main()) should not print anything itself, the output should be printed by the threads that it creates. The main() can decide which thread to begin executing first, or it can leave it to the threads to sort this out. The order of words in the phrase should not be changed in the print. Your program should work for any phrase of any reasonable length, not just the one given in the example. The output of your program should look similar to the following

$ ./vowcon Operating Systems Class at CSUN

vow: Operating

con: Systems

con: Class

vow: at con: CSUN

In this part you are *not allowed* to use synchronization primitives such as semaphores, mutexes, mutex locks such as pthread_mutex_lock and pthread_mutex_unlock and conditional variables such as pthread_cond_wait and pthread_cond_signal etc. from pthreads library for thread coordination. You do not neet to use sched_yield() to relinquish control of the CPU, but you can use it if you like. You will have to may also investigate some of the other pthread functions available to you for this project, and use them if you like. Although you do not actually need them.

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;
int a=0;
vector<string> arr;
void vow(){
    for(int i=0;i<arr.size();i++){
        while(a == 0);
        if(arr[i][0] == 'a' or arr[i][0] == 'e' or arr[i][0] == 'i' or arr[i][0] == 'o' or arr[i][0] == 'u')
            cout<<"vow: "<<arr[i]<<endl;
        a = 0;
    } 
}
void con(){

    for(int i=0;i<arr.size();i++){
        while(a == 1);
        if(!(arr[i][0] == 'a' or arr[i][0] == 'e' or arr[i][0] == 'i' or arr[i][0] == 'o' or arr[i][0] == 'u'))
            cout<<"con: "<<arr[i]<<endl;
        a = 1;
    } 
}

int main(int argc , char* argv[]){
    
    for(int i=1;i<argc;i++){
        arr.push_back(string(argv[i]));
    }

    std::thread first(vow);
    std::thread second (con);
    
    first.join();
    second.join();

     
}


Related Solutions

Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
1. Using the pThread library, write a multithreaded program that calculates various statistical values for a...
1. Using the pThread library, write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85...
Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts,...
Write a computer program using C++ that computes the value of a Lagrange Polynomial and accepts, as input: (n+1) data points (x0, f(x0)), (x1, f(x1)),...(xn, f(xn)).. at value x, which the polynomial is to evaluated. As output, the program should produce a statement that reads, "f(x) = (the value of f(x))". As a test, use the data values (1,2);(-1,1);(0,0);(2,4);(-2,3)
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Write a C program without using if-else construct that does the following. It accepts a sequence...
Write a C program without using if-else construct that does the following. It accepts a sequence of positive integers between 1 and 9 both inclusive from the keyboard. The program will stop accepting input once an integer outside the range is entered. The program will finish by printing the total number multiples of 3 and total number of even integers entered. Test data and expected output: Enter integers between 1 & 9 both inclusive, outside range to stop Enter integer...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...
(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT