Question

In: Computer Science

Ubuntu, Linux, MacOS use one of these systems. 1a. Create a C program that forks a...

Ubuntu, Linux, MacOS use one of these systems.
1a. Create a C program that forks a child process that executes the command ‘ls’.
1b. Extend the program you created in Problem 1a such that it takes an argument. For example, the created child process should be able to execute the command ‘ls’ with any arbitrary single argument such as ‘ls -l’, and ‘ls -a’.

Solutions

Expert Solution

//Program for 1a

1a.

//solution

#include <stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/wait.h>

#include<sys/unistd.h>

int main()

{

char *command[3];

int pid,status;

//create child process by using system call fork()

pid=fork();

if(pid > 0) // parent process

{

//wait for child to finish executing ls

wait(&status);

exit(0);

}

else //child process , execute ls command

{

//buld command

command[0]=(char*)malloc(sizeof(char)*3);

strcpy(command[0],"ls");

command[1]=NULL;

printf("Child process executes ls command\n");

printf("#####Output of ls command#####\n");

//execute using system call execvp

execvp(*command,command);

}

}

===============================

//Output

 gcc mySh.c
 ./a.out
Child process executes ls command
#####Output of ls command#####
a.out main.sh mySh.c

========================================

//Program for 1b

1b.

//solution

#include <stdio.h>

#include<string.h>

#include<stdlib.h>

#include<sys/wait.h>

#include<sys/unistd.h>

//make program to take command line args

int main(int argc,char *argv[])

{

char *command[3];

int pid,status;

//if there is no otion given for ls command , print error message to user and exit

if(argc < 2)

{

printf("Usage: %s single_argument_to_ls_command\n",argv[0]);

exit(-1);

}

//create child process by using system call fork()

pid=fork();

if(pid > 0) // parent process

{

//wait for child to finish executing ls

wait(&status);

exit(0);

}

else //child process , execute ls command

{

//buld command

command[0]=(char*)malloc(sizeof(char)*3);

strcpy(command[0],"ls");

command[1]=(char*)malloc(sizeof(char)*3);

strcpy(command[1],argv[1]);

command[2]=NULL;

printf("Child process executes ls command with argument %s\n",argv[1]);

printf("#####Output of ls %s #####\n",argv[1]);

//execute using system call execvp

execvp(*command,command);

}

}

========================

//Output

 gcc mySh.c
 ./a.out
Usage: ./a.out single_argument_to_ls_command
 ./a.out -l
Child process executes ls command with argument -l
#####Output of ls -l #####
total 16
-rwxr-xr-x 1 runner runner 8352 Oct 11 08:25 a.out
-rw-r--r-- 1 runner runner 0 Oct 11 08:08 main.sh
-rw-r--r-- 1 runner runner 1042 Oct 11 08:25 mySh.c

//Output2

 gcc mySh.c
 ./a.out -a
Child process executes ls command with argument -a
#####Output of ls -a #####
. .bash_history .bashrc .profile main.sh
.. .bash_logout .go.sh a.out mySh.c


Related Solutions

Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then...
[BushQ2] Consider the typical file structure of the C: (Windows) or / (Linux and macOS) drives....
[BushQ2] Consider the typical file structure of the C: (Windows) or / (Linux and macOS) drives. How is the hierarchy of this structure reflective of the permissions granted to various users within the system? Explain.
Consider Unix, Linux, MacOS, Android and Windows operating systems. Consider the primary application of each in...
Consider Unix, Linux, MacOS, Android and Windows operating systems. Consider the primary application of each in the marketplace. How does each OS implement threads? How does the usage of threads impact application (usage) ?
Ubuntu Linux HW2: 1. Give one line of the Linux command to see your (your login...
Ubuntu Linux HW2: 1. Give one line of the Linux command to see your (your login ID) credential data from the passwd file. Use a pipe and grep command. 2. Give one line of the Linux command to calculate the following and save it in the hex to a file calcOut. Use bc and <<<. 3+2*4 Submit Screenshots HW3: Permission Show the result clearly after each of the following commands are executed. 1. Create a subdirectory, “HW3”. Write the Linux...
***************************************************** * USE UBUNTU (Linux) TERMINAL MODE COMMANDS ONLY * * DO NOT USE ANY EDITORS...
***************************************************** * USE UBUNTU (Linux) TERMINAL MODE COMMANDS ONLY * * DO NOT USE ANY EDITORS TO CREATE THIS PROGRAM * ***************************************************** 1) Create a file called lastNameFirstNameInitialpgm3.sh ex: robinsonMpgm1.sh 2) Append to the above file the necessary commands that when this file is executed it will display the following: - Using your own set of multi-line comments enter your program identification as described in the Syllabus, example: <<ID ************************************************** Author : Your Name Course : Course Name and Times...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
Linux Ubuntu 1. Use the appropriate commands to locate the device file used by the device,...
Linux Ubuntu 1. Use the appropriate commands to locate the device file used by the device, mount the filesystem to a directory of your choice, and check the filesystem for errors. 2. Finally, add a line to /etc/fstab to ensure that the filesystem can be easily mounted in the future (this line should not automount the filesystem at boot time). You have to document all your steps and provide the screenshot of your work process.
Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
Create a C program that has a sequence of at least 8 linux commands you would...
Create a C program that has a sequence of at least 8 linux commands you would execute to display what each command does. Explain what each command is doing and show the output of them.
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT