Question

In: Computer Science

Modify the below program so that the following two conditions are met: 1. Each child terminates...

Modify the below program 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

Hint: Read the man page for psignal(3)

___________________________________.

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/wait.h>

void unix_error(char *msg) /* unix-style error */

{

fprintf(stderr, "%s: %s\n", msg, strerror(errno));

exit(0);

}

#define N 2

int main()

{

int status, i;

__pid_t pid;

/* Parent creates N children */

for (i = 0; i < N; i++)

if ((pid = fork()) == 0) /* child */

exit(100+i);

/* Parent reaps N children in no particular order */

while ((pid = waitpid(-1, &status, 0)) > 0) {

if (WIFEXITED(status))

printf("child %d terminated normally with exit status=%d\n",

pid, WEXITSTATUS(status));

else

printf("child %d terminated abnormally\n", pid);

}

/* The normal termination is if there are no more children */

if (errno != ECHILD)

unix_error("waitpid error");

exit(0);

}

Solutions

Expert Solution

Answer:

Please find the complete program for the question below.

#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], retpid;

        /* 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 ((retpid = waitpid(pid[i++], &status, 0)) > 0) {
                if(WIFEXITED(status))
                        printf("child %d terminated normally with exit status=%d\n", retpid, WEXITSTATUS(status));
                else if(WIFSIGNALED(status)) {            /* Exited with a signal */
                        printf("child %d terminated by signal %d: \n", retpid, SIGSEGV);
                        psignal(WTERMSIG(status), NULL);
                }
                else
                        printf("child %d terminated abnormally \n", retpid);
        }

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

        exit(0);
}


Related Solutions

In this exercise, find the values of the unknown constants so that the indicated conditions are met for each function:
In this exercise, find the values of the unknown constants so that the indicated conditions are met for each function: (a) f(t) = At2 + B; given f(0) = 2 and f(−2) = −10. (b) f(t) = Ct3 + D; given f(0) = −5 and f(3) = 49. (c) f(t) = A t 2 + B , given f(0) = 3 and f(−2) = 1.5 (d) f(t) = A t + B , given f(2) = 0.4 and f(8) =...
Determine if all of the conditions have been met for the following hypothesis tests. If so,...
Determine if all of the conditions have been met for the following hypothesis tests. If so, write the hypotheses and identify the claim. (Do not conduct a full hypothesis test.) (2 points for checking conditions and 2 points for hypotheses) a. The Pew Research Center conducted a survey of 500 adults and found that 429 of them know what Twitter is. Use a 0.01 significance level to test the claim that at least 80% of adults know what Twitter is....
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
How can i draw feynman diagram so that all the conditions are met
How can i draw feynman diagram so that all the conditions are met
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT