Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <math.h>
- #include <signal.h>
- int count = 0;
- int mode = 0;
- const uint32_t adress_mask = 1073676288;
- const uint32_t host_mask = 16383;
- uint32_t is_category_b(uint32_t num) {
- if (num >> 30 == 2) {
- return 1;
- }
- return 0;
- }
- uint32_t get_adress(uint32_t num) {
- return (num & adress_mask) >> 16;
- }
- uint32_t get_host(uint32_t num) {
- return num & host_mask;
- }
- void signal_handler(int signo) {
- if (count == 4) {
- exit(0);
- }
- if (signo == SIGUSR1) {
- mode = 1 - mode;
- ++count;
- } else if (signo == SIGUSR2) {
- uint32_t num;
- scanf("%o", &num);
- if (is_category_b(num)) {
- if (mode == 0) {
- printf("%d\n", get_adress(num));
- fflush(stdout);
- } else if (mode == 1) {
- printf("%d\n", get_host(num));
- fflush(stdout);
- }
- } else {
- printf("%d\n", 0);
- }
- }
- }
- int main(int argc, char ** argv) {
- struct sigaction sa = {};
- sa.sa_handler = signal_handler;
- sa.sa_flags = SA_RESTART;
- sigaction(SIGUSR1, &sa, NULL);
- sigaction(SIGUSR2, &sa, NULL);
- printf("%d\n", getpid());
- fflush(stdout);
- while (42) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement