-
Notifications
You must be signed in to change notification settings - Fork 0
/
21_Thread_With_Parameter.c
63 lines (54 loc) · 1.53 KB
/
21_Thread_With_Parameter.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Q. Create 5 thread and each individual thread function will print the parameter/id passed from the called thread.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *fun(void *p)
{
long tid = (long)p;
printf("Thread is called with parameter: %ld\n", tid);
pthread_exit(NULL);
}
int main()
{
// Thread declaration
pthread_t th[5];
printf("Main function starts!\n");
// Thread creation
int res;
for (long i = 0; i < 5; i++)
{
res = pthread_create(&th[i], NULL, fun, (void *)i);
if (res != 0)
{
printf("Thread not created!\n");
}
// pthread_join(th[i], NULL);
}
sleep(20);
printf("Main function ends!\n");
}
/*
Output:
[With pthread_join]
s4shibam@SHIBAM:~/OS$ gcc 21_Thread_With_Parameter.c -lpthread -o t.out
s4shibam@SHIBAM:~/OS$ ./t.out
Main function starts!
Thread is called with parameter: 0
Thread is called with parameter: 1
Thread is called with parameter: 2
Thread is called with parameter: 3
Thread is called with parameter: 4
Main function ends!
s4shibam@SHIBAM:~/OS$
[Without pthread_join]
s4shibam@SHIBAM:~/OS$ gcc 21_Thread_With_Parameter.c -lpthread -o t.out
s4shibam@SHIBAM:~/OS$ ./t.out
Main function starts!
Thread is called with parameter: 0
Thread is called with parameter: 1
Thread is called with parameter: 2
Thread is called with parameter: 3
Thread is called with parameter: 4
Main function ends!
s4shibam@SHIBAM:~/OS$
*/