Question

In: Computer Science

Programming in C (not C++) ## Requirements Only need to edit the challenge.c You have one...

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

Solutions

Expert Solution

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


Related Solutions

You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
Why does NLP need AI? Q2) Consider the dynamic programming (DP) approach to solve the edit...
Why does NLP need AI? Q2) Consider the dynamic programming (DP) approach to solve the edit distant problem, in which the distant between two strings are calculated by ?(?,?)=min{?(?−1,?)+1, ?(?,?−1)+1, ?(?−1,?−1)+?(?,?), where ?(?,?)=2, if the corresponding letters are not matching, and ?(?,?)=0, if they are matching. Apply this DP approach to compute the edit distance in the following example. Y 3 A 2 P 1 # 0 1 2 3 4 # P L A Y Q3) Given the automaton...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
I ONLY need requirements 9&10 on the first set of requirements and then 1 &2 on...
I ONLY need requirements 9&10 on the first set of requirements and then 1 &2 on the second set of requirements please. Thank you! Endless Mountain Company manufactures a single product that is popular with outdoor recreation enthusiasts. The company sells its product to retailers throughout the northeastern quadrant of the United States. It is in the process of creating a master budget for 2019 and reports a balance sheet at December 31, 2018 as follows: Endless Mountain Company Balance...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!! Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT