联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-10-28 10:41

Answer all the questions below. Once you are finished, print the code of

Question 3 on paper and submit the paper at the start of the lecture on the

due date. Put your name (in English) and student ID number on the paper.

Also add to your paper a picture of your program running.

In addition, upload the C file (only the C file itself, nothing else, no

ZIP file) for Question 3 on iSpace.

Late homework assignments will not be accepted, unless you have a valid

written excuse (medical, etc.). You must do this assignment alone. No

team work or "talking with your friends" will be accepted. No copying from

the Internet. Cheating means zero.

You can do this assignment on a computer running Microsoft Windows, Mac OS

X, or Unix, as you prefer. Or, if you want to, you can instead do this

assignment using Unix running on a virtual machine running on Microsoft

Windows (for example), or using the Cygwin system https://www.cygwin.com

which simulates Unix systems calls on Microsoft Windows.

In all cases, you will need a C compiler, such as:

- Microsoft Visual C++ / Visual Studio (big) or Pelles C (small and free)

on Microsoft Windows;

- Xcode on Mac OS X (huge, and you will need to register with Apple as a

developer to download it);

- gcc on Unix.

Make sure you start this homework assignment early so that you have time to

find, download, and install all the software you might need!

0) The goal of this homework assignment is to use system calls to create

your own simple command line interpreter (similar to the "Command Prompt"

program on Microsoft Windows, or the "Terminal" program on Mac OS X, or

shell programs on Unix).

Here is the code of a small C program that uses the printf and fgets

functions from the C standard library to do input and output:

/************************************************************/

#include <stdio.h>

#define SIZE 1024

int main(void) {

char prompt[] = "Type a command: ";

char buf[SIZE];

// Ask the user to type a command:

printf(prompt);

// Read from the standard input the command typed by the user (note

// that fgets also puts into the array buf the ’\n’ character typed

// by the user when the user presses the Enter key on the keyboard):

fgets(buf, SIZE, stdin);

// Replace the Enter key typed by the user with ’\0’:

for(int i = 0; i < SIZE; i++) {

if(buf[i] == ’\n’ || buf[i] == ’\r’) {

buf[i] = ’\0’;

break;

}

}

// Execute the command typed by the user (only prints it for now):

printf("Executing command: %s\n", buf);

return 0;

}

Since this program uses only the C standard library, it can work without

any change on all operating systems.

Compile this program on your computer and make sure you can execute it. If

you have any problem compiling or executing this program then contact us

immediately for help!

1) Instead of using the printf and fgets functions from the C standard

library, we want instead to have the program directly use system calls to

do input and output. So do the following:

- delete the "#include <stdio.h>" line from the beginning of the program

(you will not need this line anymore since your program will no longer

use functions from the C standard library);

- replace the printf and fgets function calls with system calls:

- on Microsoft Windows:

- replace "printf" with "WriteConsole"; the first argument of

WriteConsole must be a HANDLE value for the standard output stream of

the process, which you can obtain using the following code:

GetStdHandle(STD_OUTPUT_HANDLE)

WARNING: if you are using Microsoft Visual C++ then make sure that

the Project -> Properties -> Configuration Properties -> General ->

Character Set menu is set to "Not Set" (not "Use Unicode Character

Set"), otherwise your program might not print the output correctly or

might not print any output at all!

- replace "fgets" with "ReadConsole"; the first argument of ReadConsole

must be a HANDLE for the standard input stream of the process, which

you can obtain using the following code:

GetStdHandle(STD_INPUT_HANDLE)

- see https://docs.microsoft.com/en-us/windows/console/console-functions

for more information about the ReadConsole and WriteConsole functions.

- see https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types

for more information about Microsoft Windows data types such as DWORD

and LPDWORD.

- on Mac OS X and Unix:

- replace "printf" with "write"; the first argument of write must be a

"file descriptor" for the standard output stream, which is the integer 1;

- replace "fgets" with "read"; the first argument of read must be a

"file descriptor" for the standard input stream, which is the integer 0;

- see Section 2 of https://www.kernel.org/doc/man-pages/

for more information about the read and write system calls.

- WARNING: there is a difference between the size of an array, and the

length of the string stored inside an array. Be careful about this when

writing or reading data using system calls.

- if necessary, use the strlen function from the C standard library to

compute the length of a string that you want to print;

- make sure you #include in your program the correct header files for all

the system calls you are using; header files are NOT optional;

- when you print the user’s input back to the screen, a final newline

character might be missing, so just do one extra system call to print

"\n" (the string "\n", which you can directly give as argument to the

system call, not the character ’\n’) on the screen before the program

ends.

The resulting program must then work exactly like the original program

above.

2) Instead of just printing back to the screen what the user typed, we now

want to use what the user typed as the name of a program to execute in a

new child process. So do the following:

