Appendixes


These appendixes contain a number of code examples demonstrating how to convert code to Win32.

Appendix 9.1: Shared Memory

The following is an example of typical UNIX code that uses shared memory.

UNIX Example: Shared Memory

Consumer
 /* This program is a consumer. The shared memory segment is    created with a call to shmget, with the IPC_CREAT bit specified. */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include "shm_com.h" int main() {     void *shared_memory_loc = (void *)0;     struct shared_struct *shared_stuff;     int shmid;     shmid = shmget((key_t)1111, sizeof(struct shared_struct), 0666  IPC_CREAT);     if (shmid == -1) {         fprintf(stderr, "shmget function failed\n");         exit(EXIT_FAILURE);     } /* Make the shared memory accessible to the program. */     shared_memory_loc = shmat(shmid, (void *)0, 0);     if (shared_memory_loc == (void *)-1) {         fprintf(stderr, "shmat function failed\n");         exit(EXIT_FAILURE);     }     printf("Memory attached at %X\n", (int)shared_memory_loc); /* Assign the shared_memory_loc segment to shared_stuff.    Echo any text in "some_text".    Continues until end is found in "some_input" (1 in stored by Provider). */     shared_stuff = (struct shared_struct *)shared_memory_loc;     shared_stuff->some_input = 0;     while(1) {         if (shared_stuff->some_input) {             printf("You wrote: %s", shared_stuff->some_text);             sleep(1); /* the Provider is waiting for this process */             shared_stuff->some_input = 0;             if (strncmp(shared_stuff->some_text, "done", 4) == 0) {                 break;             }         }     } /* Detach and Delete shared memory */     if (shmdt(shared_memory_loc) == -1) {         fprintf(stderr, "shmdt function failed\n");         exit(EXIT_FAILURE);     }     if (shmctl(shmid, IPC_RMID, 0) == -1) {         fprintf(stderr, "shmctl(IPC_RMID) function failed\n");         exit(EXIT_FAILURE);     }     exit(EXIT_SUCCESS); } 
Provider
 /* This program is a provider of input text for the consumer. */ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include "shm_com.h" int main() {     void *shared_memory_loc = (void *)0;     struct shared_struct *shared_stuff;     char buffer[BUFSIZ];     int shmid;     shmid = shmget((key_t)1111, sizeof(struct shared_struct), 0666  IPC_CREAT);     if (shmid == -1) {         fprintf(stderr, "shmget function failed\n");         exit(EXIT_FAILURE);     }     shared_memory_loc = shmat(shmid, (void *)0, 0);     if (shared_memory_loc == (void *)-1) {         fprintf(stderr, "shmat function failed\n");         exit(EXIT_FAILURE);     }     printf("Memory attached at %X\n", (int)shared_memory_loc);     shared_stuff = (struct shared_struct *)shared_memory_loc;     while(1) {         while(shared_stuff->some_input == 1) {             printf("waiting for Consumer...\n");             sleep(1);         }         printf("Enter some text: ");         fgets(buffer, BUFSIZ, stdin);         strncpy(shared_stuff->some_text, buffer, TEXT_SZ);         shared_stuff->some_input = 1;         if (strncmp(buffer, "done", 4) == 0) {               break;         }     }     if (shmdt(shared_memory_loc) == -1) {         fprintf(stderr, "shmdt function failed\n");         exit(EXIT_FAILURE);     }     exit(EXIT_SUCCESS); } 
Header
 /* A common header file to describe the memory being shared. */ #define TEXT_SZ 256 struct shared_struct {     int some_input;     char some_text[TEXT_SZ]; }; 

Win32 Example: Shared Memory

Provider
 /* This program is a provider of input text for the consumer. */ #include <windows.h> #include <stdio.h> 3#include <stdlib.h> #include <string.h> #include "shm_com.h" void main() {     void *shared_memory_loc = NULL;     struct shared_struct *shared_stuff;     char buffer[BUFSIZ];     HANDLE hMapObject = NULL;  // handle to file mapping     hMapObject = CreateFileMapping(INVALID_HANDLE_VALUE, // use paging file         NULL,                 // no security attributes         PAGE_READWRITE,       // read/write access         0,                    // size: high 32-bits         sizeof(struct shared_struct),            // size: low 32-bits         "MMF1");     // name of map object      if (hMapObject != NULL) {           // Get a pointer to the file-mapped shared memory.           shared_memory_loc = MapViewOfFile(hMapObject,     // object to map view of                     FILE_MAP_WRITE, // read/write access                     0,              // high offset:  map from                     0,              // low offset:   beginning                     0);             // default: map entire file           if (shared_memory_loc == NULL) {                     CloseHandle(hMapObject);                      fprintf(stderr, "MapViewOfFile function failed\n");                     exit(EXIT_FAILURE);           } else                     memset(shared_memory_loc, 


UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net