I would like to start a new process from each of my child process and have them do addition of numbers passed as parameters through exec(). Something like this. I just don't know how I can access the parameters in the new process.
code.c
#include <stdio.h>
#include <stdlib.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include<stdbool.h>
# include <ctype.h>
# include<sys/wait.h>
# include<signal.h>
# include <sys/mman.h>
# include<sys/time.h>
void forking()
{ int a=4,b=5
for (int i=0;i<4;i++)
pid_t pID = fork();
if (pID == 0)
{
static char *argv[]={a,b,NULL};
execv("/EXEC.c",argv);
}
else
wait(pID);
}
void main(int argc, char **argv)
{
forking();
}
EXEC.c
#include <stdio.h>
#include <stdlib.h>
# include <stdio.h>
# include <string.h>
int main()
{
//Code to add a and b and print the sum
printf("%d",a+b);
}
/EXEC.cin (a) the root directory (don't write in the root directory — don't run as root, because only root should be able to write in the root directory, and running as root is risky and should be avoided), and (b) don't call your executable programs with names that look like source code (EXEC.cwould normally be a C source file, not a program that can be executed; you need to create and runEXECfromEXEC.c). Some would add "don't shout" — most often, program names are all lower-case, or mostly lower-case; they are rarely mostly upper-case as shown.exec.cto createexecwould work badly becauseexecis a built-in shell command that is not searched for on disk. Using/path/to/exec(or even./exec) would work, but the path would be necessary. Use a different name (don't usetestfor your programs either — that's another shell built-in).main()return in C and C++ — you should be usingint main(void)orint main(int argc, char **argv). You access arguments passed to your program viaargcandargv— they are strings. You pass arguments to executed programs as strings, not as plain integers as you are trying to do withstatic char *argv[]={a,b,NULL};(whereaandbare of typeint). You should pass the program name asargv[0], and the operational arguments asargv[1]andargv[2]— you're correct to add NULL to the end of the argument list.code.c— you should include only those you use. You shouldn't repeat<stdio.h>orstdlib.h>. You should be consistent in your spacing — preferably following the pattern of your first#include <stdio.h>line (no space between#andinclude; one space betweenincludeand<stdio.h>; no trailing white space usually, though a comment is allowed if deemed beneficial and that should be preceded by white space). And theprintf()inEXEC.cshould have a newline at the end of the format string.