Loading

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

Bookmark the permalink.

Leave a reply