GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - pseudocc/zig-ipc: Zig shared memory IPC example (POSIX)

BranchesTags
Open more actions menu

Latest commit

 

History

5 Commits
5 Commits

Folders and files

NameName
Last commit message
Last commit date
nix
nix
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.zig
build.zig
 
 
client.zig
client.zig
 
 
flake.lock
flake.lock
 
 
flake.nix
flake.nix
 
 
server.zig
server.zig
 
 
shared.zig
shared.zig
 
 

Repository files navigation

ZIG-IPC

Zig code example to use shared memory among processes, only works on POSIX.

How it works?

  1. Use shm_open and mmap to create a shared memory block.

  2. Use atomic operations to manage a boolean value (busy).

    pub inline fn lock(self: *Self, comptime value: bool) void {
        while (true) {
            _ = @cmpxchgStrong(bool, &self.ptr.busy, !value, value, .seq_cst, .seq_cst) orelse break;
        }
    }

    Updates on the rest memory block requires busy to be false, and set to true before the update.

Run this example

Build and start the server. Press CTRL+C to exit.

zig build # compiles against v0.11.0
zig-out/bin/ipc-server

Start the client in another terminal.

zig-out/bin/ipc-client

Example output

Server process:

pseudoc $ zig-out/bin/ipc-server 
Sorting(3): { 6, 80, 68 }
Sorted(3): { 6, 68, 80 }
Sorting(4): { 6, 68, 80, 87 }
Sorted(4): { 6, 68, 80, 87 }
Sorting(5): { 6, 68, 80, 87, 63 }
Sorted(5): { 6, 63, 68, 80, 87 }
Sorting(6): { 6, 63, 68, 80, 87, 3 }
Sorted(6): { 3, 6, 63, 68, 80, 87 }
Sorting(7): { 3, 6, 63, 68, 80, 87, 86 }
Sorted(7): { 3, 6, 63, 68, 80, 86, 87 }
Sorting(8): { 3, 6, 63, 68, 80, 86, 87, 33 }
Sorted(8): { 3, 6, 33, 63, 68, 80, 86, 87 }
Sorting(9): { 3, 6, 33, 63, 68, 80, 86, 87, 0 }
Sorted(9): { 0, 3, 6, 33, 63, 68, 80, 86, 87 }
Sorting(10): { 0, 3, 6, 33, 63, 68, 80, 86, 87, 13 }
Sorted(10): { 0, 3, 6, 13, 33, 63, 68, 80, 86, 87 }
Sorting(11): { 0, 3, 6, 13, 33, 63, 68, 80, 86, 87, 11 }
Sorted(11): { 0, 3, 6, 11, 13, 33, 63, 68, 80, 86, 87 }
Sorting(12): { 0, 3, 6, 11, 13, 33, 63, 68, 80, 86, 87, 70 }
Sorted(12): { 0, 3, 6, 11, 13, 33, 63, 68, 70, 80, 86, 87 }
Sorting(13): { 0, 3, 6, 11, 13, 33, 63, 68, 70, 80, 86, 87, 25 }
Sorted(13): { 0, 3, 6, 11, 13, 25, 33, 63, 68, 70, 80, 86, 87 }

Client process:

pseudoc $ zig-out/bin/ipc-client 
Filled(3): { 6, 80, 68 }
Appended(1): 87
Appended(2): 63
Appended(3): 3
Appended(4): 86
Appended(5): 33
Appended(6): 0
Appended(7): 13
Appended(8): 11
Appended(9): 70
Appended(10): 25

References

Atomic Ordering

C POSIX IPC example

About

Zig shared memory IPC example (POSIX)

Topics

ipcshared-memoryzig

Resources

Readme
MIT license
Activity

Stars

6 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.