我想在c中执行一个python脚本来获取一个自定义的可执行文件名,但我的所有尝试都失败了,因为它总是涉及一些子进程,我没有pid来联系'它...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void SIGINT_handler(int);
void SIGTERM_handler(int);
void SIGUSR1_handler(int);
int ShmID;
pid_t *ShmPTR;
pid_t c_pid; // should be the script pid
int main(void) {
pid_t p_pid = getpid();
key_t MyKey;
if (signal(SIGINT, SIGINT_handler) == SIG_ERR) {
printf("SIGINT install error\n");
exit(1);
}
if (signal(SIGTERM, SIGTERM_handler) == SIG_ERR) {
printf("SIGTERM install error\n");
exit(2);
}
if (signal(SIGUSR1, SIGUSR1_handler) == SIG_ERR) {
printf("SIGUSR1 install error\n");
exit(3);
}
MyKey = ftok(".", 's');
ShmID = shmget(MyKey, sizeof(pid_t), IPC_CREAT | 0666);
ShmPTR = (pid_t *) shmat(ShmID, NULL, 0);
*ShmPTR = p_pid;
//---This is where I'm stuck---
}
void SIGINT_handler(int sig) {
signal(sig, SIG_IGN);
kill(c_pid, sig);//send interrupt signal to pyton script
printf("From SIGINT: just got a %d (SIGINT ^C) signal\n", sig);
signal(sig, SIGINT_handler);
}
void SIGTERM_handler(int sig) {
signal(sig, SIG_IGN);
kill(c_pid, sig);//send terminate signal to python script
printf("From SIGTERM: just got a %d (SIGTERM ^\\) signal"
" and is about to quit\n", sig);
shmdt(ShmPTR);
shmctl(ShmID, IPC_RMID, NULL);
exit(3);
}
void SIGUSR1_handler(int sig) {
signal(sig, SIG_IGN);
kill(c_pid, sig);//send user signal 1 to python script
printf("From SIGUSR1: just got a %d (SIGUSR1 ^C) signal\n", sig);
signal(sig, SIGUSR1_handler);
}
免责声明:我更多是C的初学者
我该怎么做呢?