-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.c
More file actions
93 lines (72 loc) · 1.58 KB
/
task1.c
File metadata and controls
93 lines (72 loc) · 1.58 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/file.h>
#define THREAD_AMOUNT 10
void* create_file(void* arg)
{
int random = rand() % 2;
sleep(random);
int number = *(int*)arg;
char filename[10];
snprintf(filename, sizeof(filename), "thread%d", number);
FILE *file;
if((file = fopen(filename, "w")) == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
int tid = pthread_self();
// File operations are already thread safe, so there is no need for more portection
if(fprintf(file, "%d", tid) == -1)
{
perror("fprintf");
exit(EXIT_FAILURE);
}
fclose(file);
return NULL;
}
int one_or_zero()
{
int number = rand() % 2;
return number;
}
int main(void) {
srand(time(NULL));
pthread_t tid[THREAD_AMOUNT];
for(int i = 0; i < THREAD_AMOUNT; i++)
{
if(pthread_create(&tid[i], NULL, create_file, &i))
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
if(pthread_join(tid[i], NULL))
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
//pthread_detach(tid[i]);
}
for(int j = 0; j < THREAD_AMOUNT; j++)
{
if(one_or_zero() == 0)
{
// cancel thread
pthread_cancel(tid[j]);
printf("Canceled thread %d\n", j);
}
}
for(int i = 0; i < 10; i++)
{
pthread_exit(&tid[i]);
}
return EXIT_SUCCESS;
}