Skip to content
Snippets Groups Projects
server_4.c 3.16 KiB
Newer Older
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/wait.h>

volatile bool running = true;
int pipefd[2]; // File descriptors for pipe

void print_in_exit() {
    std::cout << "Message during exit" << std::endl;
}

void handle_signal(int signal_number) {
    std::cout << "Received signal: " << signal_number << std::endl;
    std::cout << "Stopping the program" << std::endl;
    running = false;
}

void print_process_info(const char* process_type) {
    std::cout << process_type << " | id: " << getpid() << std::endl;
    std::cout << process_type << " | father id: " << getppid() << std::endl;
    std::cout << process_type << " | groupd id: " << getpgrp() << std::endl;
}

int main(){
    std::cout << "started" << std::endl;

    if (atexit(print_in_exit) != 0) {
        perror("Fail in exit");
        exit(EXIT_FAILURE);
    }

    struct sigaction sa;
    sa.sa_handler = handle_signal;
    sa.sa_flags = 0;
    sigemptyset(&sa.sa_mask);

    if (sigaction(SIGINT, &sa, nullptr) == -1) {
        perror("Fail in exit");
        exit(EXIT_FAILURE);
    }

    if (sigaction(SIGTERM, &sa, nullptr) == -1) {
        perror("Fail in exit");
        exit(EXIT_FAILURE);
    }

    // Create the pipe
    if (pipe(pipefd) == -1) {
        perror("Pipe failed");
        exit(EXIT_FAILURE);
    }

    pid_t pid = fork();
    if (pid == 0) {
        close(pipefd[1]);

        int number;
        while (running) {

            ssize_t bytes_read = read(pipefd[0], &number, sizeof(number));
            if (bytes_read > 0) {
                std::cout << "Child received: " << number << std::endl;
            }
            sleep(1);
        }

        close(pipefd[0]);
    } else {
        close(pipefd[0]);

        int number = 0;
        while (running) {
            print_process_info("Parent");

            write(pipefd[1], &number, sizeof(number));
            number++;

            sleep(1);
            int status;
            pid_t result = waitpid(pid, &status, WNOHANG);
            if (result == pid) {
                std::cout << "Child stopped, father stopping" << std::endl;
                running = false;
            }
        }

        close(pipefd[1]);
    }

    std::cout << "finished" << std::endl;

    return EXIT_SUCCESS;
}
/*
with ctr+c
Child received: 45
Parent | id: 2934
Parent | father id: 2610
Parent | groupd id: 2934
Child received: 46
^CReceived signal: 2
Stopping the program
Received signal: 2
Stopping the program
finished
Message during exit
finished
Message during exit

whil kill parent
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 10
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 11
Received signal: 15
Stopping the program
finished
Message during exit

with kill child
Child received: 41
Parent | id: 3644
Parent | father id: 2610
Parent | groupd id: 3644
Child received: 42
Received signal: 15
Stopping the program
finished
Message during exit
Child stopped, father stopping
finished
Message during exit

*/