Example #1
0
  /* Return the first element in the list and remove it, moving all
   * other elements forward. */
  Object* List::shift(STATE) {
    if(empty_p()) return Qnil;

    count(state, Integer::from(state, count_->to_native() - 1));
    ListNode* n = first_;
    first(state, first_->next());

    if(last_ == n) {
      last(state, nil<ListNode>());
    }

    return n->object();
  }
Example #2
0
  /* Return the first element in the list and remove it, moving all
   * other elements forward. */
  Object* List::shift(STATE) {
    if(empty_p()) return cNil;

    count(state, Fixnum::from(count()->to_native() - 1));
    ListNode* n = first();
    first(state, first()->next());

    if(last() == n) {
      last(state, nil<ListNode>());
    }

    return n->object();
  }
Example #3
0
  /* Search the List for +obj+ and remove all instances of it.
   *
   * Returns the number of elements removed. */
  size_t List::remove(STATE, const Object* obj) {
    if(empty_p()) return 0;

    size_t deleted = 0, counted = 0;

    ListNode* node = first_;
    ListNode* lst =  nil<ListNode>();
    ListNode* nxt =  nil<ListNode>();

    while(!node->nil_p()) {
      nxt = node->next();

      if(node->object() == obj) {
        deleted++;
        if(lst->nil_p()) {
          first(state, nxt);
        } else {
          lst->next(state, nxt);
        }

        if(last_ == node) {
          last(state, lst);
        }

        lst = nil<ListNode>();
      } else {
        counted++;
        lst = node;
      }

      node = nxt;
    }

    count(state, Integer::from(state, counted));

    return deleted;
  }
