The Linux Programming Interface: a linux and unix system Programming Handbook



tải về 6.93 Mb.
Chế độ xem pdf
trang793/806
Chuyển đổi dữ liệu08.07.2022
Kích6.93 Mb.
#52607
1   ...   789   790   791   792   793   794   795   796   ...   806
The Linux Programming Interface

svmsg/svmsg_create.c
#include 
#include 
#include 
#include 
#include "tlpi_hdr.h"
#include  /* For portability */
#include 
int msgget(key_t 
key
, int 
msgflg
);
Returns message queue identifier on success, or –1 on error


System V Message Queues
939
static void /* Print usage info, then exit */
usageError(const char *progName, const char *msg)
{
if (msg != NULL)
fprintf(stderr, "%s", msg);
fprintf(stderr, "Usage: %s [-cx] {-f pathname | -k key | -p} "
"[octal-perms]\n", progName);
fprintf(stderr, " -c Use IPC_CREAT flag\n");
fprintf(stderr, " -x Use IPC_EXCL flag\n");
fprintf(stderr, " -f pathname Generate key using ftok()\n");
fprintf(stderr, " -k key Use 'key' as key\n");
fprintf(stderr, " -p Use IPC_PRIVATE key\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int numKeyFlags; /* Counts -f, -k, and -p options */
int flags, msqid, opt;
unsigned int perms;
long lkey;
key_t key;
/* Parse command-line options and arguments */
numKeyFlags = 0;
flags = 0;
while ((opt = getopt(argc, argv, "cf:k:px")) != -1) {
switch (opt) {
case 'c':
flags |= IPC_CREAT;
break;
case 'f': /* -f pathname */
key = ftok(optarg, 1);
if (key == -1)
errExit("ftok");
numKeyFlags++;
break;
case 'k': /* -k key (octal, decimal or hexadecimal) */
if (sscanf(optarg, "%li", &lkey) != 1)
cmdLineErr("-k option requires a numeric argument\n");
key = lkey;
numKeyFlags++;
break;
case 'p':
key = IPC_PRIVATE;
numKeyFlags++;
break;


940
Chapter 46
case 'x':
flags |= IPC_EXCL;
break;
default:
usageError(argv[0], "Bad option\n");
}
}
if (numKeyFlags != 1)
usageError(argv[0], "Exactly one of the options -f, -k, "
"or -p must be supplied\n");
perms = (optind == argc) ? (S_IRUSR | S_IWUSR) :
getInt(argv[optind], GN_BASE_8, "octal-perms");
msqid = msgget(key, flags | perms);
if (msqid == -1)
errExit("msgget");
printf("%d\n", msqid);
exit(EXIT_SUCCESS);
}
––––––––––––––––––––––––––––––––––––––––––––––––––––––

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   789   790   791   792   793   794   795   796   ...   806




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương