/* * Uma thread pode corromper o conteúdo da pilha de outra threaad. */ #include #include #include int* ret_0; /* Endereço de retorno em um dado instante da execução da thread0 */ int f(int param_f) { int v[2]; ret_0 = &v[3]; /* Veja o código corrompe_pilha */ sleep(2); printf("Vou morrer...\n"); return 0; } void* f_thread0(void *a) { printf("Thread 0.\n"); f(0); return NULL; } void* f_thread1(void *a) { printf("Thread 1.\n"); sleep(1); /* Com grande probabilidade, thr0 já foi escalonada e está dormindo. */ *ret_0 = 0; /* Corrompe pilha da thread 0 */ return NULL; } int main() { pthread_t thr0, thr1; pthread_create(&thr0, NULL, f_thread0, NULL); pthread_create(&thr1, NULL, f_thread1, NULL); pthread_join(thr0, NULL); pthread_join(thr1, NULL); return 0; }