Created
July 26, 2012 14:40
-
-
Save tanayseven/3182433 to your computer and use it in GitHub Desktop.
Bubble Sort using Fork System Call in linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OUTPUT:-
tanay@tanay-Inspiron-545:
$ gcc -Wall -o bubble bubble.c$ ./bubbletanay@tanay-Inspiron-545:
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