我有一个进程必须使用Ctrl-C或SIGINT终止。在这种情况下,我无法在键盘上使用Ctrl-C,因此必须在C中使用kill命令。这段代码基本上读取GPIO输入,并启动和停止一个脚本record.sh,一个GStreamer命令行脚本。
我只想终止在startVideoRecording()中创建的子进程。我需要随心所欲地开始、停止、开始、停止录音。这不是一次性的事情。
但是,当我更改gpio值并触发stopAllRecordings()时,它不会将SIGINT发送到子进程。它只是挂在"向GStreamer进程发送中断..状态0"
如何确保使用Ctrl-C等效程序终止视频录制过程?
谢谢。
这是代码:
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 threadn");
}
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 AVRecordingProcessn");
error = -1;
case 0:
execl("record.sh", "0", NULL);
//system("//root//record.sh");
printf("Error: Fail to execute AVRecordingProcessn");
error = -1;
default:
printf("Video Recording...process id %dn", AVRecordingProcess);
break;
}
} else {
printf("Error: MainHost failed to fork AVRecordingProcessn");
error = -1;
}
return error;
}
void stopAllRecordings() {
int ret;
FILE *infile, *outfile;
int ch;
printf("Terminating child processes..%dn", AVRecordingProcess);
ret = kill(AVRecordingProcess, SIGINT);
printf("Sending interrupt to GStreamer process.. Status %dn", ret);
int status;
printf("Wait for the GStreamer process %d to end....", AVRecordingProcess);
wait(AVRecordingProcess);
printf("GStreamer ended.n");
}
是否有可能启动多个子进程,并且只将SIGINT发送到您创建的最后一个子进程,而不是以前的子进程?键入"PS"并在启动此程序后点击回车键,查看您启动了多少进程。
我最终做到了:
int startVideoRecording() {
int error;
error = 0;
if ((AVRecordingProcess = fork()) != -1) {
switch (AVRecordingProcess) {
case -1:
printf("Error: Fail to create AVRecordingProcessn");
error = -1;
case 0:
setpgid(AVRecordingProcess,0); <-- NEW
execl("record.sh", "0", NULL);
//system("//root//record.sh");
printf("Error: Fail to execute AVRecordingProcessn");
error = -1;
default:
printf("Video Recording...process id %dn", AVRecordingProcess);
break;
}
} else {
printf("Error: MainHost failed to fork AVRecordingProcessn");
error = -1;
}
return error;
}
void stopAllRecordings() {
int ret;
FILE *infile, *outfile;
int ch;
printf("Terminating child processes..%dn", AVRecordingProcess);
ret = killpg(AVRecordingProcess, SIGINT); <-- NEW
printf("Sending interrupt to GStreamer process.. Status %dn", 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专家,请随时纠正我的错误,谢谢!