Question

I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

I have the following code in C:

void sighandler(int sig)

{

     printf("exiting child...");

}

int main(int argc,char* argv[])

{

     if(argc > 1)

     {

           char* commandName;

           for(int i=2;i

           {

                  forkChild = fork();

                  signal(SIGINT,sighandler);

                  if(forkChild == 0)

                  {

                         execlp(commandName,commandName,argv[i],NULL);

                         exit(0);

                  }

                  else

                  {

                         wait(NULL);

                  }

      }

My problem is that I would like to kill the child with ^C but leave the parent running. Does interrupting with cntrl- c always terminate the entire program? How can I just close the child but leave the loop open?

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

fork() will return a -1 if it cannot create a child and a 0 in parent and a value greater than 0 in parent which is the pid of child. here in this program, forkChild is used to store the returned value from fork(). so you can use kill(forkChild,SIGKILL);

to kill the child. you need to use the header #include<signal.h> while using SIGKILL to kill the child.

Add a comment
Know the answer?
Add Answer to:
I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf("...

    include «stdio.h void displaymenu(void) printf(" printf" Enter Choicen") printf Set (a), Clear (c), Toggle (t)\n"): printf(" void printbinaryfunsigned char x) int main (int argc, char argvl) r mask atoi(argv[1): unsigned char maskatoi(argv[11): ensigned ahao data - ateilargvl21): char operation if argc 3 printf(" printf" n" printf("usage: %s FristArg SecondArgin", argle)); return;

  • 5. Answer the questions on the following program: #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int...

    5. Answer the questions on the following program: #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int sig) { printf("OUCH! -­‐ I got signal %d\n", sig); } int main() { signal(SIGINT, ouch); signal(SIGQUIT, ouch); while(1) { printf("Hello World!\n"); sleep(1); } } 1) What is the output of just running the program? 2) What will happen if you try to use both keyboard signals of terminal interrupt and terminal quit: Ctrl-­‐C and Ctrl-­‐\ to kill the process? 3) Give exactly necessary commends...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Explain in words, what the issue with this code? (Assume there are no syntax errors) void sigint_handler (int signo) { printf ("Caught SIGINT!\n"); exit (EXIT_SUCCESS); } int main (void) { if...

    Explain in words, what the issue with this code? (Assume there are no syntax errors) void sigint_handler (int signo) { printf ("Caught SIGINT!\n"); exit (EXIT_SUCCESS); } int main (void) { if (signal (SIGKILL, sigint_handler) == SIG_ERR) { exit (EXIT_FAILURE); } for (;;) pause ();        return 0; }

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT