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...
-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
(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 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 parallel program using Pthread based on given sequential solution. Please set the thread number...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code. Given Sequential Solution: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from...
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 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT