In: Computer Science
Programming in C (not C++)
## Requirements
Only need to edit the challenge.c
You have one function to implement: void fork_exec(char**
argv):
This takes in an array of strings representing arguments.
The first argument is the filename of an executable (which will be
given as a relative filepath, such as "./a")
The remaining terms would be arguments for said executable.
The array is null terminated
You need to fork your process.
The child needs to call exec (rather, a variant thereof) to execute
the specified file with the specified arguments.
challenge.c
#include "challenge.h"
// goal: fork the process and have the child execute a
process
// param argv: the argument vector for the process to be
executed
// assumptions:
// the first argument of argv is the file name of the
executable
// argv is null terminated
//
// TODO: complete the function
// fork
// exec (child), probably most convenient to use
execvp
// have the parent wait on the child
void fork_exec(char** argv)
{
}
challenge.h
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#ifndef CH_HEAD
#define CH_HEAD
// goal: fork the process and have the child execute a
process
// param argv: the argument vector for the process to be
executed
// assumptions:
// the first argument of argv is the file name of the
executable
// argv is null terminated
void fork_exec(char** argv);
#endif
Summary: The program starts from main and it will call an executable called ./exec. The program to create ./exec is also pasted here for the reference. The challenge.c forkexec method will create a child process and the child will execute the ./exec program. The parent will wait for the child to get finished. The output is pasted at the end.
Challenge.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "challenge.h"
// goal: fork the process and have the child execute a process
// param argv: the argument vector for the process to be executed
// assumptions:
// the first argument of argv is the file name of the executable
// argv is null terminated
//
// TODO: complete the function
// fork
// exec (child), probably most convenient to use execvp
// have the parent wait on the child
void forkexec(char** argv)
{
pid_t childPid;
childPid = fork();
// The fork() method will return 0 in the child process
if(childPid == 0) {
// Now we are in the child process
printf("Child process id: %d PID: %d\n", getppid(), getpid());
execvp(argv[0],argv);
}
else if(childPid > 0) {
// The fork() method will return > 0 in the parent process
printf("Parent process id: %d\n", getpid());
printf("Waiting for child process to terminate.\n");
// Wait for the child to terminate
wait(NULL);
printf("Child process completed.\n");
}
else {
printf("Unable to create child process.\n");
}
}
int main()
{
char *argv[]={"./exec", "I am a child process!!!", NULL};
// Call the fork method
forkexec(argv);
return 0;
}
Creating a file exec.c
#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
// This will be called by the child process
printf("\nIn Child process\n");
printf("Program name is: %s\n", argv[0]);
printf("First argument is: %s\n", argv[1]);
return 0;
}
This will create a executable exec file