Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/resource.h>
- #include <sys/time.h>
- #include <signal.h>
- void sigHandler(int);
- void user1(int);
- void user2(int);
- pid_t pid, ppid;
- int main(int argc, char *argv[])
- {
- signal(2, sigHandler);
- signal(SIGUSR1, user1);
- signal(SIGUSR2, user2);
- if((pid = fork()) < 0)
- {
- perror("fork failed");
- exit(1);
- }else if(!pid)
- {
- printf("spawned child PID# %d\n", getpid());
- while(1)
- {
- int n;
- n = rand() % 2;
- if(n == 1)
- kill(getpid(), SIGUSR1);
- else
- kill(getpid(), SIGUSR2);
- sleep(2);
- }
- }else{
- printf("waiting...\n");
- pause();
- }
- return 0;
- }
- void sigHandler(int sigNum)
- {
- sleep(1);
- printf("outta here.\n");
- exit(0);
- }
- void user1(int sigNum)
- {
- printf("received a SIGUSR1 interrupt from process %d\n", getpid());
- }
- void user2(int sigNum)
- {
- printf("received a SIGUSR2 interrupt from process %d\n", getpid());
- }
Add Comment
Please, Sign In to add comment