Home > Blockchain >  understand how fork() and execv functions work by example
understand how fork() and execv functions work by example

Time:12-19

As I'm new to C, I have a simple code sample that demonstrates how the fork() and execv() functions work. However, I would like a more detailed explanation of the code. There's a bit struggle to understand the difference between those two functions.

Here is the following code snippets example:

// Code1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
 
int main()
{
if(fork() == 0){
char *args[]={"./f.exe",NULL};
execv(args[0],args);
}
}

Code 2 for calculating fibonacci numbers

// f.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
 
int main() {
 
  int i, n;
 
  // initialize first and second terms
  int t1 = 0, t2 = 1;
 
  // initialize the next term (3rd term)
  int nextTerm = t1   t2;
 
  // print the first two terms t1 and t2
  printf("Fibonacci Series: %d, %d, ", t1, t2);
 
  // print 3rd to nth terms
  for (i = 3; i <= 10;   i) {
    printf("%d, ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1   t2;
  }
 
  return 0;
}

CodePudding user response:

In the first code the fork() function is used to create a new process. i.e this function creates a copy of the calling process with the child process having a new process ID and the parent process having the same process ID as before.. the child process starts execution at the point where fork() is called, while the parent process continues execution after the call to fork(). the if statement following the call to fork() checks the return value of fork() , if the return value is 0, it means that the current process is the child process. in this case, the child process executes the execv() function, which replaces the current process image with a new process image specified by the arguments passed to execv(), the first argument to execv() is the path to the executable file that the child process should execute ./f.exe and the second argument is an array of strings which specifies the arguments that should be passed to the executable file when it is executed,in this case, the array is {"./f.exe",NULL}.

the second code calculates and prints out the first 10 numbers in the fibonacci sequence (i guess in your case it is the f.exe executable file that is executed by the child process in previous code.

Quick Def of Fibonacci sequence: it is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.[for more details https://en.wikipedia.org/wiki/Fibonacci_number]

in your code Fibonacci sequence is generated using a loop that starts with the first two terms t1 and t2, initialized to 0 and 1 respectively..the next term in the sequence is calculated as the sum of the previous two terms, and the loop continues until the desired number of terms is reached (in this case is 10) , teh generated terms are then printed to the console using the printf() funct.

  •  Tags:  
  • c
  • Related