Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write in C Language. Show terminal of output also. Phase 1: In Phase 1, write a function words ) that takes a character string
Please write in C Language. Show terminal of output also.
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 *stri The words in str are separated by whitespace, i.e. blanks, tabs, and newlines. The words () function detects all words in str, copies each word into some new space allocated by malloc), and returns an array with pointers to these new spaces. The array 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 pointer As an example, if str = "Rajiv Bagai rocks ! ! ", then it has 3 worls, 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 1 Phase 2: In Phase 2, we will 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 **wwords (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 execvp (wIO], w) 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 1 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 TIME CMD PID TTY 60670 ttys000 60674 ttys000 60696 ttys000 Rajiv> ls1 /Users total 0 drwxr-xr-x+ 11 Guest guest 374 Dec 9 2015 Guest drwxrwxrwt 7 root wheel drwxr-xr-xt 38 rajiv staff 1292 May 26 2018 rajiv Rajiv> 0:00.08 login -pf rajiv 0:00.03-bash 0:00.00 ps -a 238 Oct 17 2015 Shared Suggestions: If necessary, read online about Linux system calls fork), execvp ), waitpid), and the argument vector argv[1, whose structure is similar to the output of our words ) function. Assignment Submission: Write the entire C code in one source file, hal.c, and submit this source file on BlackboardStep 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