122k views
5 votes
Write a program that accepts a phrase of unspecified length on the command line. For example: prompt% letter print Operating Systems Class at Kent-State The main0 in this program has to create two threads running functions (vow and cons). The threads should take turns printing the respective words of the phrase supplied on the command line. The vow thread should print all the words that start with a vowel and the cons thread should print all words starting with a consonant. Note that the main thread (running main0) should not print anything itself-the output should be supplied by the threads that it creates. The order of words in the phrase should not be changed in the printout. 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 looks similar to the following prompt% letter print Operating Systems Class at Kent-State vow: Operating cons: Systems cons: Class vow: at cons: Kent-State In this part you are not allowed to use synchronization primitives such as mutexes for thread coordination. You can use sched yield0 to relinquish control of the CPU

User BiXiC
by
4.5k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

#define _MULTI_THREADED

#include <pthread.h>

#include <stdio.h>

#include <errno.h>

#define THREADS 2

int i=1,j,k,l;

int argcG;

char *argvG[1000];

void *threadfunc(void *parm)

{

int *num;

num=(int*)parm;

while(1)

{

if(i>=argcG)

break;

if(*num ==1)

if(argvG[i][0]=='a' ||argvG[i][0]=='2'||argvG[i][0]=='i' ||argvG[i][0]=='o' ||argvG[i][0]=='u')

{

printf("%s\\",argvG[i]);

i++;

continue;

}

if(*num ==2)

if(!(argvG[i][0]=='a' ||argvG[i][0]=='2'||argvG[i][0]=='i' ||argvG[i][0]=='o' ||argvG[i][0]=='u'))

{

printf("%s\\",argvG[i]);

i++;

continue;

}

sched_yield();

}

return NULL;

}

int main(int argc, char *argv[])

{

pthread_t threadid[THREADS];

int rc=0;

int loop=0;

int arr[2]={1,2};

argcG=argc;

for(rc=0;rc<argc;rc++)

argvG[rc]=argv[rc];

printf("Creating %d threads\\", THREADS);

for (loop=0; loop<THREADS; ++loop) {

rc =pthread_create(&threadid[loop], NULL, threadfunc,&arr[loop]);

}

for (loop=0; loop<THREADS; ++loop) {

rc = pthread_join(threadid[loop], NULL);

}

printf("Main completed\\");

return 0;

}

The below attached image is a sample output

Write a program that accepts a phrase of unspecified length on the command line. For-example-1
User Fery W
by
4.0k points