Пример #1
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;
}
Пример #2
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;
}
Пример #3
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;
}
Пример #4
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);

    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_sleep_with_timeout(5000);// 5 second sleep
    for (i=MAX_COUNT/2; i<MAX_COUNT; 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);
    }

    return 0;
}
Пример #5
0
int
transmit(int* arg) {
    char buffer[BUFFER_SIZE];
    int length;
    int i;
    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);

    minithread_fork(receive1, NULL);
    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);
        minithread_sleep_with_timeout(200);
    }

    return 0;
}
Пример #6
0
int
serverMethod(int* arg) {
    char responseBuffer[30];

    minisocket_error error;

    //create server
    server = minisocket_server_create(PORT_NUM, &error);
    assert(error == SOCKET_NOERROR);
    assert(server != NULL);

    //send from server
    int packets_sent = minisocket_send(server, aText, strlen(aText), &error);
    assert(packets_sent == strlen(aText));
    assert(error == SOCKET_NOERROR);

    minisocket_close(server);

    minithread_sleep_with_timeout(15000);

    int response = minisocket_send(server, aText, strlen(aText), &error);
    assert(response == -1);
    assert(error == SOCKET_INVALIDPARAMS);

    error = SOCKET_NOERROR;

    //recreate server
    server = minisocket_server_create(PORT_NUM, &error);
    assert(error == SOCKET_NOERROR);
    assert(server != NULL);

    response = minisocket_receive(server, responseBuffer, 30, &error);
    assert(response == 20);
    assert(strcmp(responseBuffer, "bbbbbbbbbbbbbbbbbbbb") == 0);

    minithread_sleep_with_timeout(15000);

    //Assert that minisocket has closed
    response = minisocket_send(server, aText, strlen(aText), &error);
    assert(response == -1);
    assert(error == SOCKET_INVALIDPARAMS);

    fprintf(stderr, "Full Test Passed \n");

    return 0;

}
Пример #7
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;
}
Пример #8
0
int thread3(int *arg)
{
    printf("I am thread3...\n");
    printf("I am going to sleep for 2 second...\n");
    printf("hu lu lu...\n");
    minithread_sleep_with_timeout(2000);
    printf("fired at %ld ticks.\n", ticks);
    printf("thread3 woke up...\n");

    return 0;
}
Пример #9
0
int
thread0(int *arg)
{
    printf("I am thread0...\n");
    printf("I am going to sleep for 1 second...\n");
    printf("hu hu hu...\n");
    minithread_sleep_with_timeout(1000);
    printf("fired at %ld ticks.\n", ticks);
    printf("thread0 woke up...\n");

    return 0;
}
/* sleepy thread that takes its time */
int sleepy(int* arg) {
	last = SLEEPY;

	while ( !sinkDone ) {
		last = SLEEPY;
		minithread_sleep_with_timeout(1000);
		last = SLEEPY;
	}

	printf("Sleepy takes a 3 second nap.\n");

	minithread_sleep_with_timeout(3000);

	last = SLEEPY;

	sleepyDone = 1;

	printf("Sleepy wakes. All done.\n");

	minithread_sleep_with_timeout(1000);

	printf("Sleepy exits.\n");
	return 0;
}
Пример #11
0
int
thread(int* arg) {
    char buffer[BUFFER_SIZE];
    int length = BUFFER_SIZE;
    miniport_t from;
    network_address_t my_address;

    network_get_my_address(my_address);
    receive_port = miniport_create_unbound(0);
    send_port = miniport_create_bound(my_address, 0);

    minimsg_send(receive_port, send_port, text, textlen);
    minithread_sleep_with_timeout(1000);
    miniport_destroy(receive_port);
    receive_port = miniport_create_unbound(0);
    printf("waiting\n");
    minimsg_receive(receive_port, &from, buffer, &length);
    printf("%s", buffer); //should not print

    return 0;
}
int sink(int* arg) {
	channel_t* p = (channel_t *) malloc(sizeof(channel_t));
	int value;
	filter_t* f;
	int i;
	minithread_t sleepy_t;


	last = SINK;
	sinkDone = 0;
	sleepyDone = 0;
	

	printf("Will begin in 5 seconds");

	for(i=0; i * 500 < 5000; i++) {
		minithread_sleep_with_timeout(250);
		printf(".");
	}
	minithread_sleep_with_timeout(250);
	printf("Started.\n");

	sleepy_t = minithread_fork(sleepy, NULL);

	minithread_fork(greedy, NULL);

	p->produce = semaphore_create();
	semaphore_initialize(p->produce, 0);
	p->consume = semaphore_create();
	semaphore_initialize(p->consume, 0);

	minithread_fork(source, (int *) p);

	last = SINK;
	semaphore_P(p->consume);
	last = SINK;
	value = p->value;
	semaphore_V(p->produce);
	last = SINK;
	
	while ( value != -1 ) {
		last = SINK;
		spin(1000, SINK);
		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);

		last = SINK;
		semaphore_P(p->consume);
		last = SINK;
		value = p->value;
		semaphore_V(p->produce);
	}
	last = SINK;
	semaphore_P(p->consume);
	last = SINK;

	// cleanup last channel
	semaphore_destroy(p->consume);
	semaphore_destroy(p->produce);
	free(p);

	sinkDone = 1;

	spin(1000, SINK);
	printf("Sink Exits.\n");
	return 0;
}