Question: Practical 1 Description Objectives Refresh / practice some basic C programming Familiarise yourself with the Unix system call mechanism Learn about processes and signals Get

Practical 1 Description
Objectives
Refresh/practice some basic C programming
Familiarise yourself with the Unix system call mechanism
Learn about processes and signals
Get you thinking about parallel processes, as well as what the unix shell really does.
This assignment consist of two tasks:
Task 1(30%)
Write a C program called even.c that takes the input parameter n from the command line and prints the first " n " even numbers. We want the program
to run slowly, so that it executes long enough to receive a signal. To achieve this, you should place a sleep(5) after every print statement. Compile
and run to test it works ok.
Most Unix systems will understand SIGHUP and SIGINT. Every signal has a default action associated with it, for example the default action for
SIGINT is to terminate the process.
You should modify your even number program to handle the signals as follows:
When it receives a HUP signal, it should print "Ouch!" and continue.
When it receives a INT signal, it should print "Yeah!" and continue.
Task 2 Improve a basic command line shell (70%)
Read, study and completely understand the minishell..darr, code provided for this assignment.
Modify that minishell to do the following:
put commands ended by an " & " into background mode and report when they finish.
properly interpret the shell cd command
include an appropriate perror statement after each and every system call.
fix the minishell so that if the exec system call fails, the child process is correctly terminated.
The minishell should follow the POSIX API, thus you may develop you code on a Linux machine. We will provide some simple test for testing
minishell.c:
Page
2
of 2
/*********************************************************************
Program : miniShell Version : 1.3
--------------------------------------------------------------------
skeleton code for linix/unix/minix command line interpreter
--------------------------------------------------------------------
File : minishell.c
Compiler/System : gcc/linux
********************************************************************/
#include
#include
#include
#include
#include
#include
#include
#define NV 20/* max number of command tokens */
#define NL 100/* input buffer size */
char line[NL]; /* command input buffer */
/*
shell prompt
*/
prompt(void)
{
fprintf(stdout,"
msh>");
fflush(stdout);
}
main(int argk, char *argv[], char *envp[])
/* argk - number of arguments */
/* argv - argument vector from command line */
/* envp - environment pointer */
{
int frkRtnVal; /* value returned by fork sys call */
int wpid; /* value returned by wait */
char *v[NV]; /* array of pointers to command line tokens */
char *sep ="\t
";/* command line token separators */
int i; /* parse index */
/* prompt for and process one command line at a time */
while (1){/* do Forever */
prompt();
fgets(line, NL, stdin);
fflush(stdin);
if (feof(stdin)){/* non-zero on EOF */
fprintf(stderr, "EOF pid %d feof %d ferror %d
", getpid(),
feof(stdin), ferror(stdin));
exit(0);
}
if (line[0]=='#'|| line[0]=='
'|| line[0]=='\000')
continue; /* to prompt */
v[0]= strtok(line, sep);
for (i =1; i < NV; i++){
v[i]= strtok(NULL, sep);
if (v[i]== NULL)
break;
}
/* assert i is number of tokens +1*/
/* fork a child process to exec the command in v[0]*/
switch (frkRtnVal = fork()){
case -1: /* fork returns error to parent process */
{
break;
}
case 0: /* code executed only by child process */
{
execvp(v[0], v);
}
default: /* code executed only by parent process */
{
wpid = wait(0);
printf("%s done
", v[0]);
break;
}
}/* switch */
}/* while */
}/* main */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!