Question

Please complete the following code for challenge.c in C according to the instructions in the comments....

Please complete the following code for challenge.c in C according to the instructions in the comments. Further instructions are below in INSTRUCTIONS.txt

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)

{

}

---

INSTRUCTIONS.TXT

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.

Hint: I recommend using execv for the exec-ing part

execve is under manual section 2

other exec variants are under manual section 3

Homework Answers

Answer #1

Please find the requested program below. Also including the screenshot of code to understand the indentation.

Please provide your feedback
Thanks and Happy learning!

#include <stdio.h>
#include <unistd.h>

void fork_exec(char** argv)
{
    int childExitStatus;
    pid_t pid = fork();
    if (pid == -1)
    {
        printf("Failed to fork a new process.\n");
        return;
    }
    if (pid == 0)
    {
        //In Child process
        execvp(argv[0], argv);
    }
    else
    {
        //In parent process
        //wait for the child to finish
        wait(&childExitStatus);
    }
}

int main(int argc, char** argv)
{
    fork_exec(argv);
    return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT