Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write it in in C/C++! Ignoring the SIGHUP and SIGQUIT signals This assignment in

ID: 3838688 • Letter: W

Question

Write it in in C/C++!

Ignoring the SIGHUP and SIGQUIT signals

This assignment involves the writing of a simplified version of the shell utility nohup called my_nohup. The utility is used to prevent processes from being killed by the SIGHUP or SIGQUIT signals when the user logs off from a session.

First read the man pages on nohup to understand how the utility works. Also read the man pages on the function isatty().

Your code will carry out the following actions:

If the user supplies no command line arguments (other than the name of the executable) write an informative message to stderr (STDERR_FILENO) and exit with an error value.

Use the function isatty(int filedesc) to determine if the standard output file descriptor is associated with a terminal file. If it is, your code needs to redirect standard output to the file nohup.out (which may already exist.) nohup.out needs to be created or opened for writing. If you need to create the file, give it permissions such the owner has read/write permission. When this section of code is completed, if isatty() returned true, the file descriptior originally assigned to STDOUT_FILENO should be associated with nohup.out. If you are unable to open/create nohup.out, write an informative error message and exit with an error value.

If standard error, STDERR_FILENO, is associated with a terminal device, close it and associate file descriptor 2 with nohup.out.

Ignore SIGHUP and SIGQUIT. You must do this using sigaction() and the appropriate macros. If you are unable to ignore these signals, write an informative message to stderr and exit with an error value.

The new program is the first command line argument to my_nohup. exec*() this program and its arguments, e.g.

In this example, my_nohup would use exec*() to execute the program testsim with the command line arguments 5 and 10.

If the exec*() fails, write out an informative message using either perror() or strerror(). Exit with an error value.

Note: the standard output of the new program will be written to nohup.out.

Please put all your functions in a file called my_nohup.c

a code that compiles and runs with no undersirable side affects. It protects the process from both SIGHUP and SIGQUIT and successfully redirects stdout and stderr.

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

void handle_signal(int sig) // this is a signal handler function
{
   if (sig == SIGHUP)
   {
       printf("SIGHUP caught ");
   }
   else if (sig == SIGQUIT)
   {
       printf("SIGQUIT caught ");
   }
}

void main(int argc, char **argv)
{
   if(argc <=1)
   {
       fprintf(stderr, "Less no of arguments supplied");
       exit(0);
   }
   int fd= open("nohup.out", O_WRONLY|O_CREAT, 0666); //opening nohup.out to write stdout and stderr into

   if(fd < 0)
   {
       perror("open");
   }
   if(isatty(1))
   {
       close(1); //closing stdout file descriptor
       dup(fd); // assigning stdout to nohhup.out file descriptor
   }
   else
   {
       printf("fd is not reffering to a output terminal ");
   }
   if(isatty(2))
   {
       close(2); //closing stderr file descriptor
       dup(fd); //assigning stderr to nohup.out file descriptor
   }
   else
   {
       printf("stderr is not referring to output termainl ");
   }

   struct sigaction sa;

   // Setup the sighub handler
   sa.sa_handler = &handle_signal;

   // Block every signal while executing handler
   sigfillset(&sa.sa_mask);

   if (sigaction(SIGHUP, &sa, NULL) == -1) // handling SIGHUP
   {
   perror("Error: cannot handle SIGHUP");
   }
   if (sigaction(SIGQUIT, &sa, NULL) == -1) // handling SIGQUIT
   {
   perror("Error: cannot handle SIGQUIT");
   }

//type your file path in first argument of execlp
   if((execlp( "/home/user/testsim","testsim","argv[2]","argv[3]",NULL))==-1)
   {
       perror("execlp");
       exit(0);
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote