/* ----------------------------------------------------------------- */ /* PROGRAM shm-01.c */ /* The main function serves as the server who requests a shared */ /* memory of four integers, attaches the shared memory to its own */ /* address space, fills it with the numbers from command line, forks */ /* a client, waits until its client completes, detaches the shared */ /* memory, removes the shared memory, and finally exits. */ /* The client process just displays the content of the shared */ /* memory. Note that since the client inherits the address space of */ /* the server in which shared memory has already been attached, the */ /* client does not have to go through all four steps of using shared */ /* memory segments. */ /* ----------------------------------------------------------------- */ #include #include #include #include #include void ClientProcess(int []); void main(int argc, char *argv[]) { int ShmID; int *ShmPTR; pid_t pid; int status; if (argc != 5) { printf("Use: %s #1 #2 #3 #4\n", argv[0]); exit(1); } ShmID = shmget(IPC_PRIVATE, 4*sizeof(int), IPC_CREAT | 0666); if (ShmID < 0) { printf("*** shmget error (server) ***\n"); exit(1); } printf("Server has received a shared memory of four integers...\n"); ShmPTR = (int *) shmat(ShmID, NULL, 0); if ((int) ShmPTR == -1) { printf("*** shmat error (server) ***\n"); exit(1); } printf("Server has attached the shared memory...\n"); ShmPTR[0] = atoi(argv[1]); ShmPTR[1] = atoi(argv[2]); ShmPTR[2] = atoi(argv[3]); ShmPTR[3] = atoi(argv[4]); printf("Server has filled %d %d %d %d in shared memory...\n", ShmPTR[0], ShmPTR[1], ShmPTR[2], ShmPTR[3]); printf("Server is about to fork a child process...\n"); pid = fork(); if (pid < 0) { printf("*** fork error (server) ***\n"); exit(1); } else if (pid == 0) { ClientProcess(ShmPTR); exit(0); } wait(&status); printf("Server has detected the completion of its child...\n"); shmdt((void *) ShmPTR); printf("Server has detached its shared memory...\n"); shmctl(ShmID, IPC_RMID, NULL); printf("Server has removed its shared memory...\n"); printf("Server exits...\n"); exit(0); } void ClientProcess(int SharedMem[]) { printf(" Client process started\n"); printf(" Client found %d %d %d %d in shared memory\n", SharedMem[0], SharedMem[1], SharedMem[2], SharedMem[3]); printf(" Client is about to exit\n"); }