0% found this document useful (0 votes)
47 views

Assignment 3

The document discusses using signals and signal handlers in C programs, providing examples of programs that use signals to communicate between parent and child processes, as well as a program that installs a signal handler for SIGINT to ensure a temporary file is cleaned up if control-C is pressed before the regular program completion. The examples demonstrate how to set signal handlers for different signals, send signals between processes using kill(), and have signal handlers call functions to handle signal receipt and cleanup resources.

Uploaded by

rmt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Assignment 3

The document discusses using signals and signal handlers in C programs, providing examples of programs that use signals to communicate between parent and child processes, as well as a program that installs a signal handler for SIGINT to ensure a temporary file is cleaned up if control-C is pressed before the regular program completion. The examples demonstrate how to set signal handlers for different signals, send signals between processes using kill(), and have signal handlers call functions to handle signal receipt and cleanup resources.

Uploaded by

rmt
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment-3

1: #include<stdio.h>
#include <signal.h>

voidsigproc(void);

voidquitproc(void);

main()

{ signal(SIGINT, sigproc);

signal(SIGQUIT, quitproc);

printf("ctrl-c disabled use ctrl-\\ to quit\n");

for(;;); /* infinite loop */}

voidsigproc()

{ signal(SIGINT, sigproc); /* */

/* NOTE some versions of UNIX will reset signal to default

after each call. So for portability reset signal each time */

printf("you have pressed ctrl-c \n");

void quitproc()

{printf(“ctrl pressed to quit”);

exit(0);

}
2:Write a program that communicates between child and parent processes
using kill () and signal ().

#include<stdio.h>

#include<sys/types.h>

#include<signal.h>

signal(SIGQUIT, sigquit);

for(;;); /* loop for ever */

else /* parent */

{ /* pid hold id of child */

printf("\nPARENT: sending SIGHUP\n\n");

kill(pid,SIGHUP); #include <stdio.h>

#include <signal.h>

voidsighup(); /* routines child will call upon sigtrap */

voidsigint();

voidsigquit();

main()

{ intpid;

/* get child process */

if ((pid = fork()) < 0) {

perror("fork");
exit(1);

if (pid == 0)

{ /* child */

signal(SIGHUP,sighup); /* set function calls */

signal(SIGINT,sigint);

sleep(3); /* pause for 3 secs */

printf("\nPARENT: sending SIGINT\n\n");

kill(pid,SIGINT);

sleep(3); /* pause for 3 secs */

printf("\nPARENT: sending SIGQUIT\n\n");

kill(pid,SIGQUIT);

sleep(3);

voidsighup()

{ signal(SIGHUP,sighup); /* reset signal */

printf("CHILD: I have received a SIGHUP\n");

voidsigint()

{ signal(SIGINT,sigint); /* reset signal */


printf("CHILD: I have received a SIGINT\n");

voidsigquit()

{ printf("My DADDY has Killed me!!!\n");

exit(0);

3:What if user types Ctrl-c?

 OS sends a 2/SIGINT signal to the process


 Default handler of 2/SIGINT exits the process
Problem: The temporary file is not deleted.
 Process dies before remove("tmp.txt") is executed.
Challenge: Ctrl-c could happen at any time
 Which line of code will be interrupted???
Solution: Install a signal handler
 Define a “clean up” function to delete the file
 Install the function as a signal handler for 2/SIGINT
Now write a program according to the given solution to solve the
problem.

Ans:
static FILE *psFile; /* Must be global. */

static void cleanup(intiSig) {

fclose(psFile);

remove("tmp.txt");

exit(EXIT_FAILURE);

}
int main(void) {

void (*pfRet)(int);

psFile = fopen("temp.txt", "w");

You might also like