-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
123 lines (112 loc) · 2.31 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sahafid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/07 12:26:54 by sahafid #+# #+# */
/* Updated: 2022/02/07 12:26:55 by sahafid ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "libft/libft.h"
#include "Printf/ft_printf.h"
#include <signal.h>
void checkend(int *count, int pid)
{
int i;
int j;
i = 7;
j = 0;
while (i >= 0)
{
if (count[i] == 0)
j++;
else
break ;
i--;
}
if (j == 8)
{
usleep(500);
kill(pid, SIGUSR1);
}
}
void reset(int *i, int *count)
{
int j;
j = 7;
*i = 0;
while (j >= 0)
{
count[j] = 0;
j--;
}
}
void convertingtoascii(int *count, int pid, int *j)
{
int i;
int decimal;
int weight;
int rem;
*j = 0;
rem = 0;
decimal = 0;
weight = 1;
i = 7;
while (i >= 0)
{
rem = count[i];
decimal = decimal + rem * weight;
weight = weight * 2;
i--;
}
checkend(count, pid);
ft_putchar_fd(decimal, 1);
i = 7;
while (i >= 0)
{
count[i] = 0;
i--;
}
}
void send(int user, siginfo_t *var, void *ptr1)
{
static int i = 0;
static int count[8] = {0};
static int pid = 0;
ptr1 = (void *)0;
if (pid != var->si_pid)
{
pid = var->si_pid;
reset(&i, count);
}
if (user == SIGUSR1)
{
count[i++] = 1;
if (i == 8)
convertingtoascii(count, pid, &i);
}
else if (user == SIGUSR2)
{
count[i++] = 0;
if (i == 8)
convertingtoascii(count, pid, &i);
}
}
int main(int argc, char *argv[])
{
struct sigaction sa;
argv[1] = "";
argc = 1;
sa.sa_sigaction = send;
ft_printf("%d\n", getpid());
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
while (1)
sleep(1);
return (0);
}