Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by hbani-at.

Minitalk

Description

Minitalk is an inter-process communication (IPC) project that demonstrates signal-based communication between two processes. The project implements a simple client-server architecture where the client sends messages to the server using UNIX signals (SIGUSR1 and SIGUSR2) for bit-level communication.

Goal: To understand how signals work in UNIX systems and implement a practical IPC mechanism using signals to transmit data between processes.

Overview: The project consists of two programs:

  • Server: A process that listens for incoming signals and reconstructs messages by accumulating bits received from clients
  • Client: A process that sends a message to the server by converting each character into bits and transmitting them via signals

The communication protocol encodes each character as 8 bits, where SIGUSR1 represents a binary 1 and SIGUSR2 represents a binary 0. The server acknowledges each bit with a SIGUSR1 signal, ensuring reliable transmission.

Instructions

Compilation

Build the project using the provided Makefile:

make

This will compile both the server and client programs, creating server and client executables in the current directory.

Execution

Starting the Server:

./server

The server will start and display its PID. It will then wait indefinitely for incoming messages from clients.

Sending a Message via Client:

./client <server_pid> "<message>"

Replace <server_pid> with the PID displayed by the server and <message> with the message you want to send.

Example:

# Terminal 1: Start the server
./server
# Output: 12345

# Terminal 2: Send a message
./client 12345 "Hello, World!"

# Terminal 1: Server displays
# Hello, World!

Clean

Remove compiled binaries:

make clean

Remove all generated files:

make fclean

Resources

Man Pages

  • man 7 signal - Signal concepts and usage
  • man 2 signal - Signal system call interface
  • man 1 kill - Signal sending command
  • man 7 signal-safety - Signal-safe functions
  • man sigaction - Advanced signal handling
  • man sigemptyset - Initialize signal set
  • man pgrep - Process search and filtering
  • man pstree - Process tree display
  • man pause - Suspend process execution
  • man sleep - Sleep for seconds
  • man usleep - Sleep for microseconds
  • man malloc - Memory allocation
  • man free - Free memory
  • man 2 exit - Exit process

Learning Resources

AI Usage

No AI was used in the development of this project. All code was written manually to satisfy the 42 curriculum requirements for understanding signal-based inter-process communication.

Background: Linux Processes and Signals

What is a Process?

A process is a running instance of an executable program. It includes:

  • Address space
  • Security credentials
  • One or more threads
  • A state

Process States

State Flag Description
Running R Executing on a CPU or ready to run
Sleeping S Waiting for an event or resource (interruptible)
Uninterruptible D Waiting, cannot be interrupted
Stopped T Stopped by signal or for debugging
Zombie Z Process finished; parent must clean up

Process Control with Signals

A signal is a software interrupt delivered to a process. Signals report events such as errors, I/O events, expired timers, or manual commands.

Common Signals

Signal Name Description
1 HUP Hangup; reports terminal disconnection or requests config reload
2 INT Interrupt; stops program (Ctrl+C)
9 KILL Force kill; cannot be blocked or handled
15 TERM Terminate; default clean shutdown signal
18 CONT Resume a stopped process
19 STOP Stop (suspend) process, uncatchable
20 TSTP Terminal stop (Ctrl+Z); catchable
30 USR1 User-defined signal 1 (used in Minitalk for binary 1)
31 USR2 User-defined signal 2 (used in Minitalk for binary 0)

Note: Signal names are portable across platforms, but numbers may vary. Use signal names (like SIGKILL) instead of numbers when possible.

Sending Signals

From Keyboard:

Key Combo Signal Action
Ctrl+C SIGINT Terminate process
Ctrl+Z SIGTSTP Suspend process

Using kill Command:

kill PID                    # Send default SIGTERM
kill -9 PID                 # SIGKILL (force kill)
kill -SIGTERM PID           # Named signal
kill -SIGUSR1 PID           # Send SIGUSR1
kill -SIGUSR2 PID           # Send SIGUSR2

Using pkill:

pkill -u bob                # SIGTERM all bob's processes
pkill -SIGKILL -u bob       # Force kill all bob's processes

Zombie Processes

When a child process exits:

  • It leaves a zombie (remains in process table)
  • Parent must clean it up using wait(), freeing resources
  • Does not consume CPU or memory, just a slot in the process table

Viewing Process States

ps aux              # All processes
ps j                # Job/process/session details
top                 # Real-time process monitoring

Example output:

$ ps aux
USER   PID  %CPU %MEM ... STAT ... COMMAND
root     2  0.0  0.0 ... S    ... [kthreadd]
student 3448 0.0  0.2 ... R+   ... ps aux

Further Reading on Linux Processes and Job Control

Technical Implementation

Signal Handling

The project uses sigaction() for robust signal handling with SA_SIGINFO flag to receive sender PID information. This allows the server to:

  • Receive signals from the client
  • Extract the sender's PID using siginfo_t
  • Acknowledge bit reception by sending SIGUSR1 back to the client
  • Handle both SIGUSR1 and SIGUSR2 for bit values

Key signal handling concepts:

  • Signal handlers are functions that execute when a signal is received
  • sigaction() provides more control than the older signal() function
  • SA_SIGINFO flag enables access to siginfo_t structure with sender information
  • sig_atomic_t ensures atomic access to volatile variables modified by signal handlers

Bit Transmission Protocol

Minitalk uses SIGUSR1 and SIGUSR2 to transmit binary data:

  • SIGUSR1 = binary 1 (bit value is set)
  • SIGUSR2 = binary 0 (bit value is cleared)
  • Each character is transmitted MSB-first (bits 7 to 0)
  • Null terminator ('\0') signals message end
  • 400 microseconds delay between bits for reliability
  • Acknowledgment: Server sends SIGUSR1 after each bit, confirming receipt

Transmission Example for 'A' (ASCII 65 = 01000001):

01000001 → SIGUSR2 SIGUSR1 SIGUSR2 SIGUSR2 SIGUSR2 SIGUSR1 SIGUSR2 SIGUSR1

Key Files

  • server.c - Server implementation that receives and reconstructs messages
  • client.c - Client implementation that sends messages
  • error.c - Error handling and validation functions
  • minitalk.h - Header file with type definitions and function declarations
  • libft/ - Custom C library with utility functions

About

inter-process communication (IPC) project that demonstrates signal-based communication between two processes

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages