▪️Functions used
You are allowed to use several functions in this project. You already know some of them like write, ft_printf (yours!), malloc, free and all the functions from your libft. However, other important functions that have never been used before will be essential to the success of this project. Let's look at them together.
signal()
sighandler_t signal(int signum, sighandler_t handler);
The signal
function in C is a way to specify a function, called a signal handler, to be called when a specific signal is received by a running program. A signal is a message from the operating system to a program indicating that some event has occurred. The signal
function allows you to specify a function to be called when a particular signal is received, so that you can take some action in response to the signal.
sigemptyset()
int sigemptyset(sigset_t *set);
The sigemptyset
function is used to initialize a signal set to the empty set, which means that it does not contain any signals. Signal sets are used by some functions, such as sigaction
, to define the signals to be processed. The sigemptyset
function takes a pointer to a set of signals as an argument and empties this set by adding no signal to it. This function is often used in conjunction with the sigaddset
function, which adds a specified signal to a signal set.
sigaddset()
int sigaddset(sigset_t *set, int signum);
This function allows to add a signal to a set of signals. The sigaddset
function takes two arguments: a pointer to a set of signals and the number of the signal to add to the set.
Here is an example of using sigaddset
to add the signal SIGINT to a set of signals and sigemptyset
:
sigaction()
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
The sigaction
function in C is used to specify the action to be taken when a specific signal is received by a process. It is defined in the signal.h
header file.
The signum
argument specifies the signal for which the action is being specified. The act
argument is a pointer to a struct sigaction
that specifies the action to be taken when the signal is received. The oldact
argument is a pointer to a struct sigaction
that is used to retrieve the previous action for the specified signal.
kill()
int kill(pid_t pid, int sig);
In C, the kill
function is a system call that sends a signal to a process. It is defined in the signal.h
header file.
The pid
argument specifies the process ID of the process you want to communicate with. The sig
argument specifies the signal to be sent to the process. There are various signals that can be sent, each corresponding to a different purpose. For example, the SIGKILL
signal is used to terminate processes that are unresponsive or stuck in an infinite loop.
getpid()
pid_t getpid(void);
In C, the getpid
function returns the process ID of the current process. It is declared in the unistd.h
header file.
pause()
int pause(void);
pause()
is a function in the C standard library that causes the calling process to sleep until a signal is received. The process remains blocked until a signal handler is executed or the signal is ignored
sleep()
unsigned int sleep(unsigned int seconds);
sleep()
is also a function in the C standard library that causes the calling process to sleep for a specified number of seconds.
usleep()
int usleep(useconds_t usec);
usleep()
is a function in the C standard library that causes the calling process to sleep for a specified number of microseconds.
exit()
void exit(int status);
exit()
is a function in the C standard library that terminates the calling process immediately. It takes an integer argument that specifies the exit status of the process. A value of 0 indicates successful termination, while non-zero values indicate an error.
And... that's it. You should be able to make some tests alone right now ;) If you don't know how to do it, see you in the next step.
Last updated
Was this helpful?