This is my lab report on 11/04/96: 1 Example of using shared memory: #include #include #include #include #define SHM_MODE (SHM_R|SHM_W) int main(void) { int shmid; char *shmptr; int *p, *q; pid_t pid; shmid = shmget(IPC_PRIVATE, sizeof(int), SHM_MODE); pid = fork(); if ( pid == 0) { // CHILD PROCESS shmptr = (char *)shmat (shmid, 0 , 0); p = (int *)shmptr; // READ AN INTEGER TO p, p IS IN SHARED MEMORY. printf("Enter an integer:"); scanf("%d", p); shmctl(shmid, IPC_RMID, 0); exit(0); } else // PARENT PROCESS { shmptr = (char *)shmat (shmid, 0, 0); //LET q POINTS TO THE SAME ADDRESS. q = (int *)shmptr; wait(); printf("The number was %d\n", *q); shmctl ( shmid, IPC_RMID, 0); exit(0); } } Non blocking terminal IO: We need to read from the terminal in a non blocking way. Example: #include #include #define LINESIZE 128 void menu (void); main () { char line[LINESIZE]; int size; int fd; menu(); fd = open("/dev/tty, O_RDONLY | O_NONBLOCK, "r"); for ( ; ; ) { sleep(1); size = read(fd, line, 1); if (size > 0) if (line[0] == '1') // INPUT IS '1' { while ((size = read(fd, line, 1)) >0) ; printf("Your choice is 1.\n"); menu(); } else { printf("Your choice is 2.\n"); break; } else printf("Spinning\n"); } } void menu(void) { printf("1. Print Statistics.\n"); printf("2. Terminate.\n"); printf("Enter choice [1|2] > \n"); }