English 中文(简体)
Ctrl-C终止父母和子女程序
原标题:terminating parent and child process on Ctrl-C

我正在撰写一份C方案,其中家长负责n儿童过程。 一项儿童程序一旦建立,就可援引SIGSTOP,以便建立其他儿童进程。 父母在创建所有“n>>儿童程序之后,向所有儿童发出一个SIGCONT信号。

所有儿童进程都实施一只fin,并分享使用ema光的共有资源。 现在我想,每当用户压力ctrl-c时,父母和所有儿童过程就共同终止。 然而,在终止儿童程序之前,应在一份档案中更新每份如何使用资源。

指称:

Process 1 - 5 times
Process 2 - 3 times

等等。

在实施过程中需要帮助?

最佳回答

正式的信号传承器功能应尽可能少。 C标准说,它可以写到<代码>volaective sig_atomic_tps,或打电话abort()_Exit((或,有限制,signal()。 POSIX允许更多人到场,而且你可能重新在含水层上工作(尽管你没有这样说)。 因此,你的信号手将改变sig_atomic_t的价值。 从0到1不等,表明信号发生。

因此,你们的儿童进程将充满活力。 作为居留条件的一部分,应当检查sig_atomic_t变量,看看儿童是否应当终止。 在发现发出信号时,它将停止登机,打开对口号,将其信息写到该档案中,并撤出。 您可查sig_atomic_t。 处理中其他各点的变量不单单是主要条件;这符合你的决定。

请注意,您应使用<代码>sigaction(>,而不是signal(>)控制信号处理,在处理信号时,你应阻断断。

因此,总结如下:

  • Your signal handler does as little as possible.
  • Your code detects in the main loop when the signal handler has been called and arranges to exit.
  • Your code can also detect when the signal handler has been called at other convenient points.
  • You can call a function to do the logging and exit.
问题回答

为了书写档案,你需要添加该代码,以在您的SIGINT信号手稿中写上上述档案。 如果你想把每个过程写到该档案中,那么你就会再次需要确保SIGINT被送到整个处理小组。

也许可以通过不向每一个国家发送SIGQUIT来获得,因为每个进程在经过SIGINT处理后就可以撤出。 如果你想优化一点,你就可以保持一个共享的数据结构,其中程序已经收到最高审计机关,以便你不向每个进程派出几个最高审计机关。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...