操作系统代写OS | CSCI 6140 — Computer Operating Systems Homework 2

本次C语言代写操作系统主要要求使用C语言实现系统进程创建和进程管理。

This C language Writing Service for operating system mainly requires the use of C language to achieve system process creation and process management.

CSCI 4210 — Operating Systems
CSCI 6140 — Computer Operating Systems Homework 2 (document version 1.5)

Process Creation and Process Management in C

Overview

  • This homework is due by 11:59:59 PM on Tuesday, February 19, 2019.
  • This homework is to be completed individually. Do not share your code with anyone else.
  • You must use C for this homework assignment, and your code must successfully compile via gcc with absolutely no warning messages when the -Wall (i.e., warn all) compiler option is used. We will also use -Werror, which will treat all warnings as critical errors.
  • Your code must successfully compile and run on Submitty, which uses Ubuntu v18.04.1 LTS. Note that the gcc compiler is version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04).

Homework specifications

In this second homework, you will use C to implement a rudimentary interactive shell similar to that of bash. The focus of this assignment is on process creation, process management, and inter-process communication (IPC) via fork(), wait(), waitpid(), pipe(), etc.

As with Homework 1, continue to use calloc(), realloc(), and free() to properly and efficiently manage your memory usage. Consider using valgrind to verify that there are no memory leaks.

To properly implement your shell, create an infinite loop that repeatedly prompts a user to enter a command, parses the given command, locates the command executable, then executes the command (if found).

To execute the given command, a child process is created via fork(), with the child process then calling (v1.4) execv() to execute the command. In the meanwhile, the parent process calls waitpid() to suspend its execution and wait for the child process to terminate. This is called foreground processing.

If instead the command is to be run with the parent process not waiting for the child process to complete its execution, then we have background processing.

Locating the command executable

Before executing a command entered by the user, the command executable must be found using the list of possible paths specified by an assignment-specific environment variable called $MYPATH. Do not use $PATH for this assignment (since $PATH is used for bash).

Similar to $PATH, this new $MYPATH environment variable consists of a series of paths delimited by the # character. And if $MYPATH is not set, use /bin#. as the default, meaning commands will be searched for first in the /bin directory, then the . (i.e., current) directory.

By default, the $MYPATH variable is not set, so for testing, set and unset this variable manually in the bash shell before running your shell. Here’s how:

bash$ export MYPATH=/usr/local/bin#/usr/bin#/bin#.
bash$ echo $MYPATH
MYPATH=/usr/local/bin#/usr/bin#/bin#.
bash$ unset MYPATH

And to obtain $MYPATH (or any environment variable, e.g., $HOME) from within your program, use the getenv() function. Do not use setenv().

Executing the command

Searching left-to-right in $MYPATH, if the requested command is found in one of the specified direc- tories, your program runs the executable in a child process via the fork() and (v1.4) execv() system calls. (v1.4) Note that you must use execv().

(v1.5) Further, in the parent process, you must use lstat() to determine whether the requested command exists (e.g., does /bin/ls exist?) and whether it is executable.

Commands are line-based, as in bash. Therefore, each command may optionally have any number of arguments (i.e., argv[1], argv[2], etc.). You can assume that each command read from the user will not exceed 1024 characters. Further, you can assume that each argument will not exceed 64 characters, but all memory must be dynamically allocated.

You may also assume that command-line arguments do not contain spaces. In other words, do not worry about parsing out quoted strings in your argument list, as in:

bash$ cat a.txt b.txt "some weird file.txt" d.txt

Special shell commands

Not all commands entered into the shell result in a call to fork(). For the cd command, if your shell did execute the command via fork(), your shell’s current working directory would not change! You must therefore use the chdir() system call in the parent to handle this special case. Further, if the cd command has no arguments, then you should use the $HOME environment variable as the target directory.

As for wildcards and special characters, you do not need to support *, ?, and [] in your shell, though note that these are typically expanded by the shell before calling fork() and (v1.4) execv().

To exit your shell, the user can enter exit. When this occurs, your shell must output bye and terminate.

Required output

The command prompt in the shell must show the current working directory followed by the ‘$’ prompt character and one space. To obtain the current working directory for the running process, use the getcwd() function. And you should use fgets() to read in a command from the user.

Required output is shown below, with sample input also shown. As per usual, you must match the given output format exactly as shown.

Foreground and background processing

Normally, a shell will execute the given command via a child process, with the parent calling either wait() or waitpid() to wait for the child process to complete its command and terminate. The child process essentially calls (v1.4) execv() to execute the given command with the arguments allocated dynamically.

Your shell must be able to execute a process in the background if the user includes an ampersand (i.e., ‘&’) at the end of a command. In this case, when the child process is created, the parent does not wait for the child to terminate before prompting the user for the next command.

For a background process, the parent must report that the child process has been created.

(v1.1) The parent must also report when the child process does terminate (if it does). When you detect that the background process has terminated (i.e., before you display the prompt), display the child process ID and its exit status, as in:

[process 9335 terminated with exit status 0]

Note that the & symbol can only be included at the end of the command line; otherwise, this is a user error. (v1.3) Also note that the output may be interleaved with background processes, so do not expect to always match the example output exactly line for line.

IPC via pipes

Finally, add support for a pipe between two processes; you need only support one pipe per command line. Two processes (i.e., A and B) may be connected via a pipe such that the output on stdout from process A is the input on stdin to process B.

A pipe is indicated via the | symbol. To create a pipe, use the pipe() system call. Further, the two processes A and B must have your shell process as their parent process.

Submission instructions

To submit your assignment (and also perform final testing of your code), please use Submitty, the homework submission server.

Note that this assignment will be available on Submitty a minimum of three days before the due date. Please do not ask on Piazza when Submitty will be available, as you should perform adequate testing on your own Ubuntu platform.

That said, to make sure that your program does execute properly everywhere, including Submitty, use the techniques below.

First, as discussed in class (on 1/10), use the DEBUG_MODE technique to make sure you do not submit any debugging code. Here is an example:

#ifdef DEBUG_MODE
    printf( "the value of x is %d\n", x );
    printf( "the value of q is %d\n", q );
    printf( "why is my program crashing here?!" );
    fflush( stdout );

#endif


程序代写代做C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail: [email protected]  微信:dmxyzl003


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

发表评论