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.
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...
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
this has to be coded in c ++ • Write a program which generates a DIFFERENT...
this has to be coded in c ++ • Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to solve this. You can submit their code. Make sure you include a comment which shows from where the code was taken. You do not need to understand the code but if you can, thats great. This program will be used in the upcoming...
Write a program in C to process weekly employee timecards for all employees of an organization...
Write a program in C to process weekly employee timecards for all employees of an organization (ask the user number of employees in the start of the program). Each employee will have three data items: an identification number, the hourly wage rate, and the number of hours worked during a given week.A tax amount of 3.625% of gross salary will be deducted. The program output should show the identification number and net pay. Display the total payroll and the average...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT