简体   繁体   中英

Terminate a thread that activates a process in the shell

Using pthreads, I created a thread that does audio recording through shell:

void *thread_function(void *arg) {

system("arecord data.wav");

}

However, when I call pthread_cancel(&thread_ID); to terminate the thread, the audio recorder still works on its own (Until I terminate the whole C program of course).

How do I stop a pthread that does a system call? Thanks in advance!

Your thread start function should do the following:

pid_t pid;
int status;
posix_spawnp(&pid, "arecord", 0, 0, (char *[]){"arecord", "data.wav", 0}, environ);
pthread_cleanup_push(cleanup, &pid);
while (waitpid(pid, &status, 0)<0 && errno==EINTR);
pthread_cleanup_pop(0);

With a cleanup function like:

static void cleanup(void *p)
{
    pid_t pid = *(pid_t *)p;
    kill(pid, SIGTERM);
    while (waitpid(pid, &status, 0)<0 && errno==EINTR);
}

Then cancelling the thread with pthread_cancel will kill the child process.

system("arecord data.wav");

It will make a separate process (not a thread in your program) in your system, and terminating that thread will not affect that process. You should kill that process by another system call.

However making the process with spawn* functions in non-waiting mode is a bit better than your way and in this case and you don't need an extra thread.

spawnl(P_NOWAIT, "arecord data.wav", .... );

But, killing the created process is ugly.

You can use pthread_kill to send a signal to a specific thread. The problem with thread cancellation is that it will be delayed until the program reaches a cancellation point ( see this answer ).

If you do use pthread_kill then you can choose which signal to send. I think it should be possible to end the thread with a kill signal which would then also end the child process spawned with system, although I'm not certain about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM