Factorial calculation using fork() in C for Linux
Write a Unix C program using the fork() system call that generates the factorial and gives a sequence of series like 1, 2, 6, 24, 120… in the child process.
The number of the sequence is provided in the command line.
Examples:
Input :gfg@ubuntu:~/$ gcc -o fork fork.c gfg@ubuntu:~/$ ./fork 6 Output :1 1 2 1 2 6 1 2 6 24 1 2 6 24 120 1 2 6 24 120 720 After deletion sum 1 3 9 33 153 873 Done Input :gfg@ubuntu:~/$ gcc -o fork fork.c gfg@ubuntu:~/$ ./fork -2 Output :negative number entered -2
To create child process fork()is used. fork() returns :
- < 0 fail to create child (new) process
- = 0 for child process
- > 0 i.e process ID of the child process to the parent process. When >0 parent process will execute.
Inside Child process: If input is 6, the first six numbers of the factorial sequence will be given as output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence.
Inside Parent process: The parent invokes the wait () call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is not passed on the command line.
// C program to illustrate factorial calculation // using fork() in C for Linux #include <stdio.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <string.h> #include <stdlib.h> int main( int argc , char *argv[] ) { pid_t pid; if (argc != 2) { printf ( "arg missing or exceeding\n" ); exit (0); } // atoi converts string to integer if ( atoi ( argv[1] ) <0 ) { printf ( "negative number entered %d" , atoi (argv[1])); exit (0); } pid=fork(); if ( pid<0 ) { printf ( "failed to create child\n" ); exit (0); } else if ( pid==0 ) { //Child Process int ans = 0, i, j, k = 2, n; // atoi converts string to integer n = atoi (argv[1]); int arr[n],sum[n]; arr[0] = 1; // generating factorial series for (i=1 ; i<n; i++) { arr[i] = arr[i-1]*k; k++; } // printing and sum of each sub-series for (j=0; j<n; j++) { sum[j] = 0; for (i=0; i<=j; i++) { printf ( " %d " ,arr[i]); sum[j]+=arr[i]; } printf ( "\n" ); } for (i=0; i<n; i++) { if ((sum[i]%2) == 0) sum[i] = -1; } printf ( "After deletion sum\n" ); for (i=0; i<n; i++) { if (sum[i] > 0) printf ( " %d " , sum[i]); } exit (0); } // parent process else { wait(NULL); // waiting for child process to end printf ( "Done\n" ); } } |
Compiling Code code saved with name fork.c
gfg@ubuntu:~/$ gcc -o fork fork.c
Input:
gfg@ubuntu:~/$ ./fork 5
Output:
1 1 2 1 2 6 1 2 6 24 1 2 6 24 120 After deletion sum 1 3 9 33 153 Done
Firstly, command line argument is accepted. Then the number of arguments are passed and checked if the argument is positive.
After that, generate the factorial series.
Related Article- C program to demonstrate fork() and pipe()
References:
- http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
- http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/wait.html
This article is contributed by Kartik Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.