#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include<stdlib.h>
void childProcess( int ar[ ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < n - 1 - i; j++ ) {
if ( ar[ j] > ar[ j+ 1 ] ) {
int temp = ar[ i] ;
ar[ i] = ar[ j] ;
ar[ j] = temp;
}
}
}
}
void parentProcess( int ar[ ] , int n) {
for ( int i = 0 ; i < n; i++ ) {
int j = i;
while ( j > 0 && ar[ j - 1 ] > ar[ j] ) {
int temp = ar[ i] ;
ar[ i] = ar[ j] ;
ar[ j] = temp;
j--;
}
}
}
int main( ) {
int n;
printf ( "Enter the number of elements: " ) ;
int ar[ 100 ] ;
printf ( "Enter the Numbers: \n " ) ; for ( int i
= 0 ; i
< n
; i
++ ) scanf ( "%d" , & ar
[ i
] ) ;
int p;
p = fork( ) ;
if ( p == - 1 ) {
printf ( "Error while creating a child\n " ) ; } else if ( p == 0 ) {
printf ( "We are in the child process to sort the array (Bubble Sort)\n " ) ; printf ( "Child Process ID: %d\n " , getpid
( ) ) ; printf ( "Parent Process ID: %d\n " , getppid
( ) ) ; childProcess( ar, n) ;
printf ( "After Sort in Child: \n " ) ; for ( int i = 0 ; i < n; i++ ) {
}
} else {
wait( 3000 ) ;
printf ( "We are in the parent process (Insertion sort)\n " ) ; printf ( "Parent => PID: %d\n " , getpid
( ) ) ; parentProcess( ar, n) ;
printf ( "After Sort in Parent: \n " ) ; for ( int i = 0 ; i < n; i++ ) {
}
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzeXMvd2FpdC5oPgojaW5jbHVkZSA8dW5pc3RkLmg+CiNpbmNsdWRlPHN0ZGxpYi5oPgoKdm9pZCBjaGlsZFByb2Nlc3MoaW50IGFyW10sIGludCBuKXsKICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKICAgIGZvcihpbnQgaiA9IDA7IGogPCBuIC0gMSAtIGk7IGorKyl7CiAgICAgIGlmKGF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