Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Will leave like rating :) 1. You will implement the general system process call API in C using fork, wait, and execvp. 2. You will

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Will leave like rating :)

1. You will implement the general system process call API in C using fork, wait, and execvp. 2. You will be able to describe how Unix implements general process execution of a child from a parent process. 3. You will write a shell program in C so that you can see how child processes are created and destroyed \& how they interact with parent processes. General Instructions and Hints: -The code for this assignment must be in C. -There is a significant amount of documentation in this assignment that you must read; you are better of starting it sooner rather than later. -Global variables are not allowed and you must use prototypes -Name files exactly as described in the documentation below. -All functions should match the assignment descriptions. Do not add parameters, change names, or return different values. -All output should match exactly what is in this document (including spacing!). If it does not match, it will not pass the autograder. -When part 1 is done, open a terminal and cd to your github repo, wherever you saved it. Do the following: git add . then git commit m then git push. -All work should be done on a machine where you have sudoer permission. -All work should be your own. Part 1, the Process API Background Normally, when you log in, the OS will create a user process that binds to the login port; this causes the user process at that port to execute a shell. A shell (command line interpreter) allows the user to interact with the OS and the OS to respond to the user. The shell is a character-oriented interface that allows users to type characters terminated by Enter (the in char). The OS will respond by sending characters back to the screen. If the OS is using a GUI, the window manager software will take over any shell tasks, and the user can access them by using a screen display with a fixed number of characters and lines. We commonly call this display your terminal, shell, or console, and in Linux, it will output a prompt, which is usually your user@machineName followed by the terminal's current place in the file system and then a $ (see Figure 2). Common commands in Linux use bash, and usually take on the form of command argument1 ... argumentN For example, in chmod u+x > chmod is the command, u+x _is an argument and _filename > is an argument. Not all commands require arguments- for example, you can run is with and without any arguments. After entering a command at the prompt, the shell creates a child process to execute whatever command you provided. The following are the steps that the shell must take to be functional: 1. Print a prompt and wait for input. 2. Get the command line input. 3. Parse the command line input. 4. Find associated files. 5. Pass any parameters from the shell to the OS command function. 6. Execute the command (with applicable parameters). General Directions Name your program systemCaller.c. You will turn in the C code. Failure to do so will result in a 0 on this portion. In this part of the assignment, you will write a function called executeCommand in the C language. We will use the execvp system call to launch processes when possible. However, the exit and cd (change directories) commands are not executed by execvp- they have their own system calls. This program should consist of, at minimum, 4 functions: main, executeCommand, changeDirectories, and parselnput (which is provided to you below). For each function, the name, retum type, parameters, edge cases and exceptions that you must address, and description of functionality are provided. You may implement additional functions as you see fit. In other words, I'm asking you to write a very stripped down shell that uses a very stripped down process API. Generally speaking, the program will execute a shell and loop until the user chooses to exit (you've seen a very basic code example of this in lecture). This shell will be interactive- that is, you will run the program ( ) to launch a new shell (just like if you had opened a terminal). Then you should be able to type in various execvp launchable commands (such as is -la or clear), exit, or cd into the shell, and have it perform the associated behavior. You may only use the following libraries, and not all of them are necessary: Any necessary display can be done using the write() command or the printf(0 command. Any reading can be done using fgets 0 and/or scanf0 from the C library. The man pages for any appropriate system commands/c specific commands are provided throughout these instructions. Note that if you are asked to use a specific command (like execvp), then you must use that command- not any alternatives. Because I assume you know how character arrays in C work, I provide the parselnput 0 method for every student to use to parse input entered by the user, which you can copy/paste into your code. It takes any character array, checks that there are contents in it, then fills the splitWords 2d char array with each argument. It will be used in executeCommand0. void parseInput (char *input, char *splitwords []) 1 3 ] Note that when strtok is used, and you have 2d array of tokens (see parselnput) you can access the first token and it's arguments. So if the user enters Is -la then splitWords array would look like: Is -la Where the pointer to the Is command is at splitWords[0], while the arguments(-la) are at splitWords[1]. The char array pointer can be incremented to iterate from the first command/argument combo (if necessary, which isn't the case for this iteration of the shell, but we use the 2d array here to make implementation later easier). If you do not like my parse implementation, you may write your own. main Input Parameters: none Returned Output: int Functionality: The main function is responsible for generating the shell and looping until a user chooses to exit. It parselnput, and fill the passed in 2d array with the arguments entered by the user. Add NULL as the last string in the array so that execvp executes correctly. Check the first argument of the filled array. If it is anything other than cd or exit, you should call executeCommand and pass it the newly filled 2d array. If it is cd, changeDirectories should be called with appropriate arguments. If it is exit, stop looping and return from main. The return from executeCommand function should be stored as an int, and used to determine if the call was successful ( 1 or 1 are usually failure in Unix). You can use strcmp to check the arguments entered by the user in your shell. executeCommand Input Parameters: char ] Returned Output int Functionality: executeCommand uses execvp to execute a provided command as a process. This function should fork a child process for the provided argument. The fork should be checked to see if the child process was successfully created (see below). Then execvp should be provided the command from the user that was passed into the funciton (ls) and any associated arguments (lsla). See General Directions if you don't know what portion of the char array is the argument and what is the command. The retum from execvp should be stored to check for errors. Finally, after checking for errors, wait should be used by the parent to wait for the child process executed by execvp to finish before the parent process can move on. If the process successfully executes and forks without error, retum 0 . Otherwise return 1 . Edge Cases: If a process is not successfully forked, your function should print fork failed! Hint: execvp returns 1 if it wasn't successful. changeDirectories Input Parameters: char* Returned Output: None Functionality: execvp does not execute the change directories (cd) command. To implement this functionality, you will need to write your own changeDirectories function using chdir. Chdir should accept the path supplied as an input parameter. If successful, the directory location should change. Otherwise, output Path Not Found! Edge Cases: Normally, if you enter cd without any path in a linux environment, the shell will change directories to the home directory. For this exercise, you do not need to have the shell do this. Instead, you should check the number of provided arguments, and if you have cd but no path, or too many trailing arguments, you should display Path Not Found!. To Submit When you are done, you should use git to git push the following to your assignment repo (see instructions above): To Submit When you are done, you should use git to git push the following to your assignment repo (see instructions above): 1. systemCallerc You can submit as many times as you would like

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

Students also viewed these Databases questions