C- Program Fork using Orphan Process
1. Count no of words when child process is executing
2. Count no of vowels when parent process is executing
#include<stdio.h>
#include<unistd.h>
#include<string.h>
char s[50];
void count_words();
void count_vowels();
int main(void)
{
int pid;
int i,j;
printf("\nEnter a string::");
gets(s);
pid=fork();
if(pid==0)
{
printf("\nChild Process is Executing");
sleep(10);
printf("\nProcess ID = %d\nParent ID = %d",getpid(),getppid());
count_words();
printf("\n\n");
}
else
{
printf("\nParent Process is Executing");
printf("\nProcess ID= %d\nParent ID= %d",getpid(),getppid());
count_vowel();
printf("\n\n");
}
return 0;
}
void count_words()
{
int i,j,k;
int count=1;
for(i=0;i<strlen(s);i++)
{
if(s[i]==32)
{
count++;
}
}
printf("\nThe No of Words in String::%d",count);
}
void count_vowel()
{
int i,j,k;
int count=0;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
{
count++;
}
}
printf("\nThe No Vowels in String :: %d",count);
}
/*output-
[ketan@localhost ~]$ cc fork12.c
[ketan@localhost ~]$ ./a.out
#include<string.h>
char s[50];
void count_words();
void count_vowels();
int main(void)
{
int pid;
int i,j;
printf("\nEnter a string::");
gets(s);
pid=fork();
if(pid==0)
{
printf("\nChild Process is Executing");
sleep(10);
printf("\nProcess ID = %d\nParent ID = %d",getpid(),getppid());
count_words();
printf("\n\n");
}
else
{
printf("\nParent Process is Executing");
printf("\nProcess ID= %d\nParent ID= %d",getpid(),getppid());
count_vowel();
printf("\n\n");
}
return 0;
}
void count_words()
{
int i,j,k;
int count=1;
for(i=0;i<strlen(s);i++)
{
if(s[i]==32)
{
count++;
}
}
printf("\nThe No of Words in String::%d",count);
}
void count_vowel()
{
int i,j,k;
int count=0;
for(i=0;i<strlen(s);i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
{
count++;
}
}
printf("\nThe No Vowels in String :: %d",count);
}
/*output-
[ketan@localhost ~]$ cc fork12.c
[ketan@localhost ~]$ ./a.out
Enter a string::ketan
Parent Process is Executing
Process ID= 2701
Parent ID= 2603
The No Vowels in String :: 2
[ketan@localhost ~]$
Child Process is Executing
Process ID = 2703
Parent ID = 1
The No of Words in String::1
*/
Parent Process is Executing
Process ID= 2701
Parent ID= 2603
The No Vowels in String :: 2
[ketan@localhost ~]$
Child Process is Executing
Process ID = 2703
Parent ID = 1
The No of Words in String::1
*/
thanks for the code.........
ReplyDelete