Question
  • Modify the program in Figure 8.18 so that the following two conditions are met:
    1. Each child terminates abnormally after attempting to write to a location in the read-only text segment.
    2. The parent prints output that is identical (except for the PIDs) to the following: child 12255 terminated by signal 11: Segmentation fault
    child 12254 terminated by signal 11: Segmentation fault

  • Modify your solution to Problem 8.24 so that one (and only one) child installs a Segmentation-fault handler which prints an error message and exits. What is the output of the program after this change?/* Figure 8.18 */ #include csapp.h #define N 2 int main() int status, i; pid_t pid; /* Parent creates N children */ for (i

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

Do like and comment if you have any queries.

Note: Used the code given in the question and made modifications

Code to copy:

   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <errno.h>
   #include <unistd.h> /* psignal */
   #include <sys/types.h> /* waitpid */
   #include <sys/wait.h> /* waitpid */

   #define N 2

   int main() {
      
           int status, i;
           pid_t pid[N], ret_pid;

           /* Parent creates N children */
           for(i = 0; i < N; i++) {
                   if((pid[i] = fork()) == 0) { /* child */
                           strcpy("Hello", "World"); /* Writing on read-only location. It creates SIGSEGV */
                           exit(100 + i);
                   }
           }

           /* Parent reaps N children in order */
           i = 0;
           while ((ret_pid = waitpid(pid[i++], &status, 0)) > 0)
           {
                   if(WIFEXITED(status))
                           printf("child %d terminated normally with exit status=%d\n", ret_pid, WEXITSTATUS(status));
                   else if(WIFSIGNALED(status)) { /* Exited with a signal */
                           printf("child %d terminated by signal %d: \n", ret_pid, SIGSEGV);
                           psignal(WTERMSIG(status), NULL);
                   }
                   else
                           printf("child %d terminated abnormally \n", ret_pid);
           }

           /* The only normal termination is if there are no more children */
           if(errno != ECHILD)
                   printf("waitpid error");

           exit(0);
   }

Code Screenshot:

со ја са в #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> /* psignal */ #i

/* Parent reaps N children in order / i = 0; while ((ret_pid = waitpid (pid[i++], &status, 0)) > 0) if (WIFEXITED (status)) p

Output Screenshot:

} clang-7 -pthread -lm -o main main.c } ./main child 64 terminated by signal 11: Segmentation fault child 65 terminated by si

Add a comment
Know the answer?
Add Answer to:
Modify the program in Figure 8.18 so that the following two conditions are met: 1. Each...
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
  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • 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...

  • Suppose that you have three programs that are suppose to print a house diagram in a...

    Suppose that you have three programs that are suppose to print a house diagram in a collaborative manner. (i) prog1.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“                      ^^^^^^^^^^^^                       \n”);     printf(“           ^^^^^^^^^^^            ^^^^^^^^^^^            \n”);     printf(“^^^^^^^^^^^                                  ^^^^^^^^^^^ \n”);     printf(“     |                                            |      \n”);     printf(“     |                                            |      \n”);     exit(1); } (i) prog2.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“     |                                             |    ...

  • 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()...

  • Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...

    Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include <stdlib.h> <stdio.h> <string.h> <errno.h> <unistd.h> #include <sys/types.h> #include <sys/ipc.h> Winclude <sys/msg.h> struct my msg st f long int...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • C PROGRAM The following is code prints the current activation record number, the memory address of...

    C PROGRAM The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the...

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