From e19c8aa5c53e59defd2f238857f328750bdaa060 Mon Sep 17 00:00:00 2001 From: ada-dmitry Date: Wed, 4 Oct 2023 10:55:29 +0300 Subject: [PATCH] shmat --- TMP/Practice/04.10/output/mykey | 0 TMP/Practice/04.10/output/mykey2 | 0 TMP/Practice/04.10/shmat/svshm_string.h | 60 +++++++++++++++++++ TMP/Practice/04.10/shmat/svshm_string_read.c | 60 +++++++++++++++++++ TMP/Practice/04.10/shmat/svshm_string_write.c | 60 +++++++++++++++++++ 5 files changed, 180 insertions(+) create mode 100644 TMP/Practice/04.10/output/mykey create mode 100644 TMP/Practice/04.10/output/mykey2 create mode 100644 TMP/Practice/04.10/shmat/svshm_string.h create mode 100644 TMP/Practice/04.10/shmat/svshm_string_read.c create mode 100644 TMP/Practice/04.10/shmat/svshm_string_write.c diff --git a/TMP/Practice/04.10/output/mykey b/TMP/Practice/04.10/output/mykey new file mode 100644 index 0000000..e69de29 diff --git a/TMP/Practice/04.10/output/mykey2 b/TMP/Practice/04.10/output/mykey2 new file mode 100644 index 0000000..e69de29 diff --git a/TMP/Practice/04.10/shmat/svshm_string.h b/TMP/Practice/04.10/shmat/svshm_string.h new file mode 100644 index 0000000..9eabd8a --- /dev/null +++ b/TMP/Practice/04.10/shmat/svshm_string.h @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +static void +usage(const char *pname) +{ + fprintf(stderr, "Usage: %s [-cx] pathname proj-id num-sems\n", + pname); + fprintf(stderr, " -c Use IPC_CREAT flag\n"); + fprintf(stderr, " -x Use IPC_EXCL flag\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char *argv[]) +{ + int semid, nsems, flags, opt; + key_t key; + + flags = 0; + while ((opt = getopt(argc, argv, "cx")) != -1) + { + switch (opt) + { + case 'c': + flags |= IPC_CREAT; + break; + case 'x': + flags |= IPC_EXCL; + break; + default: + usage(argv[0]); + } + } + + if (argc != optind + 3) + usage(argv[0]); + + key = ftok(argv[optind], argv[optind + 1][0]); + if (key == -1) + { + perror("ftok"); + exit(EXIT_FAILURE); + } + + nsems = atoi(argv[optind + 2]); + + semid = semget(key, nsems, flags | 0600); + if (semid == -1) + { + perror("semget"); + exit(EXIT_FAILURE); + } + + printf("ID = %d\n", semid); + + exit(EXIT_SUCCESS); +} diff --git a/TMP/Practice/04.10/shmat/svshm_string_read.c b/TMP/Practice/04.10/shmat/svshm_string_read.c new file mode 100644 index 0000000..9eabd8a --- /dev/null +++ b/TMP/Practice/04.10/shmat/svshm_string_read.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +static void +usage(const char *pname) +{ + fprintf(stderr, "Usage: %s [-cx] pathname proj-id num-sems\n", + pname); + fprintf(stderr, " -c Use IPC_CREAT flag\n"); + fprintf(stderr, " -x Use IPC_EXCL flag\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char *argv[]) +{ + int semid, nsems, flags, opt; + key_t key; + + flags = 0; + while ((opt = getopt(argc, argv, "cx")) != -1) + { + switch (opt) + { + case 'c': + flags |= IPC_CREAT; + break; + case 'x': + flags |= IPC_EXCL; + break; + default: + usage(argv[0]); + } + } + + if (argc != optind + 3) + usage(argv[0]); + + key = ftok(argv[optind], argv[optind + 1][0]); + if (key == -1) + { + perror("ftok"); + exit(EXIT_FAILURE); + } + + nsems = atoi(argv[optind + 2]); + + semid = semget(key, nsems, flags | 0600); + if (semid == -1) + { + perror("semget"); + exit(EXIT_FAILURE); + } + + printf("ID = %d\n", semid); + + exit(EXIT_SUCCESS); +} diff --git a/TMP/Practice/04.10/shmat/svshm_string_write.c b/TMP/Practice/04.10/shmat/svshm_string_write.c new file mode 100644 index 0000000..9eabd8a --- /dev/null +++ b/TMP/Practice/04.10/shmat/svshm_string_write.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include + +static void +usage(const char *pname) +{ + fprintf(stderr, "Usage: %s [-cx] pathname proj-id num-sems\n", + pname); + fprintf(stderr, " -c Use IPC_CREAT flag\n"); + fprintf(stderr, " -x Use IPC_EXCL flag\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char *argv[]) +{ + int semid, nsems, flags, opt; + key_t key; + + flags = 0; + while ((opt = getopt(argc, argv, "cx")) != -1) + { + switch (opt) + { + case 'c': + flags |= IPC_CREAT; + break; + case 'x': + flags |= IPC_EXCL; + break; + default: + usage(argv[0]); + } + } + + if (argc != optind + 3) + usage(argv[0]); + + key = ftok(argv[optind], argv[optind + 1][0]); + if (key == -1) + { + perror("ftok"); + exit(EXIT_FAILURE); + } + + nsems = atoi(argv[optind + 2]); + + semid = semget(key, nsems, flags | 0600); + if (semid == -1) + { + perror("semget"); + exit(EXIT_FAILURE); + } + + printf("ID = %d\n", semid); + + exit(EXIT_SUCCESS); +}