English 中文(简体)
如何向C进程发出信号?
原标题:How to send a signal to a process in C?
  • 时间:2011-10-08 12:51:14
  •  标签:
  • c
  • signals

我需要向一个进程发出信号,而且当进程收到这一信号时,某些事情是怎样的,在C中如何取得最佳成果?

问题回答

向一个过程发出信号的途径是lu(pid, sign);。 然而,你们应当认识到,信号不是除家长与子女之间因固有的种族条件而发出的信息之外的一种强有力的进程间通信手段。 管道、档案、目录、名称为ema光、袖珍、共享记忆等,都为处理过程之间的通信提供了高度的优待办法。

If you happen to be on one of the Unix variants, the following man pages will help:

man 2 kill
man 2 signal
man 2 sigvec

kill + fork runnable POSIX example

Time for some fun:

#define _XOPEN_SOURCE 700
#include <assert.h>
#include <signal.h>
#include <stdbool.h> /* false */
#include <stdio.h> /* perror */
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE */
#include <sys/wait.h> /* wait, sleep */
#include <unistd.h> /* fork, write */

void signal_handler(int sig) {
    char s1[] = "SIGUSR1
";
    char s2[] = "SIGUSR2
";
    if (sig == SIGUSR1) {
        write(STDOUT_FILENO, s1, sizeof(s1));
    } else if (sig == SIGUSR2) {
        write(STDOUT_FILENO, s2, sizeof(s2));
    }
    signal(sig, signal_handler);
}

int main() {
    pid_t pid;

    signal(SIGUSR1, signal_handler);
    signal(SIGUSR2, signal_handler);
    pid = fork();
    if (pid == -1) {
        perror("fork");
        assert(false);
    } else {
        if (pid == 0) {
            while (1);
            exit(EXIT_SUCCESS);
        }
        while (1) {
            kill(pid, SIGUSR1);
            sleep(1);
            kill(pid, SIGUSR2);
            sleep(1);
        }
    }
    return EXIT_SUCCESS;
}

1. 汇编和操作:

gcc -std=c99 signal_fork.c
./a.out

结果:

SIGUSR1
SIGUSR2
SIGUSR1
SIGUSR2
....

But beware that there are many complexities when dealing with signals:

Tested in Ubuntu 17.10, GitHub upstream.





相关问题
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 ...

热门标签