C 无法终止分叉进程
我有一个进程必须使用Ctrl-C或SIGINT终止。在这种情况下,我无法在键盘上实际使用Ctrl-C,因此必须使用C中的kill命令。 这段代码基本上读取GPIO输入,并启动和停止一个script record.sh(GStreamer命令行脚本) 我只想终止startVideoRecording中创建的子进程。我需要做开始,停止,开始,停止录音,只要我喜欢。这不是一次性的 但是,当我更改gpio值并触发stopAllRecordings时,它不会将SIGINT发送到子进程。它只是挂在向GStreamer进程发送中断时。。状态0 如何确保视频录制过程以Ctrl-C等效项终止 谢谢 代码如下:C 无法终止分叉进程,c,linux,gpio,C,Linux,Gpio,我有一个进程必须使用Ctrl-C或SIGINT终止。在这种情况下,我无法在键盘上实际使用Ctrl-C,因此必须使用C中的kill命令。 这段代码基本上读取GPIO输入,并启动和停止一个script record.sh(GStreamer命令行脚本) 我只想终止startVideoRecording中创建的子进程。我需要做开始,停止,开始,停止录音,只要我喜欢。这不是一次性的 但是,当我更改gpio值并触发stopAllRecordings时,它不会将SIGINT发送到子进程。它只是挂在向GStr
int main() {
pollGPIOSwitch(&vn200);
}
void* pollGPIOSwitch(void* arg) {
Vn200* vn200 = (Vn200*)arg;
int fd;
char buf[100];
char value;
int videoRecError;
bool videoRecOn = false;
sprintf(buf, "/sys/class/gpio/gpio56/value");
while (KEEP_GOING) {
fd = open(buf,O_RDONLY);
lseek(fd,0,SEEK_SET); // move to beginning of file
read(fd,&value,1);
if (value=='0') {
if (videoRecOn) { // recording on, switch off, end recording
stopAllRecordings();
videoRecOn = false;
TriggerGPSINSThreadExit = 0; // reset variables
printf("Reset GPSINS Thread variables.\n");
}
}
else if (!videoRecOn) { // recording off, switch on, start recording
if (pthread_create(&GPSINSLoggingThread, NULL, runGPSINS,(void*) vn200) != 0) {
printf("Error: Fail to create runGPSINS thread\n");
}
videoRecError = startVideoRecording();
if (videoRecError == -1)
pthread_exit(&videoRecError);
videoRecOn = true;
}
usleep(500000);
}
close(fd);
printf("Exited Polling!");
}
int startVideoRecording() {
int error;
error = 0;
if ((AVRecordingProcess = fork()) != -1) {
switch (AVRecordingProcess) {
case -1:
printf("Error: Fail to create AVRecordingProcess\n");
error = -1;
case 0:
execl("record.sh", "0", NULL);
//system("//root//record.sh");
printf("Error: Fail to execute AVRecordingProcess\n");
error = -1;
default:
printf("Video Recording...process id %d\n", AVRecordingProcess);
break;
}
} else {
printf("Error: MainHost failed to fork AVRecordingProcess\n");
error = -1;
}
return error;
}
void stopAllRecordings() {
int ret;
FILE *infile, *outfile;
int ch;
printf("Terminating child processes..%d\n", AVRecordingProcess);
ret = kill(AVRecordingProcess, SIGINT);
printf("Sending interrupt to GStreamer process.. Status %d\n", ret);
int status;
printf("Wait for the GStreamer process %d to end....", AVRecordingProcess);
wait(AVRecordingProcess);
printf("GStreamer ended.\n");
}
是否可能您正在启动多个子进程,并且只将SIGINT发送到您创建的最后一个子进程,而不是更早的子进程?启动此程序后,键入“PS”并按enter键,查看您已启动了多少进程。我最终执行了以下操作:
int startVideoRecording() {
int error;
error = 0;
if ((AVRecordingProcess = fork()) != -1) {
switch (AVRecordingProcess) {
case -1:
printf("Error: Fail to create AVRecordingProcess\n");
error = -1;
case 0:
setpgid(AVRecordingProcess,0); <-- NEW
execl("record.sh", "0", NULL);
//system("//root//record.sh");
printf("Error: Fail to execute AVRecordingProcess\n");
error = -1;
default:
printf("Video Recording...process id %d\n", AVRecordingProcess);
break;
}
} else {
printf("Error: MainHost failed to fork AVRecordingProcess\n");
error = -1;
}
return error;
}
void stopAllRecordings() {
int ret;
FILE *infile, *outfile;
int ch;
printf("Terminating child processes..%d\n", AVRecordingProcess);
ret = killpg(AVRecordingProcess, SIGINT); <-- NEW
printf("Sending interrupt to GStreamer process.. Status %d\n", ret);
int status;
printf("Wait for the GStreamer process %d to end....", AVRecordingProcess);
waitpid(AVRecordingProcess,0,0); <-- NEW
printf("GStreamer ended.\n");
}
它是有效的
基本上,在创建AVRecordingProcess的过程中,我将其设置为流程组的负责人。
发送进程组SIGINT也会将其发送到组中的其余进程。
为了确保进程组结束,我使用了waitpid
Linux专家,请随时纠正我,谢谢 嗨,尼克·威尔克森,我开始录音后看到了这个。28782-HRS my main program 28921-record.sh my script 28922-gst-launch-0.10 gstreamer命令行。在我的情况下,如何向gst-launch-0.10发送SIGINT?我猜可能它没有得到信号?你使用kill的方式是正确的。虽然您的子进程不太可能忽略SIGINT,但这是可能的。您可以尝试SIGKILL。请尝试将所有子进程的进程组id设置为相同的进程组id,然后将sigint或SIGKILL发送到该进程组id,而不是单个进程id。每次录制关闭时,您都会创建一个新线程,需要启动它。您的线程函数是runGPSINS。什么是函数定义?在创建线程之后,您又开始使用startVideoRecording函数了吗?如果子进程开始录制,线程是用来做什么的?很抱歉,请忽略这一点,我在其他函数中终止线程。@Ippier,您粘贴的代码中还有其他不相关的crud。使快速阅读变得困难。