Skip to content
Teresa Chow edited this page Nov 10, 2024 · 2 revisions

Minitalk notes

42 School: Rank 2

This document contains my notes about 42 Common Core curriculum project "Minitalk".


Table of contents

Subject instructions · References & further reading


Subject instructions

Mandatory

  • Name of executable files: client and server.
  • Turn in a Makefile to compile source files. It must not relink.
  • Use of libft is allowed.
  • Errors must be handled.
  • No memory leaks.
  • Up to one global varible per program (one for the client, one for the server).
  • The server must be started first. After its launch, it has to print its PID.
  • The client takes two parameters: server PID + string to send.
  • The client must send the string passed as a parameter to the server. Once the string has been received, the server must print it. (Original, in French: Une fois la chaîne entièrement reçue, le serveur doit l’afficher.)
  • We can only use these two signals: SIGUSR1 and SIGUSR2.
  • The server should be able to receive strings from several clients in a row without needing to restart.
  • Communication between client and your server has to be done only using UNIX signals.
  • Use of the following functions is allowed:
    Function Library Description / Notes
    write unistd.h
    signal signal.h ANSI C signal handling. ⚠️ WARNING: the behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead.
    sigemptyset signal.h int sigemptyset(sigset_t *set);Returns 0 on success and -1 on error.sigemptyset() initializes the signal set given by set to empty, with all signals excluded from the set.💡 OBS.: Objects of type sigset_t must be initialized by a call to either sigemptyset() or sigfillset() before being passed to the functions sigaddset(), sigdelset(), and sigismember() or the additional glibc functions described below (sigisemptyset(), sigandset(), and sigorset()). The results are undefined if this is not done.
    sigaddset signal.h int sigaddset(sigset_t *set, int signum);Returns 0 on success and -1 on error.sigaddset() adds signal signum from set.
    sigaction signal.h int sigaction(int signum, const struct sigaction *restrict act, struct sigaction *restrict oldact);sigaction examines and changes a signal action. The sigaction() system call is used to change the action taken by a process on receipt of a specific signal.struct sigaction {void (*sa_handler)(int);void (*sa_sigaction)(int, siginfo_t *, void *);sigset_t sa_mask;int sa_flags;void (*sa_restorer)(void);};
    The sa_flags field can be used to modify the behavior of the specified signal:If SA_SIGINFO is set and the signal is caught, the signal-catching function shall be entered as: void func(int signo, siginfo_t *info, void *context);where two additional arguments are passed to the signal-catching function.typedef struct { int si_signo; int si_code; union sigval si_value; int si_errno; pid_t si_pid; uid_t si_uid; void *si_addr; int si_status; int si_band;} siginfo_t;
    kill signal.h int kill(pid_t pid, int sig);kill sends a signal to a process. 💡 OBS.:
    • Negative PID values may be used to choose whole process groups; see the PGID (Process Group ID) column in ps command output.
    • A PID of 0 indicates all of the processes in the calling process’ group.
    • A PID of -1 indicates all processes except the kill process itself and init.
    getpid unistd.h pid_t getpid(void);getpid() returns the process ID (PID) of the calling process. (This is often used by routines that generate unique temporary filenames.)
    malloc stdlib.h
    free stdlib.h
    pause unistd.h Wait for signal.pause() causes the calling process (or thread) to sleep until a signal is delivered that either terminates the process or causes the invocation of a signal-catching function.pause() returns only when a signal was caught and the signal-catching function returned. In this case, pause() returns -1, and errno is set to EINTR.
    sleep Delay for a specified amount of time (seconds).
    usleep unistd.h usleep suspends execution for microsecond intervals.The usleep() function suspends execution of the calling thread for (at least) usec microseconds. The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.
    exit

Bonus

  • The server acknowledges every message received by sending back a signal to the client.
  • Unicode characters support.

References & further reading

Processes

Signals

Bitwise operators & bit shifting

Makefile