Discuss a parent process or in the child process


Discuss the below:

Q1. Explain exactly what this code is doing as it is written right now.

Q2. Where is the code following (textually) the "switch" block executed - in the parent process or in the child process? The first instruction following the "switch" block is "act.sa_handler = setflag".

Q3. Change the code so that no code appears _textually_ after the "switch" block. All the code following the "switch" block must be placed somewhere else, yet the effect of the "tml" command must be the same.#include

#include
#include
#include
#include

#define TRUE 1
#define FALSE 0
#define BELLS "\007\007\007" /* ASCII bells */

int alarm_flag = FALSE;

/* routine to handle SIGALRM */
void setflag(int sig)
{
alarm_flag = TRUE;
}

main (int argc, char **argv)
{
int nsecs,j;
pid_t pid;
static struct sigaction act;

if( argc <= 2 )
{
fprintf(stderr, "Usage: tml minutes message\n");
exit(1);
}

if( (nsecs = atoi(argv[1]) * 60) <= 0)
{
fprintf(stderr, "tm1: invalid time\n");
exit(2);
}

/* fork to create background process */
switch(pid = fork()){
case -1: /* error */
perror("tml");
exit(1);
case 0: /* child */
break;
default: /* parent */
printf("tm1 process-id %d\n", pid);
exit(0);
}

/* set action for alarm */
act.sa_handler = setflag;
sigaction(SIGALRM, &act, NULL);

/* turn on alarm clock */
alarm(nsecs);

/* pause until signal... */
pause();

/* if signal was SIGALRM, print message */
if (alarm_flag == TRUE)
{
printf(BELLS);
for(j = 2; j < argc; j++)
printf("%s ",argv[j]);
printf("\n");
}
exit(0);
}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Discuss a parent process or in the child process
Reference No:- TGS01936143

Now Priced at $25 (50% Discount)

Recommended (91%)

Rated (4.3/5)