Solved-Mini Programming Questions #1–Solution

$30.00 $19.00

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 le named redirect out.txt, perform redirection then restore stdout (hint: use dup, dup2). In particular, the rst and third printf statements should be written to standard out, and the…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)
  • Mini Programming Questions

  1. Redirection [5 marks]

Given the following C program that prints data on screen, modify the program in order to redirect stdout to a le named redirect out.txt, perform redirection then restore stdout (hint: use dup, dup2). In particular, the rst and third printf statements should be written to standard out, and the second line should be written to le. Please save your code in a le 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 ( )

f

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

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 nn ” ) ;

p r i n t f ( ” Third :

P r i n t

t o

s t d o u t nn ” ) ;

r e t u r n ;

g

  1. Inter Process Communication [10 marks]

The following program serves as an example for inter process communication.Given below C program needs to be modi ed 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 le 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 ( )

f

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

f

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

g

e l s e

f

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

g

r e t u r n 0 ;

g

1

  • Emulating Shell Interface

  1. 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 speci ed with information ags 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:

    1. 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 speci cation please refer to corresponding section in Additional Information section.

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

    1. Nice pre x 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 nish rst and then start its own execution by pre xing it with “nice”.

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

  1. Useful Information

    1. 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 rst argument is the command and the second argument in the ag for that command.This function also sets a background variable to 1 if it encounters & and then removes & from the string.

    1. 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.

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

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

      1. wc: Outputs either the number of lines (l ag .ie wc -l lename ) or number of words (w ag .ie wc -w lename ) present in the le provided by user.

2

      1. exit: Requires you to exit your terminal emulator.

      1. 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]

    1. 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 nd executable les with command names.Refer to the tutorial on Assignment 1 to understand using fork-execvp-waitpid trio.

    1. 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.

    1. Nice pre x If a command is run with nice pre x 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.

    1. 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 le provided by the user.It should handle the case in which le does not exist(ie. create that le else append it to existing le).

  1. 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 speci ed in this assignment statement will also result in penalty.

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

||||XX||||

3