Coder Social home page Coder Social logo

ling-lang / 42_minitalk Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.29 MB

Client-server communication program using custom signals in C for transmitting strings of characters. Demonstrates inter-process communication and signal handling.

C 11.25% Makefile 1.22% Shell 87.53%
42 c minitalk signals unix

42_minitalk's Introduction

Minitalk

Minitalk is a simple demonstration of UNIX signals as a method of inter-process communication (IPC). The program consists of two parts: a server and a client.

How it Works

The server waits for signals from a client, and the client sends signals that include a message to the server. The server then prints the message received from the client.

Each character of the message is sent as a series of signals representing the binary encoding of the character. Specifically, SIGUSR1 represents a binary 0 and SIGUSR2 represents a binary 1. After each character, the client sends an additional eight SIGUSR1 signals to indicate the end of the character.

When the server receives a signal, it adds the bit represented by the signal to a buffer. Once eight signals have been received (i.e., a full character in ASCII), the server prints the character and resets the buffer. When the server receives eight SIGUSR1 signals in a row, it interprets this as the end of the message.

Usage

To use Minitalk, first start the server in a terminal:

./server

The server will print its process ID (PID) to the console. This PID will be used to send signals to the server.

Then, in a separate terminal, send a message from the client to the server:

./client [server_PID] [message]

Replace [server_PID] with the PID printed by the server and [message] with the message you want to send. The message can be any string without spaces.

For example:

./client 12345 "HelloWorld"

This command will send the message "HelloWorld" to the server with PID 12345.

Code Overview

client.c

The client program takes two command-line arguments: the PID of the server and the message to send.

The function mt_kill sends the message to the server. For each character in the string, it sends eight signals representing the binary encoding of the character. It then sends eight additional SIGUSR1 signals to indicate the end of the character.

server.c

The server program prints its PID and then waits for signals.

The function action is called when a signal is received. It adds the bit represented by the signal to a buffer. When eight signals have been received, it prints the character represented by the buffer and resets the buffer.

Note

The code assumes that the client and server are on the same machine. If they are on different machines, they need to be able to send and receive signals to each other, which may require additional configuration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.