Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created July 26, 2012 14:40
Show Gist options
  • Save tanayseven/3182433 to your computer and use it in GitHub Desktop.
Save tanayseven/3182433 to your computer and use it in GitHub Desktop.
Bubble Sort using Fork System Call in linux
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid, p;
int n, *num, i, j, temp;
pid = getpid();
printf("Parent process executing\n");
printf("Enter the length of the array: ");
scanf("%d",&n);
num = (int*)malloc(sizeof(int)*n);
printf("Enter the array:\n");
for(i = 0 ; i < n ; i++)
scanf("%d",&num[i]);
p = fork();
if(p == 0 )
{
printf("Child process exucuting\n");
for(i = 0 ; i < n ; i++ )
{
for(j = 0 ; j < n-i-1 ; j++)
{
if(num[j] > num[j+1])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
printf("The sorted array is:\n");
for(i = 0 ; i < n ; i++)
{
printf("%d ",num[i]);
}
printf("\n");
}
return 0;
}
@tanayseven
Copy link
Author

OUTPUT:-
tanay@tanay-Inspiron-545:$ gcc -Wall -o bubble bubble.c
tanay@tanay-Inspiron-545:
$ ./bubble
Parent process executing
Enter the length of the array: 5
Enter the array:
5 3 4 1 2
Child process exucuting
The sorted array is:
1 2 3 4 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment