How to make parent and child processes send signals to eachother rather than sending signals to themselves?

75 Views Asked by At

I am making a program in which a parent and child function send signals to one another. Every time a sigtstp signal is triggered, it calls a signal handler to roll for a random amount of damage (between 0-50) that will be done on the other process. However, I am not sure whether or not my processes are sending their signals to eachother, and if they are im not sure if the damage being calculated is the result of the signal or not.

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

volatile sig_atomic_t damage;
time_t t;
// Referenced link to get a good random number seed
// https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand
unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
{
    a=a-b;  a=a-c;  a=a^(c >> 13);
    b=b-c;  b=b-a;  b=b^(a << 8);
    c=c-a;  c=c-b;  c=c^(b >> 13);
    a=a-b;  a=a-c;  a=a^(c >> 12);
    b=b-c;  b=b-a;  b=b^(a << 16);
    c=c-a;  c=c-b;  c=c^(b >> 5);
    a=a-b;  a=a-c;  a=a^(c >> 3);
    b=b-c;  b=b-a;  b=b^(a << 10);
    c=c-a;  c=c-b;  c=c^(b >> 15);
    return c;
}
void signal_handler(int signum){
    srand(mix(clock(),time(NULL),getpid()));
    damage = (rand() % 50);
}
int main() {
    int signum = SIGTSTP;
    int pid;
    signal(signum, signal_handler);
    printf("--------------------------------------------------------\n\n--------------------------------------------------------\n");
    if((pid = fork()) < 0){
        printf("pid is less than 0, exiting \n");
        exit(1);
    }
    int status;
    pid_t id = getpid();
    int total_dmg = 0;
    //Child Case
    if(pid == 0) {
        srand((unsigned)time(&t)*3);
        for (int i = 0; i < 10; i++) {
            if (total_dmg >= 200){
                printf("Child %d has died \n", id);
                kill(id,9);
                return 0;

            }
            else{
                kill(pid, signum);
                total_dmg = total_dmg + damage;
                printf("Child %d has been hit for %d, total damage is now %d\n",id,damage,total_dmg);
                sleep((rand() %3));
            }
            if(total_dmg < 200 && i == 9) {
                printf("Child %d has survived!\n", id);
            }

        }
    }
    //Parent Case
    if(pid > 0) {
        srand((unsigned)time(&t)*7);
        for (int i = 0; i < 10; i++) {
            if (total_dmg >= 200){
                printf("Parent %d has died \n", id);
                waitpid(id,&status,0);
                return 0;
            }
            else{
                kill(pid, signum);
                total_dmg = total_dmg + damage;
                printf("Parent %d has been hit for %d, total damage is now %d\n",id,damage,total_dmg);
                sleep((rand() %3));
            }
            if(total_dmg < 200 && i == 9) {
                printf("Parent %d has survived!\n", id);
        }
        }
    }
    return 0;
}

I originally had the kill signals pid argument as the pid of the process itself instead of the other process.

0

There are 0 best solutions below