A program where parent process counts number of vowels in the given sentence and child process will count number of words in the same sentence. The above programs should use system calls like fork and wait.
Copyable Code:
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
fun_parent(char s[])
{
int p,t_vowel;
int a=0,e=0,i=0,o=0,u=0;
for(p=0;s[p];p++)
{
switch(s[p])
{
case 'a':
case 'A':a++;break;
case 'e':
case 'E':e++;break;
case 'i':
case 'I':i++;break;
case 'o':
case 'O':o++;break;
case 'u':
case 'U':u++;break;
}
}
printf("\nTotal A or a = %d",a);
printf("\nTotal E or e = %d",e);
printf("\nTotal I or i = %d",i);
printf("\nTotal O or o = %d",o);
printf("\nTotal U or u = %d",u);
t_vowel=a+e+i+o+u;
printf("\nTotal noumber of vowels in the sentence = %d\n",t_vowel);
}
fun_child(char s[])
{
int w=0,y=0,f=0;
while(s[y]!='\0')
{
while(s[y]==' ' && s[y]!='\0')
{
y++;
}
while(s[y]!=' ' && s[y]!='\0')
{
y++;
f=1;
}
if(f)
{
w++;
f=0;
}
}
printf("\nNumber of words in the sentence = %d\n",w);
}
int main()
{
pid_t newPID;
char s[100];
printf("\nEnter sentence: ");
gets(s);
newPID = fork();
if(newPID<0)
{
printf("\nError occured in process");
exit(-1);
}
else if(newPID==0)
{
fun_child(s);
exit(0);
}
else
{
execlp("a.out","vowelFork.c",fun_parent(s),NULL);
sleep(10);
wait();
exit(0);
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.