- remove the printing code at the end of the program (the code that appears

after the "Execute the command typed by the user" comment and before the

"return 0;");

- replace that printing code with code to create a new child process; this

new child process is going to use what the user typed as the name of the

program to execute in the new process:

- on Microsoft Windows, use the CreateProcess system call like we saw in

class; use buf as the second argument of the system call; if the

CreateProcess system call fails then use the WriteConsole system call

to write a "CreateProcess failed\n" error message to the user;

- on Mac OS X or Unix, use the fork and execlp system calls like we saw

in class; use buf as the first and second arguments of execlp. If the

execlp system call fails then use the write system call to write an

"execlp failed\n" error message to the user, and make sure that the new

child process immediately dies after printing the error message,

otherwise both the parent and child will keep running your program!

- add code so that the parent process waits until the child process is dead

(if and only if creating the child process succeeded before):

- on Microsoft Windows, use the WaitForSingleObject system call like we saw

in class;

- on Mac OS X or Unix, use the wait system call like we saw in class.

WARNING: on Mac OS X, the Xcode console (the small window inside Xcode

that you use to give input to your program and see the output of your

program) is broken and because of this the wait system call might behave

strangely (for example, the wait system call will return even before the

child process is finished). So instead of using the Xcode console, do

the following: build the program as usual using Xcode, then in the left

pane of Xcode (the left column of the Xcode GUI) open the "Products"

folder, find the executable program inside this folder, right-click on

the executable program and use the "Show in Finder" menu entry, then

double-click on the program in the new Finder window to execute the

program.

- make sure you #include in your program the correct header files for all

the system calls you are using; header files are NOT optional.

Run the program and test that it correctly creates a child process

that executes the program indicated by the user:

- on Microsoft Windows, test your program by typing for example:

C:/Windows/System32/notepad.exe at the prompt of your program; Microsoft

Window’s basic editor must then appear on the screen.

- on Mac OS X, test your program by typing for example:

/Applications/Calculator.app/Contents/MacOS/Calculator at the prompt of

your program; Mac OS X’s calculator must then appear on the screen.

- on Unix, test your program by typing for example: /usr/X11R6/bin/xcalc at

the prompt of your program; Unix’s calculator must then appear on the

screen.

Also test that an error message is correctly printed if the user types the

name of a program that does not exist.

3) At this point, your program reads input from the user only once, and

creates a new child process only once. To transform your program into a

simple command line interpreter, the only thing left to do is to change the

code of your program to use a loop so that your program asks the user for

input, creates a new child process, waits for the child process to end,

then asks the user for input again, creates a new child process again,

waits for the child process to end again, then repeats the whole thing

again, over and over. So do the following:

- add a "while" loop around the code that executes the command typed by the

user (around all the code you modified in the previous question, and

nothing else);

- the test of the loop must stop the loop if the text typed by the user

is the word "exit", otherwise the code inside the loop must be executed

(use the strcmp function from the C standard library to compare strings);

- after your program has finished waiting for the child process to die, the

code inside the "while" loop must print the prompt again and read the

next input from the user; the code must then also replace the Enter key

typed by the user with a ’\0’, before the user input is tested again at

the beginning of the "while" loop. To do all this, simply copy the

input/output code and "for" loop which are at the beginning of your

program (before the "while" loop) and paste this code inside the "while"

loop (at the end of the code inside the "while" loop).

Running the program must then allow the user to type and execute multiple

commands one after the other, and each time your program must create a

new process for that command, until the user types "exit", at which point

your program stops.

For example, on a Microsoft Windows computer:

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

Type a command: C:/Windows/System32/notepad.exe

Type a command: C:/Windows/System32/mspaint.exe

Type a command: bla bla bla

CreateProcess failed

Type a command: C:/Windows/System32/charmap.exe

Type a command: exit

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

Or on a Unix computer:

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

Type a command: /usr/X11R6/bin/xcalc

Type a command: /usr/X11R6/bin/xeyes

Type a command: bla bla bla

execlp failed

Type a command: /usr/X11R6/bin/xload

Type a command: exit

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

For Mac OS X, look into your /Applications and /Applications/Utilities

directories for programs such as

/Applications/Calculator.app/Contents/MacOS/Calculator that you can use for

testing your own program (we do not have access to a Mac OS X computer

to test it ourselves)

The program you have created is then a very simple version of the "Command

Prompt" program on Microsoft Windows, or of the "Terminal" program on Mac

OS X, or of shell programs on Unix.

Here are a few extra instructions:

- Give meaningful names to your variables so we can easily know what each

variable is used for in your program.

- Put comments in your code (in English!) to explain WHAT your code is

doing and also to explain HOW your program is doing it.

- Make sure all your code is properly indented (formatted). Your code

must be beautiful to read.

- Include a picture of your program running, even if your program is not

completely finished.

Failure to follow these instructions will result in you losing points.

The End!


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp