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

-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 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 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 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...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT