1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright (C) Alan W. Irwin, 2016
// Copyright (C) Michael Kerrisk, 2016
//
// This program is free software. You may use, modify, and redistribute it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 or (at your option) any
// later version. This program is distributed without any warranty. See
// the file COPYING.gpl-v3 for details.
// pshm_write.c
// Usage: pshm_write
// Copy stdin into a POSIX shared memory object under unnamed semaphore control.
// To be used in conjunction with pshm_read (which reads from that shared
// memory object under unnamed semaphore control and outputs the results to stdout).
#include <sys/types.h> // Type definitions used by many programs
#include <stdio.h> // Standard I/O functions
#include <stdlib.h> // Prototypes of commonly used library functions,
// plus EXIT_SUCCESS and EXIT_FAILURE constants
#include <unistd.h> // Prototypes for many system calls
#include <errno.h> // Declares errno and defines error constants
#include <string.h> // Commonly used string-handling functions
#include <semaphore.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "pshm.h"
int
main(int argc, char *argv[])
{
int fd;
shmbuf *shmp;
int xfrs, bytes;
if (argc != 1)
{
fprintf(stderr, "%s should have no arguments because it only accepts input from stdin\n", argv[0]);
exit(EXIT_FAILURE);
}
// Create new instance of shared memory with read-write permissions of just the current user.
fd = shm_open(SHM_PATH, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
if (fd == -1)
{
fprintf(stderr, "shm-open\n");
exit(EXIT_FAILURE);
}
// Resize object to hold struct.
if (ftruncate(fd, sizeof(shmbuf)) == -1)
{
fprintf(stderr, "ftruncate\n");
exit(EXIT_FAILURE);
}
shmp = mmap(NULL, sizeof(shmbuf), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shmp == MAP_FAILED)
{
fprintf(stderr, "mmap\n");
exit(EXIT_FAILURE);
}
// 'fd' is no longer needed
if (close(fd) == -1)
{
fprintf(stderr, "close\n");
exit(EXIT_FAILURE);
}
if (sem_init(&shmp->rsem, 1, 0) == -1)
{
fprintf(stderr, "sem_init\n");
exit(EXIT_FAILURE);
}
// We (pshm_write) gets first turn
if (sem_init(&shmp->wsem, 1, 1) == -1)
{
fprintf(stderr, "sem_init\n");
exit(EXIT_FAILURE);
}
for (xfrs = 0, bytes = 0; ; xfrs++, bytes += shmp->cnt) {
// Wait for our turn
if(sem_wait(&shmp->wsem) == -1)
{
fprintf(stderr, "sem_wait\n");
exit(EXIT_FAILURE);
}
shmp->cnt = read(STDIN_FILENO, shmp->buf, BUF_SIZE);
if(shmp->cnt == -1)
{
fprintf(stderr, "read\n");
exit(EXIT_FAILURE);
}
// Give pshm_read a turn
if(sem_post(&shmp->rsem) == -1)
{
fprintf(stderr, "read\n");
exit(EXIT_FAILURE);
}
// EOF on stdin?
if (shmp->cnt == 0)
break;
}
// Wait for pshm_read to finish
if(sem_wait(&shmp->wsem) == -1)
{
fprintf(stderr, "sem_wait\n");
exit(EXIT_FAILURE);
}
// Clean up
if(shm_unlink(SHM_PATH) == -1)
{
fprintf(stderr, "shm_unlink\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
|