Signals • Change your shell program to display the list of history commands when
ID: 3795705 • Letter: S
Question
Signals
• Change your shell program to display the list of history commands when the user presses ctrl-c (which is the SIGINT signal). See course website for a guide on using signals. • In main(), register a custom signal handler for the SIGINT signal.
• Have the signal handler display the previous ten user commands (same as history command).
• Then re-display the command-prompt before returning.
• Note that when another child process is running, ctrl-c will likely cancel/ kill that process. Therefore displaying the history with ctrl-c will only be tested when there are no child processes running.
• Suggestions
• To implement this, you will also need change read_command() a little bit.
• The signal handler will do nothing but displaying the history commands and then display the prompt again. The signal will interrupt the read() system call and discard all data already read for this command.
• When read() fails, it returns -1. You can check why read fails: if it returns -1 and the environment variable errno equals EINTR it means that it was interrupted by a signal. If the return value is -1 and errno is any other value, it means read just failed and the program should exit.
• To correctly check read()'s return value you can change the code that you now have: if (length < 0){
• perror("Unable to read command. Terminating. ");
• exit(-1); /* terminate with error */
• }
• if ( (length < 0) && (errno !=EINTR) ){
• perror("Unable to read command. Terminating. ");
• exit(-1); /* terminate with error */
• }
Explanation / Answer
LIST OF HISTORY COMMANDS :
1.PS1='e[1;35m[s@h d]$ e[m '
2.PS1="e[0;26[s@h d]$ e[m"
3.PS1="s@h:e [j]$ "
4.ping google.com
5.echo $PS1
6. tail -f/var/log/messages
7.tail -f/var/log/messages
8.exit
9.clear
10.history
NOTE: The system can be interrupted by any signals such as SIGINT which is generated by CTRL -C.The SA_RESTART is set the send() will return with sent count when any data was transmitted before the signals received then it will return an error EINTR.
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
void main (int argc , char * argv[])
{
int fd1,fd2,count;
char buf[512];
if ([count=read(fd1,buf,512)]>0)
{
write(fd2,buf;count);
close(fd1);
close(fd2);
printf("read the system calls");
else
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.