int main(){ int isUserP, isMMU; //fork off the MMU //fork off the user process puts("Enter '1' for FCFS, '2' for LRU, and '3' for my own algorithm."); int type; fscanf(stdin, "%d", &type); //create shared memory that contains all the data that user might request //create a small block of shared memory for requests and responses int sharedmem = createShm(sizeof(pageStruct)*21); printf("sharedmem: %d\n", sharedmem); if (isMMU = fork()) { sleep(1); if (isUserP = fork()) { //main process that knows UserP's pid int res; waitpid(isUserP,&res, 0); puts("\ndone."); kill(isMMU, SIGKILL); //kill MMU once user is done running destroyShm(); }else { runUserP(isMMU); } }else { runMMU(type); } }
int main(void) { char line[80]; int choice = ' '; char sem_name1[] = "e"; char sem_name2[] = "s"; char sem_name3[] = "n"; char shm_name[] = "queue"; unsigned long b[bufferSize]; int i; for(i = 0; i < bufferSize; i++) b[i] = 0; //*shm_addr = 0; do { printf("Press i to initialize\n"); choice = getchar(); } while (choice != 'i'); createNewSemaphore(sem_name1); createNewSemaphore(sem_name2); createNewSemaphore(sem_name3); shm_addr = createShm(shm_name, sizeof(b)); shm_addr = openShm(shm_name); printf("*********************\n"); printf("*****Initialized*****\n"); printf("*********************\n"); do { printf("Press q to close and unlink all\n"); choice = getchar(); } while (choice != 'q'); closeSemaphore(sem_name1); closeSemaphore(sem_name2); closeSemaphore(sem_name3); CloseAndUnlink(shm_name); printf("*********************\n"); printf("*******Unlinked******\n"); printf("*********************\n"); return (0); }
int main(int argc, char **argv) { int permission = 0644; int opt; size_t size = 0; int nsems = 0; int doShm = 0, doMsg = 0, doSem = 0; progname = program_invocation_short_name; if (!progname) progname = "ipcmk"; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); while((opt = getopt(argc, argv, "hM:QS:p:")) != -1) { switch(opt) { case 'M': size = atoi(optarg); doShm = 1; break; case 'Q': doMsg = 1; break; case 'S': nsems = atoi(optarg); doSem = 1; break; case 'p': permission = strtoul(optarg, NULL, 8); break; case 'h': usage(EXIT_SUCCESS); break; default: doShm = doMsg = doSem = 0; break; } } if(!doShm && !doMsg && !doSem) usage(EXIT_FAILURE); if (doShm) { int shmid; if (-1 == (shmid = createShm(size, permission))) err(EXIT_FAILURE, _("create share memory failed")); else printf(_("Shared memory id: %d\n"), shmid); } if (doMsg) { int msgid; if (-1 == (msgid = createMsg(permission))) err(EXIT_FAILURE, _("create message queue failed")); else printf(_("Message queue id: %d\n"), msgid); } if (doSem) { int semid; if (-1 == (semid = createSem(nsems, permission))) err(EXIT_FAILURE, _("create semaphore failed")); else printf(_("Semaphore id: %d\n"), semid); } return EXIT_SUCCESS; }