/*
Tile: Program where parent process sorts array elements in descending order
and child process sorts array elements in ascending order.
*/
//#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
void ascending(int *a,int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nElements in ascending order..\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
void descending(int *a,int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nElements in descending order...\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
int main()
{
int *a,size,i,status;
pid_t pid,pid1;
printf("\nEnter the size of array\t");
scanf("%d",&size);
a = (int*)malloc(size * sizeof(int));
printf("\nEnter %d elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
pid = fork();
if(pid<0)
{
printf("\nError in process creation");
exit(-1);
}
else if(pid==0)
{
printf("\nChild Process");
printf("\nChild PID = %d",getpid());
ascending(a,size);
exit(0);
}
else
{
printf("\nParent process");
printf("\nParent PID = %d",getppid());
pid1 = wait(&status);
printf("\nPID1 = %d\nStatus = %d",pid1,status);
descending(a,size);
exit(0);
}
return 0;
}
Tile: Program where parent process sorts array elements in descending order
and child process sorts array elements in ascending order.
*/
//#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
void ascending(int *a,int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nElements in ascending order..\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
void descending(int *a,int size)
{
int i,j,temp;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nElements in descending order...\n");
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
int main()
{
int *a,size,i,status;
pid_t pid,pid1;
printf("\nEnter the size of array\t");
scanf("%d",&size);
a = (int*)malloc(size * sizeof(int));
printf("\nEnter %d elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
pid = fork();
if(pid<0)
{
printf("\nError in process creation");
exit(-1);
}
else if(pid==0)
{
printf("\nChild Process");
printf("\nChild PID = %d",getpid());
ascending(a,size);
exit(0);
}
else
{
printf("\nParent process");
printf("\nParent PID = %d",getppid());
pid1 = wait(&status);
printf("\nPID1 = %d\nStatus = %d",pid1,status);
descending(a,size);
exit(0);
}
return 0;
}
No comments:
Post a Comment