Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Created April 11, 2018 12:01
Show Gist options
  • Save shinaisan/c0f1c515e4df59f749724968a7842feb to your computer and use it in GitHub Desktop.
Save shinaisan/c0f1c515e4df59f749724968a7842feb to your computer and use it in GitHub Desktop.
Example of pthread_kill

pthread_kill Example

This short program deliberately kills a thread after pthread_join so it may cause SEGV on some targets.

Compiling

gcc -pthread thread1.c
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define N 5
void *thread_function(void *arg) {
int i;
for (i = 0; i < N; i++) {
printf("Hello!\n");
sleep(1);
}
return NULL;
}
int main(void) {
pthread_t th;
int rc = 0;
if (pthread_create(&th, NULL, thread_function, NULL)) {
printf("Error creating thread.\n");
abort();
}
if (pthread_join(th, NULL)) {
printf("Error joining thread.");
abort();
}
if ((rc = pthread_kill(th, SIGTERM)) != 0) {
printf("Error in pthread_kill; errno:%x, message:%s\n", rc, strerror(rc));
abort();
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment