include stdio include stdlib include pthread include string include un

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
void * thread_body(void * param) {
while(1)
{
printf("Child\n");
}
}
int main(int argc, char *argv[]) {
pthread_t thread;
int code;
int i;
code=pthread_create(&thread, NULL, thread_body, NULL);
if (code!=0) {
char buf[256];
strerror_r(code, buf, sizeof buf);
fprintf(stderr, "%s: creating thread: %s\n", argv[0], buf);
exit(1);
}
sleep(2);
printf("slept for 2 secs\n");
pthread_cancel(thread);
pthread_join(thread, NULL);
printf("child thread exited actually\n");
sleep(3);
return (EXIT_SUCCESS);
}
/*
worklez@perkunas:~/term_5$ ./a.out | uniq
Child
slept for 2 secs
child thread exited actually
worklez@perkunas:~/term_5$
*/