-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_slave1.c
91 lines (74 loc) · 1.76 KB
/
master_slave1.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
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
/*
* =====================================================================================
*
* Filename: master_slace.c
*
* Description: This file demonstrates the use of wait and signal APIs
*
* Version: 1.0
* Created: 12/01/2025 11:42:20 PM
* Revision: none
* Compiler: gcc
*
* Author: BRUCE MIGEI(), bmigeri@gmail.com
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#define N_SLAVES 5
pthread_t slaves[N_SLAVES];
void* write_into_file(void *arg){
char file_name[64];
char string_to_write[64];
int len;
int count = 0;
int *thread_id = (int *)arg;
sprintf(file_name, "thread_%d.txt", *thread_id);
FILE *fptr = fopen(file_name, "w");
if(!fptr){
printf("Error : Could not open log file %s, errno = %d\n",
file_name, errno);
return 0;
}
while(1) {
len = sprintf(string_to_write, "%d : I am thread %d\n", count++, *thread_id);
fwrite(string_to_write, sizeof(char), len, fptr);
fflush(fptr);
sleep(1);
}
return 0;
}
int main(int argc, char **argv){
int i;
int *thread_id = 0;
for( i = 0; i < N_SLAVES; i++){
thread_id = calloc(1, sizeof(*thread_id));
*thread_id = i;
pthread_create(&slaves[i], 0, write_into_file, thread_id);
}
/* main menu */
int choice;
int thread_num;
while(1) {
printf("1. Cancel the thread\n");
scanf("%d", &choice);
printf("Enter slave thread id [0-%d] :", N_SLAVES -1);
scanf("%d", &thread_num);
if(thread_num < 0 || thread_num >= N_SLAVES) {
printf("Invalid Thread id\n");
exit(0);
}
printf("\n");
switch(choice) {
case 1:
break;
default:
continue;
}
}
return 0;
}