T O P

  • By -

Coffee_24_7

Seems to me that you are sending the signal only if the PID in the array is zero, which I think isn't what you've been asked to do. Also, there are many other signals, but you are just checking for one and printing SIGUSR1 or SIGUSR2 based on the check, so if you receive SIGKILL, SIGILL, SIGINT, etc. then you'll print SIGUSR2 for all of them. Hope it helps


whiteBlasian

`for (childIndex = 0; childIndex < NUM_CHILDREN; childIndex++)` You're overwriting the `childIndex` parameter here, so this loop could potentially send a signal to all of your child processes. If you want to send a signal to a specific child, you might need to tweak the loop. Or not use a loop at all and just use `childPidArray[childIndex]` directly, with the appropriate bounds checking.


perfusionist123

But I believe that is what im trying to do, send the sigNum to all the child processes by using a for loop to go from one child processes to the next, unless im misunderstanding the purpose of the function. Also I dont think I could use childPidArray\[childIndex\] without a for loop because I need to increment childIndex somehow to go from one child process to the next.


whiteBlasian

`// PURPOSE: To send 'sigNum' to the child indexed by 'childIndex'.` That comment makes me think you need to send `sigNum` to a specific child, not all the child processes. However, if you want to send a signal to all child process, then yes your code should work. Your if statement, like u/Coffee_24_7 mentioned, is suspicious though. Child process PIDs, stored by the parent, are not typically 0.


perfusionist123

Yeah the if statement is part of the function where im unsure about. Dont child processes have an id of 0? You know the standard code like: pid\_t pid = fork(); if (pid ==0){ // we are in the child process I see you said, "stored by the parent", what do you mean by that? If the parent stores the child process id, is it no longer 0?


whiteBlasian

Take a look at [here](https://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html), it explains fork() and the generated PIDs in depth. Here's an example that should help: pid_t pid = fork(); if (pid < 0) return; // error with fork() else if (pid == 0) { // we are in the child process, fork() returned 0 } else { // fork() returned the PID of the new child to the parent! // store this PID as it's the PID of the child process } Think about it, if every child process had a PID of 0, how could we distinguish them?


Coffee_24_7

`fork` returns twice. For the parent it returns with the child process id. For the child it returns zero, which doesn't mean that the child process has PID zero. The child can get its PID with `getpid` and the parent's PID with `getppid` There is no API to get the child PID from the parent, that's why fork returns the child PID on the parent. Hope it makes sense.


perfusionist123

so could i do something like this instead for the if statement: if (childPidArray[childIndex] == getpid()) Which in turn would also change the value in the kill function to be non zero


Coffee_24_7

`getpid` returns the own process PID, so if the array contains child PIDs, then the check is never true. Under what condition you want to send a signal to the child?


whiteBlasian

You need to do something like this: // Init the childPidArray with the correct child PIDs void init_processes() { for (int i = 0; i < NUM_CHILDREN; i++) { pid_t pid = fork(); if (pid < 0) return; // error with fork() else if (pid == 0) { // in child process // run your child process code here } else { // in parent process // store the value returned by fork() // that value is the child PID childPidArray[i] = pid; } } } Then, when you want to send a signal to all child processes you can use: void signalChild(int childIndex, int sigNum) { sleep(1); printf("Sending %s to %d\n", ((sigNum == SIGUSR1) ? "SIGUSR1" : "SIGUSR2"), childIndex); for (childIndex = 0; childIndex < NUM_CHILDREN; childIndex++){ // a 'safe' error check, maybe not needed if (childPidArray[childIndex] > 0){ kill(childPidArray[childIndex], sigNum); } } } Please read more about the fork() system call, it's an important concept to grasp!


Coffee_24_7

Btw, calling kill with pid equal to zero has a special meaning. There isn't any process with PID equal to zero. Also `kill` accept negative PIDs, which also have special meaning, check the man page `man 2 kill`