Starting from:
$30

$24

Assignment #1 Solution




1 Mini Programming Questions




Redirection [5 marks]



Given the following C program that prints data on screen, modify the program in order to redirect stdout to a file named redirect out.txt, perform redirection then restore stdout (hint: use dup, dup2). In particular, the first and third printf statements should be written to standard out, and the second line should be written to file. Please save your code in a file named a1 redirect.c.







#i n c l u d e <s t d i o . h




#i n c l u d e <u n i s t d . h




i n t main ( )




{

p r i n t f ( ” F i r s t : P r i n t t o s t d o u t \n ” ) ;

p r i n t f ( ” Second
: P r i n t
t o
r e d i r e c t


o u t . t x t \n ” ) ;
p r i n t f ( ” Third :
P r i n t
t o
s t d o u t \n ” ) ;
r e t u r n ;










}













Inter Process Communication [10 marks]



The following program serves as an example for inter process communication.Given below C program needs to be modified so that it execute an ls command in the child process, and prints the output of the command in the parent process.Please save your code in a file named a1 command piping.c.(Hint: Use pipe,fork and execvp)







#i n c l u d e <s t d i o . h




#i n c l u d e <u n i s t d . h




i n t main ( )




{

i f ( f o r k ()==0)




{

// C h i l d : e x e c u t e l s u s i n g execvp




}

e l s e




{

// Parent : p r i n t output from l s h e r e




}

r e t u r n 0 ;




}







1
2 Emulating Shell Interface




Requirements



In this part of the assignment you are required to write a C program (using the skeleton provided) that emulates a terminal. The actual terminal has a provision which allows a command to run in the background. This is done by appending & to the command. However not all commands can be executed in background. Each command below is specified with information flags about this [F- foreground, B- background] . Now since the terminal has several commands, you are expected to implement a subset of commands which are mentioned below:




Execution of built-in commands which include cd[F],wc[F],jobs[F],exit[F].To get a better understanding of built-in commands and its specification please refer to corresponding section in Additional Information section.



Execution of executable commands. There are several commands that exits as executable files in bin directory.These include date,who,ls etc and can be run using execvp() function. Note that every executable command is [BF].



Nice prefix for commands Any command that is being run in foreground (this in-clude commands that are BF and F) can be made to wait for background commands to finish first and then start its own execution by prefixing it with ”nice”.



Output Redirection is the feature that allows you to place the results of a com-mand execution in a file provided (as filename) by the user.For ex. lsfilename.txt. Note that this feature is expected to work only with the executable commands.



Useful Information



Processing input text The skeleton code provides you a function that takes user



input and breaks it into tokens of array of arguments. In all cases the first argument getcmd is the command and the second argument in the flag for that command.This function




also sets a background variable to 1 if it encounters & and then removes & from the string.




Background and Foreground : This point applies only to executable commands. To enforce this feature you need to exploit the power of fork. By fork ,create a child process and in the child process you could have your commands run (which are suitable) using execvp. In case of the command being run as background the parent process need not wait the child process to terminate (look for suitable attribute for waitpid function) and in case of foreground the parent process has to wait for the child process to terminate.



Built-in commands : These commands are always run in the foreground and hence there is no need to fork.



cd: Requires you to call chdir system call.Takes destination directory as ar-gument.If no argument is provided then change to home directory.



wc: Outputs either the number of lines (l flag .ie wc -l filename ) or number of words (w flag .ie wc -w filename ) present in the file provided by user.






2



exit: Requires you to exit your terminal emulator.



jobs: Requires you to print the details of the jobs that are running the back-ground.Essentially refresh the linked list , traverse it and print [ID, PID, Com-mand, Status, Spawn time]



Executable Command All the executable are of the type [BF] and require fork-execvp-waitpid calls.If you want to have a look at all the commands that can be run using execvp. Look into bin directory and you shall find executable files with command names.Refer to the tutorial on Assignment 1 to understand using fork-execvp-waitpid trio.



Augmented Execution Time All the commands in terminal execute very fast and you wouldn’t be able to notice the background execution . In order to make it visible, the skeleton has a function which deliberately introduces random delay (0-15 seconds) before execution.You are requested to place this function calls at suitable location for the above purpose.



Nice prefix If a command is run with nice prefix that means before doing fork-execvp-waitpid, continuosly (at intervals of 1 sec ) check the linked-list untill it becomes empty.Only then proceed with running the command. There exist a func-tion in the skeleton that halts the parent process until the linked list is empty.Use this function cautiously.



Output Redirection You are expected to manipulate stdout stream (using dup or dup2) and then restore it, to dump the output of a command to a text file provided by the user.It should handle the case in which file does not exist(ie. create that file else append it to existing file).



Marking Scheme



Criteria
Weightage
A simple shell that runs: shows prompt, runs executable commands,


goes to the next one, account for errors in inputs.
25
Built-in commands
30
Nice Command
15
Output Redirection
10
Code comments and documentation
5



You are provided with skeleton code and are expected to use it in order to complete your assignment. Failure to do so will result in penalty. Any additional functional-ity that is not specified in this assignment statement will also result in penalty.




A penalty of 5% shall be applied if the submission guidlines specified in the submission folder description are not followed.




————XX————






















3

More products