-
Notifications
You must be signed in to change notification settings - Fork 0
/
posix_tests1.c
66 lines (54 loc) · 1.36 KB
/
posix_tests1.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
64
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
void *thread_function1(void *arg);
void *thread_function2(void *arg);
int main(int argc, char *argv[])
{
int params_counter, i=0, j, parameter;
pthread_t thread_id1, thread_id2;
void *exit_status1, *exit_status2;
printf("Running program: %s\n", argv[0]);
for (params_counter=1; params_counter < argc; params_counter++)
{
parameter = atoi(argv[params_counter]);
printf("With parameter%d: %d\n", params_counter, parameter);
}
pthread_create(&thread_id1, NULL, thread_function1, ¶meter);
pthread_create(&thread_id2, NULL, thread_function2, ¶meter);
pthread_join(thread_id1, &exit_status1);
pthread_join(thread_id2, &exit_status2);
return 0;
}
void *thread_function1(void *arg)
{
int i=*((int*)arg), j=0;
pid_t tid = syscall(__NR_gettid);
printf("thread %d started with parameter: %d\n", tid, i);
while (j < i)
{
printf("thread id: %d. Current j: %d\n", tid, j);
j++;
//usleep(1000);
sched_yield();
}
}
void *thread_function2(void *arg)
{
int i=0, j=0;
i = *((int*)arg);
pid_t tid = syscall(__NR_gettid);
printf("thread %d started with parameter: %d\n", tid, i);
while (j < i)
{
printf("thread id: %d. Current j: %d\n", tid, j);
j++;
//usleep(1000);
sched_yield();
}
}