- Introduction
- Features
- Requirements
- Installation
- Usage
- Project Structure
- Compilation
- Running the Programs
- Code Explanation
- Contributing
- License
- Acknowledgements
mini_talk is a project developed for the 42 school curriculum. The goal of this project is to create a simple communication program in C using UNIX signals. The project consists of two programs: a server and a client. The server receives messages from the client and displays them.
- Server Program: Receives and displays messages sent by the client.
- Client Program: Sends messages to the server.
- Signal Handling: Uses UNIX signals (
SIGUSR1andSIGUSR2) for communication between the client and server. - Error Handling: Robust error handling for signal transmission and reception.
- Operating System: Linux or macOS
- Compiler: GCC or Clang
- Make: Build automation tool
-
Clone the Repository:
git clone https://github.com/yourusername/mini_talk.git cd mini_talk -
Build the Project:
make
-
Start the Server:
./server
-
Send a Message from the Client:
./client <server_pid> "Your message here"
Replace
<server_pid>with the process ID of the running server.
mini_talk/
├── inc/
│ ├── libft/
│ ├── ft_printf/
│ └── mini_talk.h
├── obj/
├── src/
│ ├── client/
│ │ └── client.c
│ ├── server/
│ │ └── server.c
│ └── libft/
│ └── ft_printf/
├── Makefile
└── README.md
- inc/: Header files and libraries.
- obj/: Compiled object files.
- src/: Source code for the client and server programs.
- Makefile: Build instructions.
- README.md: Project documentation.
To compile the project, simply run:
makeThis will compile the libraries and the client and server programs.
-
Start the Server:
./server
The server will print its process ID (PID). Note this PID as it will be used by the client to send messages.
-
Send a Message from the Client:
./client <server_pid> "Your message here"
Replace
<server_pid>with the PID of the running server.
The client program sends a message to the server using UNIX signals.
void smoke_signal(int pid, char *msg)
{
int i;
int j;
int k;
i = 0;
while (msg[i])
{
j = 0;
while (j < 8)
{
if (msg[i] & (1 << j))
k = kill(pid, SIGUSR1);
else
k = kill(pid, SIGUSR2);
if (k == -1)
{
ft_printf("Error: Invalid PID\n");
exit(1);
}
j++;
usleep(100);
}
i++;
}
}- Purpose: Sends each character of the message to the server bit by bit using
SIGUSR1andSIGUSR2signals. - Parameters:
pid: The process ID of the server.msg: The message to be sent.
- Logic:
- Iterates through each character of the message.
- For each character, sends 8 bits to the server using
SIGUSR1for1andSIGUSR2for0. - Uses
usleepto add a small delay between signals.
int main (int argc, char **argv)
{
int pid;
char *msg;
if (argc != 3)
{
ft_printf("Error: Invalid number of arguments\n");
return (1);
}
pid = ft_atoi(argv[1]);
if (pid < 0)
{
ft_printf("Error: Invalid PID\n");
return (1);
}
msg = argv[2];
smoke_signal(pid, msg);
return (0);
}- Purpose: Validates the input arguments and calls the
smoke_signalfunction to send the message. - Logic:
- Checks if the number of arguments is correct.
- Converts the server PID from string to integer.
- Validates the PID.
- Calls the
smoke_signalfunction with the PID and message.
The server program receives messages from the client and displays them.
void signal_handler(int signum)
{
static char c;
static int i;
if (signum == SIGUSR1)
c |= (1 << i);
i++;
if (i == 8)
{
if (c == '\0')
{
ft_printf("\n");
exit(0);
}
ft_printf("%c", c);
i = 0;
c = 0;
}
}- Purpose: Handles the signals sent by the client and reconstructs the message.
- Parameters:
signum: The signal number (SIGUSR1orSIGUSR2).
- Logic:
- Uses a static character
cand an indexito reconstruct each character bit by bit. - When 8 bits are received, prints the character.
- If the character is
'\0', prints a newline and exits.
- Uses a static character
int main(void)
{
struct sigaction signal;
pid_t pid;
pid = getpid();
ft_printf("Server PID: %d\n", pid);
signal.sa_handler = signal_handler;
signal.sa_flags = 0;
sigaction(SIGUSR1, &signal, NULL);
sigaction(SIGUSR2, &signal, NULL);
while (1)
{
pause();
}
}- Purpose: Sets up the signal handler and waits for signals from the client.
- Logic:
- Prints the server PID.
- Sets up the
signal_handlerfunction to handleSIGUSR1andSIGUSR2. - Enters an infinite loop, waiting for signals using
pause.
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Commit your changes (
git commit -am 'Add new feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
This project is licensed under the MIT License. See the
LICENSE
file for details.
- 42 School for the project inspiration and curriculum.
- GNU Project for the GCC compiler.
- UNIX for the signal handling mechanisms.