Question

In: Computer Science

Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...

Includes you will need:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <signal.h>

#include <sys/wait.h>

Create a .c file that does the following:

1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7)

• Note: The parent should save the pid_t of the child (returned from fork()) for later.

2. Have the child register a signal handler for SIGTERM using the method we saw. Have your SIGTERM handler print a message and then call exit(). (APUE chapter 10)

• Hint: functions in C need to be declared before they are used. Normally this happens in a header file, but within your .c file you have two options: •Implement the function above main().

• Put a function signature above main, such as static void mySignalHandler(int);, and then implement the function below main.

3. Have the child go to sleep using this code (which will keep it asleep indefinitely): for ( ; ; ) { pause(); }

4. Have the parent sleep for 5 seconds sleep(5); (an imperfect way to ensure that the child has time to register its signal handler), then use the kill() command to send SIGTERM to the child. Note that SIGTERM is a defined constant; you don’t need to look up the number. (APUE chapter 10)

5. Have the parent do a waitpid(…) on the child’s pid (which should come back quickly since we just asked him to exit). Print a message saying that the child has exited. (APUE chapter 8)

6. Have the parent register an atexit() function that prints a message as you exit. (APUE chapter 7)

7. Let the parent exit (fall out of main, return from main, or call exit()).

What should happen when you run the program:

• The parent and child will print who is who (in either order, or even with characters overlapping—this is okay).

• After 5 seconds, you should see the message from the child’s signal handler (#2 above).

• Right after that you should see the message saying that the child has exited (#5 above).

• Right after that you should see the message from atexit() (#6 above).

Solutions

Expert Solution

Please find the following c program.

Program:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

//signal handler
static void mySignalHandler(int);

//customized exit handler
void customExit(void);

void main()
{
pid_t ch_pid;

/* get child process */
if ((ch_pid = fork()) < 0)
{
perror("fork");
exit(1);
}

if (ch_pid == 0)
{ /* child process block*/
printf("I am child process with process id as: %d.\n", getpid());
//register the signal handler
signal(SIGTERM, mySignalHandler);
for(;;)
{
pause();
}
}
else /* parent process block*/
{
printf("I am parent process with process id as: %d.\n", getpid());
sleep(5);
printf("PARENT: sending SIGTERM\n\n");
//parent kills the child process
kill(ch_pid, SIGTERM);
//parent waits for child process to get completed
if(waitpid(ch_pid, NULL, 0))
{
printf("PARENT: Child with process id %d is exited.\n", ch_pid);
}
//register the exit function pointer
if( atexit(customExit) != 0 )
{
printf("Unable to set customized exit function\n");
return;
}

return;

}
}


static void mySignalHandler(int signal)
{
printf("CHILD: I have received a signal SIGTERM(%d)\n", signal);
exit(0);
}


void customExit(void)
{
printf("PARENT has exited..\n");
}

Output:

USER>./a.out
I am parent process with process id as: 106885.
I am child process with process id as: 106886.
PARENT: sending SIGTERM

CHILD: I have received a signal SIGTERM(15)
PARENT: Child with process id 106886 is exited.
PARENT has exited..
USER>

Screen Shot:


Related Solutions

#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except that you should read the number using scanf() * and the string to print using fgets() (up to 1024 characters.) Use "num: " as * the prompt for the number and "str: " as the prompt for the string. Keep in * mind that the newline that is entered will still be in the string when * printing it. NOTE: After the scanf() for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT