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

Section 1 Write a program in C/C++ that: • Spawns two processes. • Each process

ID: 3676437 • Letter: S

Question

Section 1
Write a program in C/C++ that:
• Spawns two processes.
• Each process executes an infinite loop and waits for the parent process to send them a signal.
• After 5 seconds, the parent sends the signal SIGINT to both children.
• Upon receiving the signal, the children print their own pid and the pid of the parent and terminate.
Section 2
Modify the program from Section 1 so that upon receiving the signal:
• the first child process, lists all the elements in the “/home” directory
• the second child process, sends a SIGQUIT signal to the parent
• when the parent receives the SIGQUIT signal it prints the message “Now I have to terminate” and exits. The parent should not terminate otherwise.
Section 3
Write a program that creates three processes and gets as an input from command line an integer number N (N > 10).
• The first process prints the numbers [0, N/3) continuously (the process should never finish).
• The second process prints the numbers [N/3, 2N/3) continuously (the process should never finish).
• The third process prints the numbers [2N/3, N] continuously (the process should never finish).
• After 5 seconds the father should send a SIGKILL signal to the three children.

Explanation / Answer

void noteProcDeath( CFFileDescriptorRef fdref, CFOptionFlags callBackTypes, void* info) { // LOG_DEBUG(@"noteProcDeath... "); struct kevent kev; int fd = CFFileDescriptorGetNativeDescriptor(fdref); kevent(fd, NULL, 0, &kev, 1, NULL); // take action on death of process here unsigned int dead_pid = (unsigned int)kev.ident; CFFileDescriptorInvalidate(fdref); CFRelease(fdref); // the CFFileDescriptorRef is no longer of any use in this example int our_pid = getpid(); // when our parent dies we die as well.. LOG_INFO(@"exit! parent process (pid %u) died. no need for us (pid %i) to stick around", dead_pid, our_pid); exit(EXIT_SUCCESS); } void suicide_if_we_become_a_zombie(int parent_pid) { // int parent_pid = getppid(); // int our_pid = getpid(); // LOG_ERROR(@"suicide_if_we_become_a_zombie(). parent process (pid %u) that we monitor. our pid %i", parent_pid, our_pid); int fd = kqueue(); struct kevent kev; EV_SET(&kev, parent_pid, EVFILT_PROC, EV_ADD|EV_ENABLE, NOTE_EXIT, 0, NULL); kevent(fd, &kev, 1, NULL, 0, NULL); CFFileDescriptorRef fdref = CFFileDescriptorCreate(kCFAllocatorDefault, fd, true, noteProcDeath, NULL); CFFileDescriptorEnableCallBacks(fdref, kCFFileDescriptorReadCallBack); CFRunLoopSourceRef source = CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, fdref, 0); CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopDefaultMode); CFRelease(source); }

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