©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c7/shmex.c

Go to the documentation of this file.
00001 /*
00002     Example using shared memory (bad example)
00003     AUP2, Sec. 7.13.1
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/shm.h>
00023 /*[pgm]*/
00024 static int *getaddr(void)
00025 {
00026     key_t key;
00027     int shmid, *p;
00028 
00029     (void)close(open("shmseg", O_WRONLY | O_CREAT, 0));
00030     ec_neg1( key = ftok("shmseg", 1) )
00031     ec_neg1( shmid = shmget(key, sizeof(int), IPC_CREAT | PERM_FILE) )
00032     ec_neg1( p = shmat(shmid, NULL, 0) )
00033     return p;
00034 
00035 EC_CLEANUP_BGN
00036     return NULL;
00037 EC_CLEANUP_END
00038 }
00039 
00040 int main(void)
00041 {
00042     pid_t pid;
00043 
00044     if ((pid = fork()) == 0) {
00045         int *p, prev = 0;
00046 
00047         ec_null( p = getaddr() )
00048         while (*p != 99)
00049             if (prev != *p) {
00050                 printf("child saw %d\n", *p);
00051                 prev = *p;
00052             }
00053         printf("child is done\n");
00054     }
00055     else {
00056         int *p;
00057 
00058         ec_null( p = getaddr() )
00059         for (*p = 1; *p < 4; (*p)++)
00060             sleep(1);
00061         *p = 99;
00062     }
00063     exit(EXIT_SUCCESS);
00064 
00065 EC_CLEANUP_BGN
00066     exit(EXIT_FAILURE);
00067 EC_CLEANUP_END
00068 }
00069 

Generated on Fri Apr 23 10:56:59 2004 for AUP2 Example Source by doxygen 1.3.1