gpt4 book ai didi

c - 无法终止并发运行的进程

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:44 25 4
gpt4 key购买 nike

我正在同时运行程序 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(EXIT_SUCCESS)。但是,当我并行运行进程 A 时,只有 1 个进程正在退出,而不是其余的。其他人还在跑。我正在通过以下 shell 脚本并行运行该过程:

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

do

./A file.txt &
done

“file.txt”是每个进程必须单独读取的文件。我想杀死所有 5 个进程,而不仅仅是一个。有谁知道我该怎么做?请帮忙。我想我的代码不正确,但我不知道该怎么做。

最佳答案

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

pkill -f "A file.txt"

您可能丢失了无限 while(1)do 循环:

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

关于c - 无法终止并发运行的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27172163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com