#define LOGFILE "signal_log.txt"
// Signal handlers
void sigusr1_handler(int signo);
void sigusr2_handler(int signo);
void sighup_handler(int signo);
// SIGUSR1 handler
void sigusr1_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %d\n",
signo, strsignal(signo), __FUNCTION__, __LINE__);
fflush(stdout);
}
// SIGUSR2 handler
void sigusr2_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %d\n",
signo, strsignal(signo),__FUNCTION__, __LINE__);
fflush(stdout);
}
// SIGHUP handler
void sighup_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %d\n",
signo, strsignal(signo), __FUNCTION__, __LINE__);
fflush(stdout);
}
char *interpret(char *cmdline)
{
char **tokens;
char *cmd;
int i;
char *result;
char sysCommand;
tokens=history_tokenize(cmdline); //Split cmdline into individual words.
if(!tokens) return "no response needed";
cmd=tokens[0];
//Detecting commands: table lookup: 2 techniques
//Using the parallel arrays to look up function calls
for(i=0;commands[i];i++)
{
if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]);
}
//Using struct CMDSTRUCT as an alternative lookup method. Pick either technique, not both
//Note that its possible to create multiple aliases for the same command using either method.
for(i=0;cmdStruct[i].cmd;i++)
if(strcasecmp(cmd,cmdStruct[i].cmd)==0) return (cmdStruct[i].method)(cmd,&tokens[1]);
sysCommand=system(cmdline);
if (sysCommand==-1){
return"command status: fail";
}else if (sysCommand==0){
return"command status: sucsess";}
}
int main(int argc, char * argv[],char * envp[])
{
char cmd[100];
char *cmdLine;
char *expansion;
time_t now=time(NULL);
int nBytes; //size of msg rec'd
char cwd[PATH_MAX];
signal(SIGINT,ctrlCHandler);
read_history("shell.log");
add_history(ctime(&now));
fprintf(stdout,"Starting the shell at: %s\n",ctime(&now));
while(true) {
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
signal(SIGIO, SIG_IGN);
signal(SIGUSR1, sigusr1_handler);
signal(SIGUSR2, sigusr2_handler);
signal(SIGHUP, sighup_handler);
cmdLine=readline("Enter a command: ");
if(!cmdLine) break;
history_expand(cmdLine,&expansion);
add_history(expansion);
if(strcasecmp(cmdLine,"bye")==0) break;
char *response=interpret(cmdLine);
fprintf(stdout,"%s\n",response);
}
write_history("shell.log");
system("echo Your session history is; cat -n shell.log");
fprintf(stdout,"Server is now terminated \n");
retur SIGHUP, Set up SIGIO as a signal to be ignored. (6 marks) a. The initial handler you
should write for each of them should be stub routines that output a message: "Signal \#
(SIGxxxx) received in function _FUNCTION_. Use strsignal to output the name of the signal
using dprintf to send the output to a file. (The tail -f \& command will be demonstrated to allow
you to follow text output to a file while a program is running. Ta.
Hybridoma Technology ( Production , Purification , and Application )
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
1. #define LOGFILE "signal_log.txt"
// Signal handlers
void sigusr1_handler(int signo);
void sigusr2_handler(int signo);
void sighup_handler(int signo);
// SIGUSR1 handler
void sigusr1_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %dn",
signo, strsignal(signo), __FUNCTION__, __LINE__);
fflush(stdout);
}
// SIGUSR2 handler
void sigusr2_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %dn",
signo, strsignal(signo),__FUNCTION__, __LINE__);
fflush(stdout);
}
// SIGHUP handler
void sighup_handler(int signo)
{
dprintf(STDOUT_FILENO, "Signal %d (%s) received in function %s() at line %dn",
signo, strsignal(signo), __FUNCTION__, __LINE__);
fflush(stdout);
}
char *interpret(char *cmdline)
{
char **tokens;
char *cmd;
int i;
char *result;
char sysCommand;
tokens=history_tokenize(cmdline); //Split cmdline into individual words.
if(!tokens) return "no response needed";
2. cmd=tokens[0];
//Detecting commands: table lookup: 2 techniques
//Using the parallel arrays to look up function calls
for(i=0;commands[i];i++)
{
if(strcasecmp(cmd,commands[i])==0) return (methods[i])(cmd,&tokens[1]);
}
//Using struct CMDSTRUCT as an alternative lookup method. Pick either technique, not both
//Note that its possible to create multiple aliases for the same command using either method.
for(i=0;cmdStruct[i].cmd;i++)
if(strcasecmp(cmd,cmdStruct[i].cmd)==0) return (cmdStruct[i].method)(cmd,&tokens[1]);
sysCommand=system(cmdline);
if (sysCommand==-1){
return"command status: fail";
}else if (sysCommand==0){
return"command status: sucsess";}
}
int main(int argc, char * argv[],char * envp[])
{
char cmd[100];
char *cmdLine;
char *expansion;
time_t now=time(NULL);
int nBytes; //size of msg rec'd
char cwd[PATH_MAX];
signal(SIGINT,ctrlCHandler);
read_history("shell.log");
add_history(ctime(&now));
fprintf(stdout,"Starting the shell at: %sn",ctime(&now));
while(true) {
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %sn", cwd);
3. } else {
perror("getcwd() error");
return 1;
}
signal(SIGIO, SIG_IGN);
signal(SIGUSR1, sigusr1_handler);
signal(SIGUSR2, sigusr2_handler);
signal(SIGHUP, sighup_handler);
cmdLine=readline("Enter a command: ");
if(!cmdLine) break;
history_expand(cmdLine,&expansion);
add_history(expansion);
if(strcasecmp(cmdLine,"bye")==0) break;
char *response=interpret(cmdLine);
fprintf(stdout,"%sn",response);
}
write_history("shell.log");
system("echo Your session history is; cat -n shell.log");
fprintf(stdout,"Server is now terminated n");
retur SIGHUP, Set up SIGIO as a signal to be ignored. (6 marks) a. The initial handler you
should write for each of them should be stub routines that output a message: "Signal #
(SIGxxxx) received in function _FUNCTION_. Use strsignal to output the name of the signal
using dprintf to send the output to a file. (The tail -f & command will be demonstrated to allow
you to follow text output to a file while a program is running. Take notes!) Display the name of
the file, when it was compiled and the line # of the output message. (2 marks) b. Test all of the
above signal handlers. (2 marks) i. Verify that all of your signal handlers work sending your
command servers each of the above signals from a 2nd terminal. ii. To set up a log file to record
the output of dprintf(fd, fmtstr, argsI) use one of more of the following before running your
command server: exec fd>/dev/tty #isplays the messages on the current terminal exec
fd>/dev/pts/n #display on a different terminal that you own exec fd>logfile #writes to a file tail
-f logfile & #displays new data as it is appended to logfile tail -f logfile >/dev/pts/n &
#displays new data on an alternate terminal Hand in a log file (as opposed to a screen shot)
showing that you tested all of the signals c. In a 3rd terminal attach strace to the pid of your
4. command server. (2) i. Send each of the 4 signals to the pid of the command server. How does
strace respond to each signal. Summarize the result. ii. Send each of the 4 signals to the parent
pid of the command server. Answer the same question and hilite/describe any difference.