Loading

Archive for 12 Jul 2012

C - Program for Fork using Orphan Process

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

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
*/

1 Comment

C Program for Fork to sort Numbers

C - Program for Fork to Sort Numbers in Ascending And Descending Order


#include<stdio.h>
#include<unistd.h>

int nos[10],n;
void ascending();
void descending();

int main(void)
{
  int pid;
  int i,j;
  printf("\nEnter the no of nos::");
  scanf("%d",&n);
  printf("\nEnter the Nos:");
  for(i=0;i<n;i++)
      scanf("%d",&nos[i]);
  pid=fork();
  if(pid==0)
  {
        printf("\nChild Process is Executing");
    printf("\nProcess ID = %d\nParent ID = %d",getpid(),getppid());
    ascending();
    printf("\n\n");
   }
   else
   {
    printf("\nParent Process is Executing");
    printf("\nProcess ID= %d\nParent ID= %d",getpid(),getppid());
    descending();
        printf("\n\n");
    }  


    return 0;
}


void ascending()
{
  int i,j,k,temp;
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
        if(nos[i]>nos[j])
        {
            temp=nos[i];
            nos[i]=nos[j];
            nos[j]=temp;  
        }
    }
   }

     printf("\n\nThe no in Ascending order are::\n");
         for(i=0;i<n;i++)
                printf("%d\t",nos[i]);

}

void descending()
{
  int i,j,k,temp;
  for(i=0;i<n;i++)
  {
        for(j=i+1;j<n;j++)
        {
                if(nos[i]<nos[j])
                {
                        temp=nos[i];
                        nos[i]=nos[j];
                        nos[j]=temp;
                }
        }
   }

    printf("\n\nThe no in Descending order are::\n");
        for(i=0;i<n;i++)
            printf("%d\t",nos[i]);
}
 
/*output-
[ketan@localhost ~]$ cc fork11.c
[ketan@localhost ~]$ ./a.out

Enter the no of nos::5

Enter the Nos:1 9 5 2 4

Child Process is Executing
Process ID = 2745
Parent ID = 2742

The no in Ascending order are::
1       2       4       5       9


Parent Process is Executing
Process ID= 2742
Parent ID= 2603

The no in Descending order are::
9       5       4       2       1
*/

Leave a comment