4.karina.18_test.decoder 4.ilyas.18_test.decoder

--- 4.karina.18_test.decoder.fmt.c	2020-06-08 11:39:11.762115970 +0300
+++ 4.ilyas.18_test.decoder.fmt.c	2020-06-08 11:39:11.738115710 +0300
@@ -1,34 +1,36 @@
 #1 "<stdin>"
+#include <assert.h>
+#include <signal.h>
+#include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <stdbool.h>
-int global = 0;
-int x = 1;
-char c = 0;
-void func(int sig) {
-    if (sig == SIGUSR1)
-	c = c << 1;
-    else if (sig == SIGUSR2)
-	c = ((c << 1) + 1);
-    global++;
-    if (global == 8) {
-	if (c == 0) {
-	    x = 0;
+int done = 1;
+char current = 0;
+int len = 0;
+void signal_handler(int sig) {
+    current = current << 1;
+    if (sig == SIGUSR2)
+	current += 1;
+    len += 1;
+    if (len == 8) {
+	if (current == 0) {
+	    done = 0;
 	    return;
 	}
-	global = 0;
-	printf("%c", c);
+	len = 0;
+	printf("%c", current);
     }
 }
 int main(int argc, char *argv[]) {
-    printf("PID: %d\n", getpid());
-    signal(SIGUSR1, func);
-    signal(SIGUSR2, func);
-    for (int i = 0;; i++)
+    int pid;
+    pid = getpid();
+    printf("PID: %d\n", pid);
+    signal(SIGUSR1, signal_handler);
+    signal(SIGUSR2, signal_handler);
+    while (done)
 	sleep(1);
     return 0;
 }