int transmit(int* arg) { char buffer[BUFFER_SIZE]; int length; int i; minithread_t receiver1; minithread_t receiver2; miniport_t write_port1; miniport_t write_port2; network_address_t my_address; network_get_my_address(&my_address); port1 = miniport_create_unbound(0); port2 = miniport_create_unbound(1); write_port1 = miniport_create_bound(my_address, 0); write_port2 = miniport_create_bound(my_address, 1); receiver1 = minithread_fork(receive1, NULL); receiver2 = minithread_fork(receive2, NULL); for (i=0; i<MAX_COUNT; i++) { printf("Sending packet %d to receiver 1.\n", i+1); sprintf(buffer, "Count for receiver 1 is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port1, write_port1, buffer, length); minithread_yield(); printf("Sending packet %d to receiver 2.\n", i+1); sprintf(buffer, "Count for receiver 2 is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port2, write_port2, buffer, length); } return 0; }
int transmit1(int* arg) { char buffer[BUFFER_SIZE]; int length; int i; minithread_t transmitter2; minithread_t receiver; miniport_t write_port; network_get_my_address(&my_address); port = miniport_create_unbound(0); write_port = miniport_create_bound(my_address, 0); transmitter2 = minithread_fork(transmit2, NULL); receiver = minithread_fork(receive, NULL); for (i=0; i<MAX_COUNT; i++) { printf("Sending packet %d from sender 1.\n", i+1); sprintf(buffer, "Count from sender 1 is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port, write_port, buffer, length); minithread_yield(); } return 0; }
int run(int *arg) { minithread_fork(thread0, NULL); minithread_fork(thread1, NULL); minithread_fork(thread2, NULL); minithread_fork(thread3, NULL); return 0; }
int fork_test(int* arg) { port = miniport_create_unbound(100); for(int i = 0; i < PROC_COUNT; i++) { minithread_fork(receive, NULL); minithread_fork(send, NULL); } return -1; }
int main1(int* arg){ int counter; for (counter=0;counter<num_employees;counter++) { minithread_fork(unpack_phone,&counter); } for (counter=0;counter<num_customers;counter++) { minithread_fork(receive_phone,&counter); } printf("%i employees busy unpacking\n",num_employees); printf("%i customers eager for their phones!\n",num_customers); return 0; }
static int start(int* arg) { int i; for (i = 0; i < employee_num; i++) { minithread_fork(employee, NULL); } for (i = 0; i < customer_num; i++) { minithread_fork(customer, NULL); } return 0; }
static int receiver(int* arg) { int len; char buffer[BUFFER_SIZE]; network_address_t dest; if (NULL != remote_name) { network_translate_hostname(remote_name, dest); connection = minisocket_client_create(dest, port_num, &error); } else { connection = minisocket_server_create(port_num, &error); } if (NULL == connection) { printf("Connection error.\n"); return -1; } else { minithread_fork(sender, NULL); printf("Connection successful. Type and enter to send messages.\n"); } while (1) { buffer[BUFFER_SIZE] = '\0'; len = minisocket_receive(connection, buffer, BUFFER_SIZE - 1, &error); buffer[len] = '\0'; printf("[Received message]: %s", buffer); } return 0; }
int testthread(int* arg){ int i; int id[100]; temp = 0; timeout = 2000; /* in millisceonds */ for(N = 2; N<=50; N*=5){ printf("\n\n* %ld threads Test...", N); fflush(stdout); timeout *= 3; strt = mytime(); id[0]=0; for(i = 1; i < N; i++){ id[i]=i; minithread_fork(thread1, &id[i]); } thread1(&id[0]); if(temp >= N*3) printf(" Preemption working"); else printf(" Preemption failed"); fflush(stdout); time_wait(4000); /* in milliseconds */ } return 0; }
/* * Initialize the system to run the first minithread at * mainproc(mainarg). This procedure should be called from your * main program with the callback procedure and argument specified * as arguments. */ void minithread_system_initialize(proc_t mainproc, arg_t mainarg) { runnable_queue = multilevel_queue_new(MAX_LEVELS); stopped_queue = queue_new(); scheduler_thread = scheduler_thread_create(); assert(scheduler_thread); running_thread = scheduler_thread; int res = network_initialize((network_handler_t) network_handler); assert(res == 0); alarm_system_initialize(); minimsg_initialize(); minisocket_initialize(); reaper_thread = minithread_create(clean_stopped_threads, NULL); minithread_fork(mainproc, mainarg); interrupt_level_t prev_level = set_interrupt_level(ENABLED); minithread_clock_init(PERIOD * MILLISECOND, clock_handler); while (1) { if (!multilevel_queue_is_empty(runnable_queue)) { minithread_yield(); } } set_interrupt_level(prev_level); multilevel_queue_free(runnable_queue); queue_free(stopped_queue); }
int thread2(int* arg) { minithread_t thread = minithread_fork(thread3, NULL); printf("Thread 2.\n"); minithread_yield(); return 0; }
int transmit(int* arg) { char buffer[BUFFER_SIZE]; int length; int i; network_address_t addr; miniport_t port; miniport_t dest; AbortOnCondition(network_translate_hostname(hostname, addr) < 0, "Could not resolve hostname, exiting."); port = miniport_create_unbound(0); dest = miniport_create_bound(addr, 1); minithread_fork(transmit1, NULL); for (i=0; i<MAX_COUNT/2; i++) { printf("Sending packet %d.\n", i+1); sprintf(buffer, "Count is %d.\n", i+1); length = strlen(buffer) + 1; minimsg_send(port, dest, buffer, length); minithread_yield(); } return 0; }
int mainproc(int* arg) { // Creates 10 employee unpacking threads for(int i=0; i<10; i++){ minithread_t employee = minithread_fork(employee_unpack, NULL); } // Creates 10 customer threads. for(int i=0; i<10; i++){ minithread_t customer = minithread_fork(customer_getphone, NULL); } // Once the threads have been created this thread will be used as the idle thread. while(1){ minithread_yield(); } return 0; }
/* Performs any initialization of the miniroute layer, if required. */ void miniroute_initialize() { network_get_my_address(hostaddr); discovery_alarm = -1; intrpt_buffer = queue_new(); intrpt_sig = semaphore_create(); discovery_mutex = semaphore_create(); discovery_sig = semaphore_create(); route_cache = miniroute_cache_new(65536, SIZE_OF_ROUTE_CACHE, MINIROUTE_CACHED_ROUTE_EXPIRE); disc_cache = miniroute_cache_new(65536, SIZE_OF_ROUTE_CACHE, MINIROUTE_CACHED_ROUTE_EXPIRE * 10); if (NULL == intrpt_buffer || NULL == intrpt_sig || NULL == route_cache || NULL == discovery_mutex || NULL == discovery_sig) { queue_free(intrpt_buffer); semaphore_destroy(intrpt_sig); semaphore_destroy(discovery_mutex); semaphore_destroy(discovery_sig); miniroute_cache_destroy(route_cache); return; } semaphore_initialize(intrpt_sig, 0); semaphore_initialize(discovery_mutex, 1); semaphore_initialize(discovery_sig, 0); network_get_my_address(hostaddr); minithread_fork(miniroute_control, NULL); }
int spawn_client(int* arg) { int i; for (i = 0; i < MAX_CONN; i++) { minithread_fork(client, &ports[i]); } return 0; }
int spawn_server(int* arg) { int i; for (i = 0; i < MAX_CONN; i++) { minithread_fork(server, &ports[i]); } return 0; }
int B(int* arg) { int i; char name[257]; minithread_fork(B, NULL); for (i = 0; i < 10; i++) { sprintf(name, "%d", i); if (minifile_mkdir(name) == -1) { printf("B: mkdir failed on %dth entry. abort!\n", i); return -1; } if (minifile_cd(name) == -1) { printf("B: cd failed on %dth entry. abort!\n", i); return -1; } } for (i = 9; i >= 0; i--) { sprintf(name, "%d", i); if (minifile_cd("..") == -1) { printf("B: cd failed on %dth entry. abort!\n", i); return -1; } if (minifile_rmdir(name) == -1) { printf("B: rmdir failed on %dth entry. abort!\n", i); return -1; } } printf("B: exiting\n"); return 0; }
int initialize_threads(int *arg) { int i; // start the employees working for (i = 0; i < N_EMPLOYEES; i++) { minithread_fork(unpack, NULL); } // start the customers purchasing for (i = 0; i< M_CUSTOMERS; i++) { int *customer_id = (int *)malloc(sizeof(int)); *customer_id = i; minithread_fork(purchase, customer_id); } return 0; }
int clientMethod(int* arg) { char responseBuffer1[10]; char responseBuffer2[10]; minithread_fork(serverMethod, NULL); network_address_t my_address; network_get_my_address(my_address); minisocket_error error; //create client client = minisocket_client_create(my_address, PORT_NUM, &error); assert(client != NULL); assert(error == SOCKET_NOERROR); //receive first 10 bytes int response = minisocket_receive(client, responseBuffer1, 10, &error); assert(response == 10); assert(strcmp(responseBuffer1, "aaaaaaaaaa") == 0); //receive second 10 bytes response = minisocket_receive(client, responseBuffer2, 10, &error); assert(response == 10); assert(strcmp(responseBuffer2, "aaaaaaaaaa") == 0); minithread_sleep_with_timeout(15000); //Make sure that the port does not exist response = minisocket_send(client, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); error = SOCKET_NOERROR; fprintf(stderr, "Test Part 1 Passed \n"); //recreate client client = minisocket_client_create(my_address, PORT_NUM, &error); assert(client != NULL); assert(error == SOCKET_NOERROR); //send from client int packets_sent = minisocket_send(client, bText, strlen(bText), &error); assert(packets_sent == strlen(bText)); assert(error == SOCKET_NOERROR); minisocket_close(client); minithread_sleep_with_timeout(15000); //assert that minisocket has closed response = minisocket_send(client, aText, strlen(aText), &error); assert(response == -1); assert(error == SOCKET_INVALIDPARAMS); return 0; }
int thread1(int* arg) { minithread_fork(thread2, NULL); printf("Thread 1 starts.\n"); minithread_sleep_with_timeout(500); /* five seconds */ printf("Thread 1 just woke up, and is going to sleep again.\n"); minithread_sleep_with_timeout(1500); /* fifteen seconds */ printf("Thread 1 just woke up and finishes\n"); return 0; }
int thread2(int* arg) { /*minithread_t thread =*/ minithread_fork(thread3, NULL); printf("Thread 2 starts.\n"); minithread_sleep_with_timeout(1000); /* ten seconds */ printf("Thread 2 just woke up and finishes\n"); // printf("Curr time: %llu\n", ((unsigned long long) clk_count) * clk_period); return 0; }
int server(int* arg) { int i; //network_synthetic_params(0.1, 0.1); for (i=0; i<THREAD_COUNTER; i++) { minithread_fork(sender,&thread_id[i]); } return 0; }
int create_server(int *arg) { minisocket_error error; int i = 0; printf("Server waits.\n"); server_socket = minisocket_server_create(port,&error); printf("Server created.\n"); minithread_fork(sender, &thread_id[i]); return 0; }
int run_sleep_threads(int* arg){ if ((long)arg == 1){ minithread_sleep_with_timeout((long)arg * 1000); printf("Thread %li woke up!\n", (long)arg); return 0; } minithread_fork(run_sleep_threads, (void*)(((long)arg)-1)); minithread_sleep_with_timeout((long)arg * 1000); printf("Thread %li woke up!\n", (long)arg); return 0; }
int thread1(int* arg) { minithread_fork(thread2, NULL); while (x < 20) { printf("Thread 1, x = %d.\n", x++); semaphore_P(sem1); semaphore_V(sem2); } printf("End of Thread 1\n"); return 0; }
int sink(int* arg) { channel_t* p = (channel_t *) malloc(sizeof(channel_t)); int value; p->produce = semaphore_create(); semaphore_initialize(p->produce, 0); p->consume = semaphore_create(); semaphore_initialize(p->consume, 0); minithread_fork(source, (int *) p); for (;;) { filter_t* f; semaphore_P(p->consume); value = p->value; semaphore_V(p->produce); if (value == -1) break; printf("%d is prime.\n", value); f = (filter_t *) malloc(sizeof(filter_t)); f->left = p; f->prime = value; p = (channel_t *) malloc(sizeof(channel_t)); p->produce = semaphore_create(); semaphore_initialize(p->produce, 0); p->consume = semaphore_create(); semaphore_initialize(p->consume, 0); f->right = p; minithread_fork(filter, (int *) f); } return 0; }
int necromancer(int* arg) { int num_children = 2999; int i; printf("Necromancer %d spawning %d undead threads.\n", minithread_id(), num_children); for(i = 0; i < num_children; i++){ minithread_fork(zombie, NULL); } printf("Come, my servants!\n"); minithread_yield(); printf("Fascinating.\n"); return 0; }
/* * Initialization. * Initializes reaper and idle threads, starts and initializes main thread. * Also creates the scheduler and other data * */ void minithread_system_initialize(proc_t mainproc, arg_t mainarg) { //allocate room for schedule data (global) schedule_data = (scheduler *) malloc(sizeof(scheduler)); if (schedule_data == NULL) { exit(1); //OOM } schedule_data->cleanup_queue = queue_new(); schedule_data->multi_run_queue = multilevel_queue_new(num_levels); reaper_sema = semaphore_create(); semaphore_initialize(reaper_sema, 0); // create main thread minithread_t* main_thread = minithread_fork(mainproc, mainarg); // initialize idle thread idle_thread = (minithread_t *) malloc(sizeof(minithread_t)); idle_thread->stacktop = NULL; idle_thread->thread_id = -1; //initialize alarm bookeeping data structure (priority queue) alarm_init(); //remove from run queue and run it schedule_data->running_thread = main_thread; main_thread->status = RUNNING; multilevel_queue_dequeue(schedule_data->multi_run_queue, 0, (void *) main_thread); //reaper thread init reaper_thread = minithread_create(reaper_queue_cleanup, NULL); minithread_start(reaper_thread); //Start clock minithread_clock_init(clock_period, clock_handler); //Initialize network network_initialize(network_handler); //START MAIN PROC //minithread_switch also enables clock interrupts minithread_switch(&idle_thread->stacktop, &main_thread->stacktop); //always comes back here to idle in the kernel level (allows freeing resources) while (1) { minithread_t* next = next_runnable(); set_interrupt_level(DISABLED); next->status = RUNNING; schedule_data->running_thread = next; minithread_switch(&idle_thread->stacktop, &next->stacktop); } }
int client(int* arg) { int i; minisocket_error error; network_address_t address; network_translate_hostname(hostname, address); recv_skt = minisocket_client_create(address, port, &error); for (i=0; i<THREAD_COUNTER; i++) { minithread_fork(receiver,&thread_id[i]); } return 0; }
int transmit(int* arg) { char buffer[BUFFER_SIZE]; int i; int bytes_sent; minisocket_t socket; minisocket_error error; minithread_fork(receive, NULL); // receiver = minithread_fork(receive, NULL); printf("in transmit\n"); socket = minisocket_server_create(port,&error); printf("created a server\n"); if (socket==NULL){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); return -1; } /* Fill in the buffer with numbers from 0 to BUFFER_SIZE-1 */ for (i=0; i<BUFFER_SIZE; i++){ buffer[i]=(char)(i%256); } /* send the message */ bytes_sent=0; while (bytes_sent!=BUFFER_SIZE){ int trans_bytes= minisocket_send(socket,buffer+bytes_sent, BUFFER_SIZE-bytes_sent, &error); printf("Sent %d bytes.\n",trans_bytes); if (error!=SOCKET_NOERROR){ printf("ERROR: %s. Exiting. \n",GetErrorDescription(error)); /* close the connection */ minisocket_close(socket); return -1; } bytes_sent+=trans_bytes; } /* close the connection */ minisocket_close(socket); return 0; }
int A(int* arg) { char** file_list; minithread_fork(B, NULL); file_list = minifile_ls("/"); if (!file_list) { printf("A: ls failed. abort!\n"); return -1; } assert(list_count(file_list) == 2); assert(!strcmp(file_list[0], ".")); free(file_list[0]); assert(!strcmp(file_list[1], "..")); free(file_list[1]); free(file_list); printf("A: root test passed\n"); if (minifile_mkdir("A") == -1) { printf("A: mkdir failed. abort!\n"); return -1; } file_list = minifile_ls("/"); if (!file_list) { printf("A: ls failed. abort!\n"); return -1; } assert(list_count(file_list) == 3); assert(!strcmp(file_list[0], ".")); free(file_list[0]); assert(!strcmp(file_list[1], "..")); free(file_list[1]); assert(!strcmp(file_list[2], "A")); free(file_list[2]); free(file_list); printf("A: exiting\n"); return 0; }