Created
September 22, 2012 09:12
-
-
Save quiver/3765624 to your computer and use it in GitHub Desktop.
Michael Kerrisk : "The Linux Programming Interface" CH.44 : sequence number server(Python Implementation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael Kerrisk : "The Linux Programming Interface" CH.44 : sequence number server(Python Implementation) | |
# fifo_seqnum_client | |
import atexit | |
import os | |
import struct | |
import sys | |
import config | |
pid = os.getpid() | |
CLIENT_FIFO = config.CLIENT_FIFO_TEMPLATE % pid | |
@atexit.register | |
def remove_fifo(): | |
os.remove(CLIENT_FIFO) | |
def main(): | |
os.mkfifo(CLIENT_FIFO) | |
seq_len = int(sys.argv[1]) if len(sys.argv) > 1 else 1 | |
# print 'client', pid, seq_len | |
# Construct request message, open server FIFO, and send message | |
wr_fd = os.open(config.SERVER_FIFO, os.O_WRONLY) | |
os.write(wr_fd, struct.pack(config.CLIENT_FORMAT, pid, seq_len)) | |
os.close(wr_fd) | |
# Open our FIFO, read and display response | |
rd_fd = os.open(CLIENT_FIFO, os.O_RDONLY) | |
resp = os.read(rd_fd, config.SERVER_SIZE) | |
os.close(rd_fd) | |
print struct.unpack(config.SERVER_FORMAT, resp) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael Kerrisk : "The Linux Programming Interface" CH.44 : sequence number server(Python Implementation) | |
# fifo_seqnum_config | |
import struct | |
CLIENT_FORMAT = 'II' | |
CLIENT_SIZE = struct.calcsize(CLIENT_FORMAT) | |
SERVER_FORMAT = 'I' | |
SERVER_SIZE = struct.calcsize(SERVER_FORMAT) | |
SERVER_FIFO = 'seqnum_sv' | |
CLIENT_FIFO_TEMPLATE = 'seqnum_cl.%ld' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael Kerrisk : "The Linux Programming Interface" CH.44 : sequence number server(Python Implementation) | |
# fifo_seqnum_server | |
import os | |
import struct | |
import config | |
def remove_fifo(signum, frame): | |
os.remove(config.SERVER_FIFO) | |
def main(): | |
seq_num = 0 | |
while True: | |
# Open client FIFO (previously created by client) | |
rd_fd = os.open(config.SERVER_FIFO, os.O_RDONLY) | |
req = os.read(rd_fd, config.CLIENT_SIZE) | |
os.close(rd_fd) | |
(pid, seq_len) = struct.unpack(config.CLIENT_FORMAT, req) | |
print 'server', pid, seq_len | |
# Send response and close FIFO | |
wr_fd = os.open(config.CLIENT_FIFO_TEMPLATE % pid, os.O_WRONLY) | |
os.write(wr_fd, struct.pack(config.SERVER_FORMAT, seq_num)) | |
os.close(wr_fd) | |
seq_num += seq_len | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment