signal notes

1. code example

#include <signal.h>

static void my_signal_handler(int sig)
{
    printf("receive signal %d", sig);
}

int main(/*int argc, char **argv*/) {
    struct sigaction sa;
   /* Set up the signal handler */
    sa.sa_handler = my_signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_ONESHOT;
   /* 攔截Ctl+C 及 kill */
    if (sigaction(SIGINT , &sa, NULL) == -1) {
        return 1;
    }
    if (sigaction(SIGTERM , &sa, NULL) == -1) {
        return 1;
    }
}


2 常見 signal id

#define SIGINT 2 /* Interrupt (ANSI). */ 按 Ctl + C
#define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */  不可用
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */ kill <process id>
#define SIGSTKFLT 16 /* Stack fault. */
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
#define SIGCHLD 17 /* Child status has changed (POSIX). */
#define SIGCONT 18 /* Continue (POSIX). */
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */ 不可用

3. sa.sa_flags

 
 
SA_NOCLDSTOP
If signumis SIGCHLD, do not receive notification when child processes stop (i.e., when childprocesses receive one of SIGSTOP, SIGTSTP, SIGTTINor SIGTTOU).
SA_ONESHOT or SA_RESETHAND
Restore the signal action to the default state once the signal handlerhas been called.
SA_ONSTACK
Call the signal handler on an alternate signal stack provided by sigaltstack(2).If an alternate stack is not available, the default stack will be used.
SA_RESTART
Provide behaviour compatible with BSD signal semantics by making certainsystem calls restartable across signals.
SA_NOMASK or SA_NODEFER
Do not prevent the signal from being received from within its own signalhandler.
SA_SIGINFO
The signal handler takes 3 arguments, not one. In this case, sa_sigactionshould be set instead of sa_handler.(The sa_sigaction field was added in Linux 2.1.86.)

4. 若是攔截 Ctrl+c 或kill, process 將無法被外部結束. 記得要自己叫exit(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,使用了Python和TensorFlow: ``` # 导入必要的库 import tensorflow as tf import numpy as np # 加载LSTM模型 lstm_model = tf.keras.models.load_model('lstm_model.h5') # 加载GAN模型 gan_model = tf.keras.models.load_model('gan_model.h5') # 定义生成音乐的函数 def generate_music(): # 生成旋律序列 melody_input = np.random.rand(1, 16, 128) # 16个时间步,每个时间步128个音符 melody_sequence = lstm_model.predict(melody_input)[0] # 生成和弦序列 chord_input = np.random.rand(1, 64) # 64个时间步 chord_sequence = gan_model.predict(chord_input)[0] # 将旋律序列和和弦序列结合起来 music_sequence = [] for i in range(16): melody_notes = melody_sequence[i] chord_notes = chord_sequence[i*4:(i+1)*4] music_notes = np.concatenate((melody_notes, chord_notes), axis=1) music_sequence.append(music_notes) music_sequence = np.array(music_sequence) # 将音符序列转换为音频信号 music_signal = convert_to_audio(music_sequence) return music_signal # 调用生成音乐函数,生成一段音乐 music_signal = generate_music() # 后期处理,如保存音乐文件、调整音量、添加音效等 save_music_file(music_signal, 'generated_music.wav') adjust_volume(music_signal, 0.8) add_effects(music_signal, 'reverb') ``` 请注意,这只是一个简单的示例代码,需要根据具体情况进行修改和优化,以生成高质量的音乐作品。同时,还需要编写一些辅助函数,如将音符序列转换为音频信号、保存音乐文件、调整音量、添加音效等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值