Question

In: Computer Science

Write a C program which fork the process and have the child execute a process.You have...

Write a C program which fork the process and have the child execute a process.You have to implement void forkexec(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 "./a").The remaining terms would be arguments for said executable.The array is null terminated and you need to fork your process.The child needs to call exec/execvp to execute the specified file with the specified arguments. Also have the parent wait on the child. Assume that the first argument of argv is the file name of the executable and argv is null terminated.

Solutions

Expert Solution

The program demostrates the use of fork() to create a child tread and inside the child thread we will execute a program. The parent process will wait until the child executes using the wait funtion available. The child program will print output when the control of execution reaches there.

Child program - sample.c

#include<stdio.h> 
#include<unistd.h> 
 
int main() 
{ 
    # This program is used to create the executable for testing the child process creation
    int i; 
    printf("I am in child process called by execvp() "); 
    printf("\n"); 
      
    return 0; 
} 

Main program - fork.c

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

void forkexec(char** argv)
{
        pid_t childPid;
        childPid = fork();

        if(childPid == 0)  // fork succeeded 
        {   
        // We are in child process
                execvp(argv[0],argv);
            exit(0); 
        }
        else  // Main (parent) process after fork succeeds 
        {    
            int returnStatus;    
            // wait for child to terminate
            waitpid(childPid, &returnStatus, 0); 

            printf("The parent process is terminating.");
        }
        
}
int main() 
{ 
        char *args[]={"./sample", NULL}; 
    // Call the api for creating the process
        forkexec(args);
    return 0; 
} 

Output


Related Solutions

You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
In C language, Write a program called minishell that creates two child processes: one to execute...
In C language, Write a program called minishell that creates two child processes: one to execute 'ls -al' and the other to execute ‘grep minishell.c’. After the forks, the original parent process waits for both child processes to finish before it terminates. The parent should print out the pid of each child after it finishes. The standard output of 'ls -al' process should be piped to the input to the 'grep minishell.c' process. Make sure you close the unnecessary open...
The following program calls the fork() system call to create a child process. Suppose that the...
The following program calls the fork() system call to create a child process. Suppose that the actual pids of the parent process and child process are as follows: Parent process: 801 Child process:   802 (as returned by fork) Assume all supporting libraries have been included. => Use the answer text field to write the values of the pids printed at lines 1, 2, 3, and 4 (indicate each line number and the value printed). int main() {      pid_t pid,...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
Use any C program without using PTHREADS calculate time taken to execute program. Write same program...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program of question 1 using PTHREADS. Calculate time taken to execute program.(1 mark) Identify data races in above program and explain why this situation occurred with an example (1 mark) Rewrite the code to avoid data races should use any of the THREE techniques.(1.5 marks) please I need the c code.. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2,...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
C programming language The program first creates a child process CP. So, there are two processes:...
C programming language The program first creates a child process CP. So, there are two processes: The parent process does the following: a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary; b. wait for the termination of the child process CP, then terminate; The child process does the following: a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary; b. terminates;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT