Skip to content

mazen-tech/minitalk.c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linkedin 42

AboutHow to useMandatoryBonusNorminetteLicense

ABOUT

This is a server/client system that uses UNIX signals for communication. The server displays its PID and receives decrypted signals as characters, while the client encrypts messages and sends true/false signals using SIGUSR1 or SIGUSR2 to the server's PID.

Stuff that you need to know

getpid(void) --> is a system call in Unix-like operating systems that retrieves the process ID (PID) of the calling process and it doesn't accept any arrgument.

#include <unistd.h>
#include <stdio.h>

int main() {
    printf("My PID is: %d\n", getpid());
    return 0;
}

getppid(void) --> is a system call in Unix-like operating systems that retrieves the parent process ID (PPID) of the calling process and it also doesnot accept any argument at getpid.

#include <unistd.h>
#include <stdio.h>

int main() {
    printf("My parent's PID is: %d\n", getppid());
    return 0;
}

kill(pid, signal) --> is a system call used to send a signal to a process or a group of processes.

It can be used to terminate or send other signals to a process based on the signal number provided

#include <signal.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    int pid = getpid();
    kill(pid, SIGTERM); // Terminates the current process
    return 0;
}

sleep("number of seconds") --> is a function that causes the current thread to sleep for a specified number of seconds.

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("Sleeping for 5 seconds...\n");
    sleep(5); // Sleeps for 5 seconds
    printf("Woke up after sleeping.\n");
    return 0;
}

fork() --> is a system call that creates a new process by duplicating the calling process.

After a successful fork(), two identical processes are created: the parent process and the child process.

#include <stdio.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();
    if (pid == 0) {
        printf("Child process\n");
    } else if (pid > 0) {
        printf("Parent process\n");
    } else {
        fprintf(stderr, "Error in forking\n");
    }
    return 0;
}

exit() is a library function used to terminate the calling process

#include <stdlib.h>

int main() {
    // Some program logic...

    if (/* Some condition for error */) {
        // Error occurred, terminate program with non-zero status
        exit(1);
    }

    // Otherwise, exit program normally with zero status
    exit(0);
}

signal() function is used to establish signal handlers for specific signals.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void sigint_handler(int signum) {
    printf("Received SIGINT signal\n");
    // Perform any necessary actions in response to the signal
}

int main() {
    // Registering SIGINT handler
    signal(SIGINT, sigint_handler);

    // Your program logic goes here
    // ...

    return 0;
}

sigemptyset():

This function initializes an empty signal set.
It clears all signals from the given signal set.
It takes a pointer to a sigset_t data structure, which represents the set of signals.
#include <stdio.h>
#include <signal.h>

int main() {
    sigset_t set;
    // Initialize an empty signal set
    sigemptyset(&set);

    // Now 'set' represents an empty signal set
    // You can add signals to it using sigaddset() if needed

    return 0;
}

sigaddset():

This function adds a specified signal to the given signal set.
It allows you to specify which signals you want to include in the signal set.
It takes two arguments: a pointer to a sigset_t data structure representing the signal set, and the signal number you want to add to the set
#include <stdio.h>
#include <signal.h>

int main() {
    sigset_t set;
    // Initialize an empty signal set
    sigemptyset(&set);

    // Add SIGINT and SIGTERM to the signal set
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGTERM);

    // Now 'set' contains SIGINT and SIGTERM signals
    // You can use sigismember() to check if a signal is in the set

    return 0;
}

sigaction():

This function is used to change the action taken by a process upon receipt of a specific signal.
It allows you to specify a new signal handler function for a given signal.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

// Signal handler function for SIGINT
void sigint_handler(int signum) {
    printf("Received SIGINT signal\n");
    // Perform any necessary cleanup actions
    // Terminate the program
    exit(EXIT_SUCCESS);
}

int main() {
    // Set up signal handler for SIGINT using sigaction
    struct sigaction sa;
    sa.sa_handler = sigint_handler; // Specify the handler function
    sigemptyset(&sa.sa_mask);       // Clear the signal mask during handling
    sa.sa_flags = 0;                // No special flags

    // Register the handler for SIGINT
    if (sigaction(SIGINT, &sa, NULL) == -1) {
        perror("sigaction");
        exit(EXIT_FAILURE);
    }

    // Main program loop
    while (1) {
        // Your program logic goes here
        // In this example, we just sleep indefinitely
        sleep(1);
    }

    return 0;
}

HOW TO USE

1º - Clone the repository

git clone https://github.com/mazen-tech/minitalk.git

2º - Enter the project folder and run make

cd minitalk/minitalk
make

3º - Run server or server_bonus and copy its PID

./server
./server_bonus

4º - Open another terminal, run client or client_bonus, paste the PID and write a message

./client [SERVER PID] [MESSAGE]
./client_bonus [SERVER PID] [MESSAGE] [EMOTE]

MAKEFILE RULES

make - Compile minitalk mandatory files.

make bonus - Compile minitalk bonus files.

make all - Compile mandatory + bonus files.

make clean - Delete all .o (object files) files.

make fclean - Delete all .o (object files) and .a (executable) files.

make re - Use rules fclean + all.

MANDATORY

  • Produce server and client executables.
  • The Client must communicate a string passed as a parameter to the server (referenced by its process ID) and display it.
  • Use SIGUSR1 and SIGUSR2 signals only.

BONUS

  • Add reception acknowledgement system.
  • Support Unicode characters.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors