user3033194
user3033194

Reputation: 1821

Unable to kill processes running concurrently

I am running a program A.c concurrently, say 5 times. A part of the code is given below:

int main(int argc, char *argv[]){

    char s = 0;
    int i = 0;
    pid_t procB_id = 0;
    int retval = 0;

    struct sigaction act;
    ch = &c[0];
    memset(c, 0, 50);

    // open the file entered in the command-line for reading 
    fptr = fopen(argv[1], "r");

    if(fptr == NULL){
            printf("Error - input file could not be opened for reading!\n");
        exit(EXIT_FAILURE);
        }

    // Write characters read by file pointer to an array
    while((s=fgetc(fptr)) != EOF){
        ch[i] = s;
        i++;
    }
    printf("Length of text: %d\n",i);

    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;
    act.sa_sigaction = handlerA;

    if((sigaction(SIGRTMIN, &act, NULL)) != 0){
        printf("Signal could not be registered!\n");    
    }

    //get PID of daemon B to be able to send it a real-time signal, indicating that A has started
    procB_id = getBprocessID();

    printf("PROCESS ID OF B: %d\n", (int) procB_id);

    //call sendSignal() method to send real-time signal to B
    retval = sendBSignal(procB_id);

    if(retval == 1){

        while(n < 0){
            //printf("BEFORE PAUSE\n");
            pause();
        }
        //writeToFIFO(n);
        if(writeToFIFO(n) == 1){
            n = -1;
            exit(EXIT_SUCCESS);
        }
    }


    while (1);
}

The relevant part of the code is really exit(EXIT_SUCCESS). However, when I am running the process A in parallel, only 1 process is exiting, not the rest. The others are still running. I am running the process in parallel by the following shell script:

for ((i=1;i<=5;i++))

do

    ./A file.txt &
done

"file.txt" is a file each process has to read separately. I want to kill all 5 processes, not just one. Does anyone know how I can do that? Please help. I guess my code is not correct, but I don't know what to do here.

Upvotes: 0

Views: 371

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136228

I want to kill all 5 processes, not just one. Does anyone know how I can do that?

pkill -f "A file.txt"

You probably lost a do loop for your infinite while(1):

do {
    procB_id = getBprocessID();

    printf("PROCESS ID OF B: %d\n", (int) procB_id);

    //call sendSignal() method to send real-time signal to B
    retval = sendBSignal(procB_id);

    if(retval == 1){
        while(n < 0){
            //printf("BEFORE PAUSE\n");
            pause();
        }
        //writeToFIFO(n);
        if(writeToFIFO(n) == 1){
            n = -1;
            exit(EXIT_SUCCESS);
        }
    }
} while (1);

Upvotes: 2

Related Questions