在C代码中检测SIGUSR1并打印Hello World

0sgqnhkj  于 2023-04-29  发布在  其他
关注(0)|答案(1)|浏览(95)

我有一个相对不重要的问题,关于可重复的代码(您可以直接从您的环境运行它
本质上
我有一些C和Python代码。
我的python代码
创建两个管道tomcat_1和tomcat_2,这两个管道可以分别读写。
现在我使用我的tomcat_2管道编写字符串F-14 TOMCAT,在我的C代码中,我打开两个管道,并从tomcat_2管道读取已经编写的内容
这东西没有任何戏剧性的工作。

import os
import signal

#Remove existing fifos if not cleaned up from last time.
try:
    os.unlink('/tmp/tomcat_1')
    os.unlink('/tmp/tomcat_2')
except FileNotFoundError:
    pass

signal.signal(signal.SIGINT, lambda x,y: None) #Setup signal handler with no-op lambda expression

os.mkfifo('/tmp/tomcat_1') #Fifos can be constructed in python
os.mkfifo('/tmp/tomcat_2')


r = open('/tmp/tomcat_1','r')
w = open('/tmp/tomcat_2','w')

w.write("F-14 TOMCAT")
w.flush()
w.close()
with open('/tmp/tomcat_2', 'r') as f:
    pid = int(f.read())

os.kill(pid, signal.SIGUSR1)
while True:
    data = r.read()
    if len(data) > 0:
        print('PYTHON: Received ' + data) # FIFOs can be read from like any other file

# Close reading pipe
r.close()


signal.pause() #mimics pause(2)

print('PYTHON: ending process')

r.close()

#cleanup fifos
os.unlink('/tmp/tomcat_1')
os.unlink('/tmp/tomcat_2')

现在
我有C代码,可以读取写入管道的数据

void signal_handler(int SIG);

volatile sig_atomic_t g_sigint_flag = 0;
volatile sig_atomic_t g_sigusr1_flag = 0;
int fd1;
int fd2;

void exchange_signal_handler(int signo);

int main(int argc, char ** argv) {

    char trader[100];

    fd2 = open("/tmp/tomcat_1",O_WRONLY);
    fd1 = open("/tmp/tomcat_2", O_RDONLY);

    if (fd1== -1) {
        perror("open pipe failed");
        return 1;
    }

    if (fd2== -1) {
        perror("open pipe failed");
        return 1;
    }

    if (signal(SIGINT, signal_handler) == SIG_ERR) {
        printf("Unable to register signal handler for SIGINT\n");
        return 1;
    }

    if (signal(SIGUSR1, exchange_signal_handler) == SIG_ERR) {
        printf("Unable to register exchange signal handler for SIGUSR1\n");
        return 1;
    }

    char buf[1024];
    int buf_size = read(fd1, buf, sizeof(buf));
    buf[buf_size] ='\0';
    printf("Received data: %s\n", buf);

    while (!g_sigint_flag) {
        if (g_sigusr1_flag) {
            g_sigusr1_flag = 0; // Reset the flag
        }
    }

    close(fd2);
    close(fd1);
    return 0;
}

void signal_handler(int SIG) {
    if (SIG == SIGINT) {
        printf("Terminated\n");
        g_sigint_flag = 1;
    }
}

void exchange_signal_handler(int signo) {
    if (signo == SIGUSR1) {
        printf("Received SIGUSR1 signal from exchange\n");
        g_sigusr1_flag = 1;
    }
}

现在我的问题是,当我的Python代码向tomcat_2管道发送SIGUSR 1信号时,在Python中,我的C代码应该检测到这一点并打印一个hello world
我该怎么做呢?

j5fpnvbx

j5fpnvbx1#

代码至少有这个问题。不知道这是否是OP问题的来源。

风险:写入缓冲区外

当读取1024字节时,代码写入缓冲区外。

char buf[1024];
// int buf_size = read(fd1, buf, sizeof(buf));
int buf_size = read(fd1, buf, sizeof(buf) - 1);
buf[buf_size] ='\0';
printf("Received data: %s\n", buf);

char buf[1024];
int buf_size = read(fd1, buf, sizeof(buf));
// printf("Received data: %s\n", buf);
printf("Received data: %.*s\n", buf_size, buf);

相关问题