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

Write and test a C++ (or Java) that performs the following. a) A parent process

ID: 3621887 • Letter: W

Question

Write and test a C++ (or Java) that performs the following.

a) A parent process that creates a child process and prints its PID.
b) the child creates a grandchild and sleeps 160 seconds.
c) The grandchild then exits.
d) The parent sleeps for 80 seconds.
e) Find out whether there are zombie processes in the program that you have written. If not, modify your program to create at list one zombie.

Note: You may use either fork () in Linux family or CreateProcess( ) in Windows family.

Explanation / Answer

READ CAREFULLY..IT MAY HELP U Java and C++ Interaction Java allows invocation of C/C++ (and other languages) APIs from Java code by using the so called Java Native Interface (JNI). In JNI, native functions are implemented in separate *.c or *.cpp files. (C++ provides a slightly cleaner interface with JNI.). When the JVM invokes the native function, it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method. The details of JNI interface are beyond the scope of this article. For our purposes, it is just important to mention that every C/C++ function/method that you want to call from Java must be wrapped by C function (JNI wrapper) that will be called directly from Java and will in turn invoke the required C/C++ API. // JNI wrapper for void MyClass::MyClassMethod(const char*) C++ method JNIEXPORT void JNICALL Java_JNI_MyClass_MyClassMethod (JNIEnv *env, jobject obj, jstring javaString) { //Get the native string from javaString const char *nativeString = env->GetStringUTFChars(javaString, 0); // Invoke the native C/C++ function here MyClass *myobj = *(MyClass **) &obj; myobj->MyClassMethod(nativeString); //DON'T FORGET THIS LINE!!! env->ReleaseStringUTFChars(javaString, nativeString); } The JNI wrappers can be written manually or can be generated automatically by Swig utility from the C/C++ API function declarations. Debugging C++ at Run-time Our goal is to be able to invoke the C++ debugger on demand when execution flow of Java application passes to the C++ code through the JNI wrapper. In Linux, the C++ debugger (gdb) can be attached to the already running process (Java application in our case) using the process identifier (pid). Each Linux process has its own unique integer identifier that can be obtained by calling getpid() C system function from within the process. Moreover it is possible to attach the C++ debugger to a running process just-in-time from a certain point in C/C++ code of this process. This allows starting debugging at the specific C/C++ code location in the code. The following C code attaches the debugger from within an already running process. The code uses fork() C system function that spawns an additional child process that is used to run the C++ debugger. int gdb_process_pid = 0; void exec_gdb() { // Create child process for running GDB debugger int pid = fork(); if (pid < 0) /* error */ { abort(); } else if (pid) /* parent */ { // Application process gdb_process_pid = pid; // save debugger pid sleep(10); /* Give GDB time to attach */ // Continue the application execution controlled by GDB } else /* child */ { // GDB process. We run DDD GUI wrapper around GDB debugger stringstream args; // Pass parent process id to the debugger args
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