00001 /* 00002 Sys V IPC-object test program 00003 AUP2, Sec. 7.04 (not in book) 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include <sys/msg.h> 00023 #include <sys/sem.h> 00024 00025 #define MQNAME "tempqnamea" 00026 #define MQNAME2 "tempqname2a" 00027 00028 int main(void) 00029 { 00030 int mqid, semid; 00031 key_t key; 00032 int i; 00033 struct msg { 00034 long mtype; /* message type */ 00035 char mtext[100000]; /* message text */ 00036 } msg; 00037 struct msqid_ds msginfo; 00038 00039 (void)close(open(MQNAME, O_WRONLY | O_CREAT, 0)); 00040 ec_neg1( key = ftok(MQNAME, 1) ) 00041 if ((mqid = msgget(key, 0666)) != -1) 00042 ec_neg1( msgctl(mqid, IPC_RMID, &msginfo) ) 00043 ec_neg1( mqid = msgget(key, 0666 | IPC_CREAT | IPC_EXCL) ) 00044 ec_neg1( msgctl(mqid, IPC_STAT, &msginfo) ) 00045 printf("bytes = %ld\n", (long)msginfo.msg_qbytes); 00046 msg.mtype = 1; 00047 for (i = msginfo.msg_qbytes; i > 0; i--) { 00048 if (msgsnd(mqid, &msg, i, IPC_NOWAIT) != -1) { 00049 printf("Size %d worked\n", i); 00050 exit(EXIT_SUCCESS); 00051 } 00052 if (errno != EINVAL) { 00053 printf("Failed on size %d\n", i); 00054 EC_FAIL 00055 } 00056 } 00057 /* 00058 for (i = 1; i < 1000000; i++) 00059 if (msgsnd(mqid, &msg, msginfo.msg_qbytes, IPC_NOWAIT) == -1) { 00060 if (errno == EAGAIN) { 00061 printf("queue full -- i = %d\n", i); 00062 exit(EXIT_SUCCESS); 00063 } 00064 else { 00065 printf("failed on msg #%d\n", i); 00066 EC_FAIL 00067 } 00068 } 00069 */ 00070 (void)close(open(MQNAME2, O_WRONLY | O_CREAT, 0)); 00071 ec_neg1( key = ftok(MQNAME2, 1) ) 00072 ec_neg1( semid = semget(key, 1, 0666 | IPC_CREAT) ) 00073 ec_neg1( semid = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT) ) 00074 00075 system("ipcs"); 00076 exit(EXIT_SUCCESS); 00077 00078 EC_CLEANUP_BGN 00079 exit(EXIT_FAILURE); 00080 EC_CLEANUP_END 00081 }