Question
please someone help me with this assignment. * Figure 3.33 */ #include #include #include #include int main() { pid_t pid; /* fork a child process
please someone help me with this assignment.
* Figure 3.33 */
#include
int main() { pid_t pid; /* fork a child process */ pid = fork();
if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls","ls",NULL); printf("LINE J"); } else { /* parent process */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete"); }
return 0; }
Part #1: Implementing a simple Shell
For interprocess communication, we need a few more concepts:
- Pipes: A UNIX pipe is a kernel buffer with two file descriptors, one for writing (to put data into the pipe) and one for reading (to pull data out of the pipe), as illustrated in the following Figure 1.
Figure 1: File descriptors for an ordinary pipe
Data is read in exactly the same sequence it is written, but since data is buffered, the execution of the producer/writer and consumer/reader (another concept, we will elaborate when discussing Threads and Process Synchronization) can be decoupled, reducing waiting in the common case. The pipe terminates when either endpoint closes the pipe or exits.
The Internet has a similar facility to UNIX pipes called TCP (Transmission Control Protocol), we will talk about TCP more later on Client-Sever Socket programming. When UNIX pipes connect processes on the same machine, TCP provides a bi-directional pipe between two processes running on different machines. In TCP, data is written as a sequence of bytes on one machine and read out as the same sequence on the other machine.
On UNIX systems, ordinary pipes are constructed using the function
pipe(int fd[])
This function creates a pipe that is accessed through the int fd[] file descriptors: fd[0] is the read-end of the pipe, and fd[1] is the write-end. UNIX treats a pipe as a special type of file (BTW, UNIX treats everything as a file). Thus, pipes can be accessed using ordinary read() and write() system calls.
- Replace file descriptor. By manipulating the file descriptors of the child process, the shell can cause the child to read its input from, or send its output to, a file or to a pipe instead of from a keyboard or to the screen. This way, the child process does not need to be aware of who is providing or consuming its I/O. The shell does this redirection using a special system call named dup2(from, to) that replaces the to file descriptor with a copy of the from file descriptor.
The dozen UNIX system calls listed in the following Table 1 will be help in designing a simple command line shell, one that will run entirely at user-level with no special permissions.
System call | Description |
Creating and managing processes | |
fork() | Create a child process as a clone of the current process. The fork call returns to both the parent and child |
exec(prog, args) | Run the application prog in the current process |
exit() | Tell the kernel the current process is complete, and its data structures should be garbage collected |
wait(processID) | Pause until the child process has exited |
signal(processID, type) | Send an interrupt of a specified type to a process |
I/O operations | |
fileDesc open(name) | Open a file, channel, or hardware device, specified by name; returns a file descriptor that can be used by other calls |
pipe(fileDesc[2]) | Create a one-directional pipe for communication between two processes. Pipe returns two file descriptors, one for reading and one for writing. |
dup2(fromFileDesc, toFileDesc) | Replace the toFileDesc file descriptor with a copy of fromFileDesc. Used for replacing stdin or stdout or both in a child process before calling exec |
int read(fileDesc, buffer, size) | Read up to size bytes into buffer, from the file, channel, or device. Read returns the number of bytes actually read. For streaming devices this will often be less than size. For example, a read from the keyboard device will (normally) return all of its queued bytes. |
int write(fileDesc, buffer, size) | Analogous to read, write up to size bytes into kernel output buffer for a file, channel, or device. Write normally returns immediately but may stall if there is no space in the kernel buffer. |
close(fileDescriptor) | Tell the kernel that the process is done with this file, channel, or device |
Table 1: List of UNIX system calls required to implement a simple shell
Compilation using: g++ -g Wall std=c++14 o NameOfExecutable source.cpp
Part #1.1:Show/Run the Shell code Fig3-33.cpp as it is described above and attach a screen shot
Part #1.2: Modify the code as follows
- Instead of execlp, make use of execvp as described below:
The first task is to modify the main() function in the above code so that a child process is forked and executes the command specified by the user. This will require parsing what the user has entered into separate tokens and storing the tokens in an array of character strings(agrs). For example, if the user enters the command ps ael at the mysh> prompt, the values stored in the args array are:
args[0] = ps
args[1] = -ael
args[2] = NULL
This args array will be passed to the execvp() function, which has the following prototype
execvp(char *command, char *params[]);
Here, command represents the command to be performed and params stores the parameters for this command. For this part of the Lab #6, the execvp() function should be invoked as execvp(args[0], args).
Another example to run command from shell: ls l | wc l
char *ls_args[] = {"ls", "-l", NULL}; //for parent process
char *wc_args[] = {"wc", "-l", NULL}; //for child process
Part # 1.3 Outline of a simple shell
#include
#include
#define MAXLINE 80 /* The maximum length command */
int main(void)
{
char *args[MAXLINE/2 + 1]; /* command line arguments */
int should_run = 1; /* flag to determine when to exit program */
char *prog = NULL;
char **args = NULL;
while (should_run) {
printf(mysh);
fflush(stdout);
/**
*After reading user input, the steps are:
* (1) fork a child process using fork()
* (2) the child process will invoke execvp()
*/
int child_pid = fork();
if (child_pid == 0) { /* Im the child process, run program with parents I/O
exec(prog, args);
}
else { /* Im the parent: wait for the child to complete
wait(child_pid);
return 0;
}
}
return 0;
}
The above pseudocode illustrates the basic operation of a shell. The shell reads a command line from the input, and it forks a process to execute that command. UNIX fork automatically duplicates all open file descriptors in the parent, incrementing the kernels reference counts for those descriptors, so the input and output of the child is the same as the parent. The parent waits for the child to finish before it reads the next command to execute.
Because the commands to read and write to an open file descriptor are the same whether the file descriptor represents a keyboard, screen, file, device, or pipe, UNIX programs do not need to be aware of where their input is coming from, or where their output is going. This is helpful in a number of ways:
- A program can be a file of commands.
- A program can send its output to a file.
- A program can read its input from a file.
- The output of one program can be the input to another program.
Show/Run the Shell code CustomShell.cpp as it is described above and attach a screen shot
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started