-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Teresa Chow edited this page Nov 10, 2024
·
2 revisions
This document contains my notes about 42 Common Core curriculum project "Minitalk".
Subject instructions · References & further reading
- Name of executable files:
client
andserver
. - 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 theserver
). - 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 theserver
. 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
andSIGUSR2
. - 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 ofsignal()
varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: usesigaction(2)
instead.sigemptyset signal.h int sigemptyset(sigset_t *set);
Returns0
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 typesigset_t
must be initialized by a call to eithersigemptyset()
orsigfillset()
before being passed to the functionssigaddset()
,sigdelset()
, andsigismember()
or the additional glibc functions described below (sigisemptyset()
,sigandset()
, andsigorset()
). The results are undefined if this is not done.sigaddset signal.h int sigaddset(sigset_t *set, int signum);
Returns0
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. Thesigaction()
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);
};
Thesa_flags
field can be used to modify the behavior of the specified signal:IfSA_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 thekill
process itself andinit
.
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.Theusleep()
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 - Negative PID values may be used to choose whole process groups; see the PGID (Process Group ID) column in
- The
server
acknowledges every message received by sending back a signal to theclient
. - Unicode characters support.
- The GNU C Library Reference Manual (version 2.40.): Signal Handling
- Code(quoi); : Sending and intercepting a signal in C
- Understanding signals in Linux system [video]
- Code Vault: Short introduction to signals in C [video]
- Code Vault: Handling signals [video]
- Code Vault: Communicating between processes using signals [video]