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 Description
Objectives
Refreshpractice 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
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 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 Improve a basic command line shell
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
of
Program : miniShell Version :
skeleton code for linixunixminix command line interpreter
File : minishell.c
CompilerSystem : gcclinux
#include
#include
#include
#include
#include
#include
#include
#define NV max number of command tokens
#define NL input buffer size
char lineNL; command input buffer
shell prompt
promptvoid
fprintfstdout
msh;
fflushstdout;
mainint 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 vNV; 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 do Forever
prompt;
fgetsline NL stdin;
fflushstdin;
if feofstdin nonzero on EOF
fprintfstderr "EOF pid d feof d ferror d
getpid
feofstdin ferrorstdin;
exit;
if line# line
line
continue; to prompt
v strtokline sep;
for i ; i NV; i
vi strtokNULL sep;
if vi NULL
break;
assert i is number of tokens
fork a child process to exec the command in v
switch frkRtnVal fork
case : fork returns error to parent process
break;
case : code executed only by child process
execvpv v;
default: code executed only by parent process
wpid wait;
printfs done
v;
break;
switch
while
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
