int main(int argc, char **argv) { if (argc != 2) { printf("Usage: packet_sender <num of messages to receive>\n"); exit(-1); } int k = atoi(argv[1]); /* num of messages you will get from the sender */ int i; char *msg; mm_t mm2; mm = mm2; mm_init(&mm, 10, MSGSIZE); message.num_packets = 0; pid_queue_msg pqm; pqm.pid = getpid(); pqm.mtype = QUEUE_MSG_TYPE; msqid = msgget(key, 0666|IPC_CREAT); printf("mid: %d\n", msqid); msgsnd(msqid, &pqm, sizeof(int), 0); printf("Sent pid\n"); printf("pid = %d\n", pqm.pid); /* set up SIGIO handler to readhow many: 6 GOT IT: message=aaabbbcccdddeeeff incoming packets from the queue. Check packet_handler() */ struct sigaction act; act.sa_sigaction = (void *)packet_handler; act.sa_flags = SA_SIGINFO; sigemptyset(&act.sa_mask); sigaction(SIGIO, &act, NULL); for (i = 1; i <= k; i++) { packet_t pkt; while (pkt_cnt < pkt_total) { pause(); /* block until next packet */ } msg = assemble_message(); if (msg == NULL) { perror("Failed to assemble message"); } else { fprintf(stderr, "GOT IT: message=%s\n", msg); free(msg); } } // deallocate memory manager mm_release(&mm); // remove the queue once done msgctl(msqid, IPC_RMID, NULL); return EXIT_SUCCESS; }
/* * Turn us into a lazy TLB process if we * aren't already.. */ static inline void __exit_mm(struct task_struct * tsk) { struct mm_struct * mm = tsk->mm; mm_release(); if (mm) { atomic_inc(&mm->mm_count); BUG_ON(mm != tsk->active_mm); /* more a memory barrier than a real lock */ task_lock(tsk); tsk->mm = NULL; task_unlock(tsk); enter_lazy_tlb(mm, current, smp_processor_id()); mmput(mm); } }
static inline void __exit_mm(struct task_struct * tsk) { struct mm_struct * mm = tsk->mm; /* Set us up to use the kernel mm state */ if (mm != &init_mm) { flush_cache_mm(mm); flush_tlb_mm(mm); destroy_context(mm); tsk->mm = &init_mm; tsk->swappable = 0; SET_PAGE_DIR(tsk, swapper_pg_dir); mm_release(); mmput(mm); } }
void timer_mm() { struct timeval time_start, time_end; int j,i; j = gettimeofday (&time_start, (void *)NULL); mm_t MM; if(mm_init (&MM, 10000, 64) == -1){ perror("failed to initialize mm"); } for (i = 0; i < 10000; i++){ mm_get (&MM); } for (i = 0; i < 10000; i++){ mm_put (&MM, (void*) (MM.blocks + i * MM.size)); } mm_release(&MM); j = gettimeofday (&time_end, (void *)NULL); fprintf (stderr, "Time taken for MM = %f msec\n", comp_time (time_start, time_end)/1000.0); }
/* Main driver that tests the * clock time for our dynamic * memory allocator */ int main (int argc, char **argv) { int i, j; struct timeval times, timee; mm_t MM; void *chunk = NULL; if (mm_init(&MM,NUM_CHUNKS, CHUNK_SIZE) < 0) fprintf(stderr, "Error in mm_init\n"); j = gettimeofday (×, (void *)NULL); for (i=0; i< ITERS; i++) { chunk = mm_get(&MM); mm_put(&MM,chunk); } j = gettimeofday (&timee, (void *)NULL); mm_release(&MM); /* release stuff on heap*/ fprintf (stderr, "MM time took %f msec\n",comp_time (times, timee)/1000.0); return 0; }
int main() { mm_t mm; if(mm_init(&mm, 1000000, 64) == 0) { printf("success\n"); } gettimeofday (&time_s, NULL); mm_t *mmArray[1000000]; int i; for (i = 0; i < 1000000; i++){ if ((mmArray[i] = mm_get(&mm)) == NULL){ printf("Failed to allocate i = %d\n", i); } } while (i > 0){ i--; mm_put(&mm, mmArray[i]); } gettimeofday(&time_e, NULL); mm_release(&mm); fprintf(stderr, "main_mm duration = %f msec\n", comp_time(time_s, time_e) / 1000.0); return 0; }
int main(int argc, char **argv) { /* set up all the sigsets here*/ // Weichao's Question, do we need to put all signals within it? sigset_t newset; if ((sigemptyset(&newset) == -1) || (sigfillset(&newset) == -1)) { perror("Failed to set up signal set to include all signals.\n"); exit(1); } else { if (sigprocmask(SIG_UNBLOCK, &newset, NULL) == -1) { perror("Failed to block the incoming signals"); exit(1); } } /* set up alarm handler -- mask all signals within it */ // save the old signal action in case we messed up, comment out later struct sigaction old_action; if (sigaction(SIGALRM, NULL, &old_action) == -1) { perror("Unable to save the old signal action handler for SIGALRM."); exit(1); } // set up the new action for SIGALRM struct sigaction new_action; new_action.sa_handler = packet_handler; new_action.sa_flags = 0; new_action.sa_mask=newset; // should be careful here. This is not in notes? if (sigaction(SIGALRM, &new_action, NULL) == -1) { perror("Fail to set the new action mask for SIGALRM"); exit(1); } /* turn on alarm timer ... use INTERVAL and INTERVAL_USEC for sec and usec values */ struct itimerval interval; interval.it_value.tv_sec = TIMER_TV_SEC; interval.it_value.tv_usec = TIMER_TV_USEC; interval.it_interval.tv_sec = TIMER_TV_SEC; interval.it_interval.tv_usec = TIMER_TV_USEC; if (setitimer(ITIMER_REAL, &interval, NULL) == -1) { fprintf(stderr, "Fail to set real-time timber to sec=%d, micro sec=%d: %s\n", TIMER_TV_SEC, TIMER_TV_USEC, strerror(errno)); exit(1); } else { fprintf(stderr, "Real-time timber set to sec=%d, micro sec=%d.\n", TIMER_TV_SEC, TIMER_TV_USEC); } // Real thing starts here fprintf(stderr, "Starting packet_public \n"); int j; message.num_packets = 0; mm_init(&MM, 200); for (j = 1; j <= NumMessages; j++) { while (pkt_cnt < pkt_total) // fprintf(stderr,"Starting while \n"); pause(); // reset these for next message pkt_total = 1; pkt_cnt = 0; message.num_packets = 0; cnt_msg++; // anything else? } /* Deallocate memory manager */ mm_release(&MM); fprintf(stderr, "Memory manager released. End of packet_public \n"); }
int main() { typedef struct { int val; } chunk_t; mm_t MM; mm_t MM1; mm_t MM2; int i; int j; int found; void* address; unsigned long int* addresses = (unsigned long int*) malloc(NUM_CHUNKS * sizeof(unsigned long int)); unsigned long int* free_addresses = (unsigned long int*) malloc(NUM_CHUNKS * sizeof(unsigned long int)); srand(time(NULL)); /* * Initialize memory */ if (mm_init(&MM, NUM_CHUNKS, CHUNK_SIZE) == -1) { printf("Failed to initialize memory!\n"); exit(-1); } /* * Allocate all chunks */ for (i = 0; i < NUM_CHUNKS; i++) { if (!(address = mm_get(&MM))) { printf("Failed to allocate a chunk %d while chunks available!\n", i); return 1; } else { addresses[i] = (unsigned long int) address; } } /* * Sort Address in ascending order */ unsigned long int temp; for (i = 0; i < NUM_CHUNKS; i++) { for (j = i; j < NUM_CHUNKS; j++) { if (addresses[i] > addresses[j]) { temp = addresses[i]; addresses[i] = addresses[j]; addresses[j] = temp; } } } /* * Addresses are distinct */ for (i = 1; i < NUM_CHUNKS; i++) { if (addresses[i] == addresses[i-1]) { printf("Not all chunk addresses are Distinct!\n"); break; } } /* * Memory is contiguous * Chunk size OK */ for (i = 1; i < NUM_CHUNKS; i++) { if (addresses[i] != addresses[i-1] + CHUNK_SIZE) { printf("Contiguity and Chunk Size NOT OK!\n"); break; } } /* * Does not allocate more than NUM_CHUNKS */ if (mm_get(&MM) != NULL) { printf("Allocating more than %d\n possible chunks!", NUM_CHUNKS); } /* * Deallocate a random number of chunks * Reallocate only these */ for (i = 0; i < NUM_CHUNKS; i += (rand() % (NUM_CHUNKS/5)) + 1) { mm_put(&MM, (void*) addresses[i]); free_addresses[i] = addresses[i]; } while (address == mm_get(&MM)) { found = 0; for (i = 0; i < NUM_CHUNKS; i++) { if (free_addresses[i] == (unsigned long int) address) { found = 1; break; } } if (found == 0) { printf("A chunk in use (or unmanaged) is being Allocated!\n"); break; } } mm_release(&MM); free(free_addresses); free(addresses); /* * What about allocation and * deallocation of just a few chunks? */ addresses = (unsigned long int*) malloc(10 * sizeof(unsigned long int)); if (mm_init(&MM1, 10, 20) == -1) { printf("Failed to initialize memory!\n"); exit(-1); } for (i = 0; i < 4; i++) addresses[i] = (unsigned long int) mm_get(&MM1); mm_put(&MM1, (void*) addresses[1]); addresses[1] = 0; while (address == mm_get(&MM1)) { found = 0; for (i = 0; i < 4; i++) { if (addresses[i] == (unsigned long int) address) { printf("A chunk in use is being reallocated!\n"); break; } } } mm_release(&MM1); free(addresses); /* * Finally, Can we use the chunks? * Expect no segmentation fault. */ if (mm_init(&MM2, NUM_CHUNKS, sizeof(chunk_t)) == -1) { printf("Failed to initialize memory!\n"); exit(-1); } chunk_t *my_chunk; for (i = 0; i < NUM_CHUNKS; i++) { chunk_t new_chunk; new_chunk.val = i; my_chunk = (chunk_t*) mm_get(&MM2); memcpy(my_chunk, &new_chunk, sizeof(chunk_t)); } mm_release(&MM2); return 0; }