gpt4 book ai didi

c - 如何使用 P 进程确定一个值是否存在于数组中

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:27 28 4
gpt4 key购买 nike

如何使用 P 进程更快地确定一个值是否存在于数组中?到目前为止,这是我尝试过的。我将数组分成几部分,这样每个子进程都会检查一 block (划分和工作)。找到该值后,子进程将写入管道。父进程将读取管道,如果有读取到的内容,则会显示一条消息:已找到该值。我想了解的是:如何向其他子进程发出已找到该值并停止搜索的信号。

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#define P 10
#define SIZE (sizeof(elementsList)/sizeof(elementsList[0]))

static pid_t pid[P];
static int elementsList[] = { }; //some values

void findValue(int elemList[], int start, int step, int size, int value,
int wPipe)
{
int i;
for (i = start; i < size; i += step)
{
if (value == elemList[i])
{
write(wPipe, &elemList[i], sizeof elemList[i]);
}
}
}

int main()
{

int fd[2];
int result;
int nbytes;
int child;
int valueToFind;
int wPid;

printf("Enter the value to be found: \n");
scanf("%d", &valueToFind);

if (pipe(fd) < 0)
{
perror("pipe");
}

for (child = 0; child < P; child++)
{
if ((pid[child] = fork()) < 0)
{
perror("fork");
exit(1);
}
else if (pid[child] == 0)
{
close(fd[0]);
printf("Child #%d\n", getpid());
findValue(elementsList, child, P, SIZE, valueToFind, fd[1]);
close(fd[1]);
exit(0);
}
}

close(fd[1]);
int status = 0;

nbytes = read(fd[0], &result, sizeof result);
printf("Parent reads %d bytes\n", nbytes);
if (nbytes > 0)
{
printf("The value %d was found\n", result);
}
else
{
printf("The value wasn't found.\n");
}

wPid = wait(&status);
if (WIFEXITED(status))
{
int returnCode = WEXITSTATUS(status);
if (returnCode == 0)
{
printf("Child %d exit status is 0\n", wPid);
}

for (child = 0; child < P; child++)
{
kill(pid[child], SIGTERM);
}
}

return 0;
}

P.S 我必须使用 fork() 来实现它

最佳答案

I am new into this and so far i only can use pipes. My question is if it's ok to wait until the first of the processes exits and after that calling the read or first make the read and only if is there something written terminate the other processes because the value was found. It's ok to use kill? I have edited the code with the second approach.

kill 到目前为止没问题。这听起来不可避免,但实际上有不同的信号,有些可以被捕获,有些则不能(SIGKILL,SIGSTOP)。

可能的解决方案

  • SIGTERM 安装一个信号处理程序来进行任何清理(在现代系统上分配可能只是被丢弃 -> 更快)并通过向它发送该信号来终止相同的进程。
  • 但是,终端中的 CTRL+C 也会发送 SIGTERM。您可以代替或另外使用 SIGUSR1SIGUSR2 来实现特定应用目的。
  • 要退出该进程,不应从信号处理程序中调用 exit()。有关详细信息,请参阅此讨论:Can exit() fail to terminate process?
  • 要获得正确的退出状态,应该恢复捕获信号的默认信号处理程序,然后再次引发该信号,如下例所示。

例子

void
fatal_error_signal (int sig)
{
/* Since this handler is established for more than one kind of signal,
it might still get invoked recursively by delivery of some other kind
of signal. Use a static variable to keep track of that. */
if (fatal_error_in_progress)
raise (sig);
fatal_error_in_progress = 1;

/* Now do the clean up actions:
- reset terminal modes
- kill child processes
- remove lock files */
...

/* Now reraise the signal. We reactivate the signal's
default handling, which is to terminate the process.
We could just call exit or abort,
but reraising the signal sets the return status
from the process correctly. */
signal (sig, SIG_DFL);
raise (sig);
}

来源:http://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_24.html#SEC488

关于c - 如何使用 P 进程确定一个值是否存在于数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018069/

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