无法杀死并发运行的进程

时间:2014-11-27 13:45:37

标签: c concurrency

我同时运行一个程序A.c,说5次。代码的一部分如下:

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);
}

代码的相关部分实际上是退出(EXIT_SUCCESS)。但是,当我并行运行进程A时,只有一个进程退出,而不是其余进程。其他人仍在运行。 我通过以下shell脚本并行运行该进程:

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

do

    ./A file.txt &
done

&#34; file.txt的&#34;是每个进程必须单独读取的文件。我想要杀死所有5个进程,而不只是一个进程。有谁知道我怎么做到这一点?请帮忙。我想我的代码不正确,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

  

我想要杀死所有5个进程,而不只是一个进程。有谁知道我怎么能这样做?

pkill -f "A file.txt"

您的无限do可能丢失了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);