Example #4
0
static string printList(Object self) {
  Object next;
  int first = 1;
  string str = calloc(2, sizeof(string));

  if (!list_p(self)) {
    exitWithMessage(-2, "Internal error in printList");
  }

  str = alloc_strcat(str, "(");

  for (next = self ;;) {
    if (empty_p(next)) {
      str = alloc_strcat(str, ")");
      break;
    } else {
      if (first) {
        first = 0;
      } else {
        str = alloc_strcat(str, " ");
      }

      if (next->type == LIST) {
        str = alloc_strcat(str, car(next)->print(car(next)));
        next = cdr(next);
      } else {
        str = alloc_strcat(str, ". ");
        str = alloc_strcat(str, next->print(next));
        str = alloc_strcat(str, ")");
        break;
      }
    }
  }

  return str;
}
Example #5
0
int main()
{
	struct timeval start, end;
    int running = 1;
    void *shared_memory = (void *)0;
    struct shared_use_st *shared_stuff;
    int shmid;

    srand((unsigned int)getpid());

    sem_id = semget((key_t)1233, 1, 0666 | IPC_CREAT);
    sem_id2 = semget((key_t)1235, 1, 0666 | IPC_CREAT);
    sem_id3 = semget((key_t)1236, 1, 0666 | IPC_CREAT);
    
	if (!set_available()| !set_empty()) {
            fprintf(stderr, "Failed to initialize semaphore\n");
            exit(EXIT_FAILURE);
        }

    shmid = shmget((key_t)1231, (sizeof(struct shared_use_st)- sizeof(int)) , 0666 | IPC_CREAT);

    if (shmid == -1) {
        fprintf(stderr, "shmget failed prod\n");
        exit(EXIT_FAILURE);
    }

/* We now make the shared memory accessible to the program. */

    shared_memory = shmat(shmid, (void *)0, 0);
    if (shared_memory == (void *)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }

    printf("Memory attached at %X\n", (void *)shared_memory);

/* The next portion of the program assigns the shared_memory segment to shared_stuff,
 which then prints out any text in written_by_you. The loop continues until end is found
 in written_by_you. The call to sleep forces the consumer to sit in its critical section,
 which makes the producer wait. */

    shared_stuff = (struct shared_use_st *)shared_memory;
    //shared_stuff->written_by_you = 0;

    char ibuffer[BUFSIZ];   // input buffer 
 
    int in; // variable to hold reference to input file
    int index=0; // current index of element in array of structs in shared memory 
    int len;  // length read 
     int tot = 0; // Total length read

	//timer start
	gettimeofday(&start, NULL);
	for (int i = 0; i < 100000; i++){}
	
    in = open("test4k.txt",O_RDONLY);
   
	len = read(in,ibuffer, sizeof(ibuffer));
    while(running){
	
	for(int i = 0; i<= len; i+=128){
		//sleep(2);
		if((len-i)<128){
		
		shared_stuff->msgs[index].written = len-i;
		memcpy(shared_stuff->msgs[index].some_text, ibuffer + (len-i), (len-i));
		tot+= shared_stuff->msgs[index].written;
		printf("Producer: bytes written val %d\n", shared_stuff->msgs[index].written);

		printf("Producer: Total bytes written val %d\n", tot);		

		
		//timer finished
		gettimeofday(&end, NULL);
		printf("\nElapsed Time: %ld micro sec\n", ((end.tv_sec * MICRO_SEC_IN_SEC + end.tv_usec)- (start.tv_sec * MICRO_SEC_IN_SEC + start.tv_usec)));
		
		sleep(4);
		del_semvalue();
		
		/*
		//timer finished
		gettimeofday(&end, NULL);
		printf("\nElapsed Time: %ld micro sec\n", ((end.tv_sec * MICRO_SEC_IN_SEC + end.tv_usec)- (start.tv_sec * MICRO_SEC_IN_SEC + start.tv_usec)));
		*/
		exit(EXIT_SUCCESS);
		
		}
		else{
		empty_p();	
		
 		shared_stuff->msgs[index].written = 128;
		//printf("writte\n");
		printf("Producer: bytes written val %d\n", shared_stuff->msgs[index].written);
		memcpy(shared_stuff->msgs[index].some_text,ibuffer + i,128);		
		tot+= shared_stuff->msgs[index].written;
		
		index = (index+1)%k;  

		available_v();
		
		}
	}
   }
  

/* Lastly, the shared memory is detached and then deleted. */

    if (shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }

    if (shmctl(shmid, IPC_RMID, 0) == -1) {
        fprintf(stderr, "shmctl(IPC_RMID) failed\n");
        exit(EXIT_FAILURE);
    }

	//del_semvalue();

    exit(EXIT_SUCCESS);
}
Example #6
0
int main()
{
    int running = 1;
    void *shared_memory = (void *)0;
    struct shared_use_st *shared_stuff;
    int shmid;

    srand((unsigned int)getpid());

	//get the semaphores	

    sem_id = semget((key_t)1233, 1, 0666 | IPC_CREAT);
    sem_id2 = semget((key_t)1235, 1, 0666 | IPC_CREAT);
    sem_id3 = semget((key_t)1236, 1, 0666 | IPC_CREAT);

/*
*sets the semaphores to the required values if it fails program ends
*/
    
	if (!set_mutex()| !set_available()| !set_empty()) {
            fprintf(stderr, "Failed to initialize semaphore\n");
            exit(EXIT_FAILURE);
        }

	//get shared memory

    shmid = shmget((key_t)1231, (sizeof(struct shared_use_st)- sizeof(int)) , 0666 | IPC_CREAT);

    if (shmid == -1) {
        fprintf(stderr, "shmget failed prod\n");
        exit(EXIT_FAILURE);
    }

/* We now make the shared memory accessible to the program. */

    shared_memory = shmat(shmid, (void *)0, 0);
    if (shared_memory == (void *)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }

    printf("Memory attached at %X\n", (void *)shared_memory);

/* The next portion of the program assigns the shared_memory segment to shared_stuff,
 Reads in from the input file test4k.txt into the buffer ibuffer string of size BUFSIZ, then the 
 loop continually copies text from ibuffer into the buffers in shared memory which take 128 bytes of char until a value less than 128 is written which means approached end of text then it sleeps and deletes the semaphores */

    shared_stuff = (struct shared_use_st *)shared_memory;
    //shared_stuff->written_by_you = 0;

    char ibuffer[BUFSIZ];   // input buffer 
 
    int in; // variable to hold reference to input file
    int index=0; // current index of element in array of structs in shared memory 
    int len;  // length read 
     int tot = 0; // Total length read

    in = open("test4k.txt",O_RDONLY);
   
	len = read(in,ibuffer, sizeof(ibuffer));
    while(running){
	
	for(int i = 0; i<= len; i+=128){
		sleep(2);
		if((len-i)<128){

		//if value written is less than 128  delete semaphores


		shared_stuff->msgs[index].written = len-i;
		memcpy(shared_stuff->msgs[index].some_text, ibuffer + (len-i), (len-i));
		tot+= shared_stuff->msgs[index].written;
		printf("Producer: bytes written val %d\n", shared_stuff->msgs[index].written);

		printf("Producer: Total bytes written val %d\n", tot);		

		sleep(10);
		del_semvalue();
		exit(EXIT_SUCCESS);
		
		}
		else{
		empty_p();	
		mutex_p();

 		shared_stuff->msgs[index].written = 128;
		
		printf("Producer: bytes written val %d\n", shared_stuff->msgs[index].written);
		memcpy(shared_stuff->msgs[index].some_text,ibuffer + i,128);		
		tot+= shared_stuff->msgs[index].written;
		
		index = (index+1)%k;  
		mutex_v();
		available_v();
		
		}
	}
   }
  

/* Lastly, the shared memory is detached and then deleted. */

    if (shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }

    if (shmctl(shmid, IPC_RMID, 0) == -1) {
        fprintf(stderr, "shmctl(IPC_RMID) failed\n");
        exit(EXIT_FAILURE);
    }

	

    exit(EXIT_SUCCESS);
}