Skip to content
Snippets Groups Projects
Commit ee935379 authored by iliya's avatar iliya
Browse files

ya hate to see it

parent 9364d082
No related branches found
No related tags found
No related merge requests found
*.o
synchro
CC := clang
CFLAGS := -g -pedantic -Wall -Wextra -std=c11
LDFLAGS := -fsanitize=address -fsanitize=leak -fsanitize=undefined -lm -lpthread
TARGET := synchro
all: $(TARGET)
$(TARGET): synchro.o
@printf "=================== Building executable ===================\n"
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
@printf "\n"
%.o: %.c
@printf "================== Building object files ==================\n"
$(CC) $(CFLAGS) -c $<
@printf "\n"
.PHONY: clean
clean:
rm -f *.o $(TARGET)
.PHONY: rebuild
rebuild: clean all
#include <libgen.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define DATA_LEN (1 << 9)
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 2) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t./%s <nb_processes>\n", basename(argv[0]));
exit(EXIT_FAILURE);
}
int nb_processes = atoi(argv[1]);
fprintf(stdout, "P0 (main) (%d)\n", getpid());
for (int i = 0; i < nb_processes - 1; i++) {
pid_t pid = fork();
if (pid > 0 && i == 0) {
// int status;
// if (wait(&status) == -1) {
// perror("wait");
// exit(EXIT_FAILURE);
// }
//
// if (WIFEXITED(status)) {
// fprintf(stdout, "P%d terminated...\n", i + 1);
// }
} else if (pid == 0) {
int duration = rand() % (15 + 1 - 3) + 3;
sleep(duration);
fprintf(stdout, "P%d (%d) (%d)\t:\t%d sec.\n", i + 1, getpid(),
getppid(), duration);
} else if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
// srand(time(NULL));
//
// int arr_fd[2] = {0};
// char buff[DATA_LEN] = {0};
// int wstatus;
//
// for (size_t i = 0; i < DATA_LEN; i++) {
// buff[i] = rand() % INT8_MAX;
// fprintf(stdout, "%d ", buff[i]);
// }
// fprintf(stdout, "\n");
//
// if (pipe(arr_fd) == -1) {
// perror("pipe");
// }
//
// pid_t pid = fork();
//
// if (pid > 0) { // Parent
// // Parent closes exit FD
// close(arr_fd[0]);
//
// write(arr_fd[1], buff, DATA_LEN);
// close(arr_fd[1]);
//
// wait(&wstatus);
// if (WIFEXITED(wstatus)) {
// fprintf(stdout, "Child has terminated\n");
// }
// } else if (pid == 0) { // Child
// // Child closes entry FD
// close(arr_fd[1]);
// char child_buff[DATA_LEN] = {0};
// size_t count = 0;
// size_t bytes_read = 0;
//
// while ((bytes_read = read(arr_fd[0], child_buff, 16))) {
// count += bytes_read;
// }
//
// fprintf(stdout, "Number of bytes read: %lu\n", count);
// } else { // Error
// perror("fork");
// }
//
// return EXIT_SUCCESS;
}
#include <bits/types/sigset_t.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
volatile sig_atomic_t count = 0; volatile sig_atomic_t enabled = 0;
struct sigaction usr1;
static void handler_usr1(int __attribute__((unused)) signum) { void sigusr1_handler(int __attribute__((unused)) signum) {
// if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) { // sigset_t set; sigprocmask(SIG_BLOCK, NULL, &set);
// fprintf(stdout, "SIGTSTP will be unblocked...\n");
// //
// if (sigdelset(&usr1.sa_mask, SIGTSTP) == -1) { // if (sigismember(&set, SIGTSTP) == 1) {
// perror("sigdelset"); // char msg[] = "SIGTSTP will be unblocked..\n";
// write(STDOUT_FILENO, msg, strlen(msg));
//
// sigdelset(&set, SIGTSTP);
//
// if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
// perror("sigprocmask");
// } // }
// } else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) { // } else if (sigismember(&set, SIGTSTP) == 0) {
// fprintf(stdout, "SIGTSTP will be blocked...\n"); // char msg[] = "SIGTSTP will be blocked..\n";
// write(STDOUT_FILENO, msg, strlen(msg));
// //
// if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) { // sigaddset(&set, SIGTSTP);
// perror("sigaddset"); // if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
// perror("sigprocmask");
// } // }
// } else { // } else {
// perror("sigismember"); // perror("sigismember");
// } // }
}
void test(void) {
sigset_t set;
sigprocmask(SIG_BLOCK, NULL, &set);
if (sigismember(&set, SIGTSTP) == 1) {
char msg[] = "SIGTSTP will be unblocked..\n";
write(STDOUT_FILENO, msg, strlen(msg));
if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) { sigdelset(&set, SIGTSTP);
fprintf(stdout, "SIGTSTP will be unblocked...\n");
if (sigprocmask(SIG_UNBLOCK, &usr1.sa_mask, NULL) == -1) { if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
perror("sigprocmask"); perror("sigprocmask");
} }
sigaddset(&usr1.sa_mask, SIGTSTP); } else if (sigismember(&set, SIGTSTP) == 0) {
} else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) { char msg[] = "SIGTSTP will be blocked..\n";
fprintf(stdout, "SIGTSTP will be blocked...\n"); write(STDOUT_FILENO, msg, strlen(msg));
sigaddset(&usr1.sa_mask, SIGTSTP); sigaddset(&set, SIGTSTP);
if (sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL) == -1) { if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
perror("sigprocmask"); perror("sigprocmask");
} }
} else { } else {
...@@ -42,39 +59,27 @@ static void handler_usr1(int __attribute__((unused)) signum) { ...@@ -42,39 +59,27 @@ static void handler_usr1(int __attribute__((unused)) signum) {
} }
} }
// static void handler_usr2(int signum) { void sigusr2_handler(int __attribute__((unused)) signum) {}
// // fprintf(stdout, "Ctrl-C has been pressed %d times\n", counter);
// }
int main(void) { int main(void) {
usr1.sa_handler = handler_usr1; struct sigaction usr1 = {0};
usr1.sa_flags = 0; struct sigaction usr2 = {0};
sigemptyset(&usr1.sa_mask); // memset(&usr1, 0, sizeof(struct sigaction));
// memset(&usr2, 0, sizeof(struct sigaction));
sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL); usr1.sa_handler = sigusr1_handler;
usr2.sa_handler = sigusr2_handler;
if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) { sigemptyset(&usr1.sa_mask);
perror("sigaddset"); sigemptyset(&usr2.sa_mask);
}
if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) {
perror("sigprocmask");
exit(EXIT_FAILURE);
}
if (sigaction(SIGUSR1, &usr1, NULL) == -1) { if (sigaction(SIGUSR1, &usr1, NULL) == -1) {
perror("sigaction"); perror("sigaddset");
exit(EXIT_FAILURE);
} }
// struct sigaction usr2; if (sigaction(SIGUSR2, &usr2, NULL) == -1) {
// usr2.sa_handler = handler_usr2; perror("sigaddset");
// usr2.sa_flags = 0; }
// sigemptyset(&usr2.sa_mask);
// if (sigaddset(&usr2.sa_mask, SIGUSR2) == -1) {
// perror("sigaddset");
// }
while (true) { while (true) {
fprintf(stdout, "hello world\n"); fprintf(stdout, "hello world\n");
......
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
volatile sig_atomic_t count = 0;
struct sigaction usr1;
static void handler_usr1(int __attribute__((unused)) signum) {
if (sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL) == -1) {
perror("sigprocmask");
}
if (sigismember(&usr1.sa_mask, SIGTSTP) == 1) {
fprintf(stdout, "SIGTSTP will be unblocked...\n");
sigdelset(&usr1.sa_mask, SIGTSTP);
} else if (sigismember(&usr1.sa_mask, SIGTSTP) == 0) {
fprintf(stdout, "SIGTSTP will be blocked...\n");
sigaddset(&usr1.sa_mask, SIGTSTP);
} else {
perror("sigismember");
}
if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) {
perror("sigprocmask");
}
}
// static void handler_usr2(int signum) {
// // fprintf(stdout, "Ctrl-C has been pressed %d times\n", counter);
// }
int main(void) {
usr1.sa_handler = handler_usr1;
usr1.sa_flags = 0;
sigemptyset(&usr1.sa_mask);
sigprocmask(SIG_BLOCK, &usr1.sa_mask, NULL);
if (sigaddset(&usr1.sa_mask, SIGTSTP) == -1) {
perror("sigaddset");
}
if (sigprocmask(SIG_SETMASK, &usr1.sa_mask, NULL) == -1) {
perror("sigprocmask");
exit(EXIT_FAILURE);
}
if (sigaction(SIGUSR1, &usr1, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
// struct sigaction usr2;
// usr2.sa_handler = handler_usr2;
// usr2.sa_flags = 0;
// sigemptyset(&usr2.sa_mask);
// if (sigaddset(&usr2.sa_mask, SIGUSR2) == -1) {
// perror("sigaddset");
// }
while (true) {
fprintf(stdout, "hello world\n");
sleep(1);
}
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment