Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

The goal of this programming assignment is to get some understanding of operating system command interpreters, system calls, and processes. As all textbook program examples

image text in transcribed

image text in transcribed

The goal of this programming assignment is to get some understanding of operating system command interpreters, system calls, and processes. As all textbook program examples are in C, that will be the language for all our programming assignments. If you're not yet fluent in C please take some time to get better at it, and use it anyways. It is recommended that the code for this assignment be developed in two phases Phase 1: In Phase 1, write a function words) that takes a character string str as input, and returns a dynamically allocated, fixed-lengthed, NULL-terminated array of pointers to dynamically allocated copies of words in the given string str. That's a mouthful, but the underlying idea is really simple :) The function can be declared as: char **words(char *str The words in str are separated by whitespace, i.e. blanks, tabs, and newlines. The words function detects al words in str, copies each word into some new space allocated by malloc (, and returns an array with pointers to these new spaces. The amay can be of some fixed length, say 64, but will have as many pointers as the number of words in str, followed by a NULL pointe. As an example, if str "Rajiv Bagai rocks!!",then it has 3 words, namely "Rajiv" "Bagai", and "rocks!!". After making copies of these 3 words, the words ) function will return an array of 64 pointers. Only the first 4 elements in the array will have useful pointer values, namely to the newly created copies of these 3 words, and NULL As a final detail, if str has more than 63 words, just ignore the extra ones Note: There are many other ways of accomplishing the above breakup of a string into words, like using a static, global array of pointers, etc. As long as you make Phase 2 work, feel free to employ any design for Phase Phase 2: In Phase 2, we develop a simple command interpreter (shell) for Linux. It should be structured as a loop, like: 1. Display a prompt, made up of your first name, followed by the string">" 2. Read a Linux command line from keyboard into some variable like char *str 3. If this line is empty, then exit the program 4. Otherwise, break this line into words by something like char **words (str) 5. Use the system call fork ) to spawn a new child process, like in Figure 1-19 on the textbook Page 55 a. Child process calls execve (w[0], w, 0) to execute the command entered from keyboard b. Parent process employs waitpid) to wait for the child to complete 6. When child terminates, parent resumes, frees all dynamically allocated memory created by words ), and jumps to Step I An Example Run Here's an example run (shell output in red, user input in blue, child process output in black) Rajiv> date Mon Jan 28 15:59:01 CST 2019 Rajiv ps -a PID TTY 60670 ttys000 0:00.08 login -pf rajiv 60674 ttys000 60696 ttys000 Rajiv> ls -1 /Users total 0 drwxr-xr-x+ 11 Guest q drwxrwxrwt 7 root whee1 drwxr-xr-x+ 38 rajiv staff 1292 May 26 2018 rajiv Rajiv> TIME CMD 0:00.03-bash 0:00.00 ps -a uest 374 Dec 9 2015 Guest 238 Oct 17 2015 Shared Suggestions If necessary, read online about Linux system calls fork ), execve, waitpid), and the argument vector argv1, whose structure is similar to the output of our words ) function. Assign ment Submission: Write the entire C code in one source file, hal.c, and submit this source file on Blackboard

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions