fixed_queue_t *fixed_queue_new(size_t capacity) {
  fixed_queue_t *ret = calloc(1, sizeof(fixed_queue_t));
  if (!ret)
    goto error;

  ret->list = list_new(NULL);
  if (!ret->list)
    goto error;

  ret->enqueue_sem = semaphore_new(capacity);
  if (!ret->enqueue_sem)
    goto error;

  ret->dequeue_sem = semaphore_new(0);
  if (!ret->dequeue_sem)
    goto error;

  pthread_mutex_init(&ret->lock, NULL);
  ret->capacity = capacity;

  return ret;

error:;
  if (ret) {
    list_free(ret->list);
    semaphore_free(ret->enqueue_sem);
    semaphore_free(ret->dequeue_sem);
  }

  free(ret);
  return NULL;
}
Exemplo n.º 2
0
/* Establish the file system and write it to disk */
int
fs_format(mem_sblock_t sbp)
{
    blocknum_t inode_bitmap_size;
    blocknum_t block_bitmap_size;
    blocknum_t i;
    sbp->filesys_lock = semaphore_new(1);
    if (NULL == sbp->filesys_lock) {
        return -1;
    }

    fs_lock(sbp);

    /* Format super block */
    sblock_get(maindisk, sbp);
    sblock_format(sbp, disk_size);
    sblock_update(sbp);

    /* Format inode and block bitmaps */
    inode_bitmap_size = sbp->inode_bitmap_last - sbp->inode_bitmap_first + 1;
    sbp->inode_bitmap = malloc(inode_bitmap_size * DISK_BLOCK_SIZE);
    block_bitmap_size = sbp->block_bitmap_last - sbp->block_bitmap_first + 1;
    sbp->block_bitmap = malloc(block_bitmap_size * DISK_BLOCK_SIZE);
    if (NULL == sbp->block_bitmap || NULL == sbp->inode_bitmap) {
        semaphore_V(sbp->filesys_lock);
        return -1;
    }
    /* Clear bitmaps */
    bitmap_zeroall(sbp->inode_bitmap, inode_bitmap_size * BITS_PER_BLOCK);
    bitmap_zeroall(sbp->block_bitmap, block_bitmap_size * BITS_PER_BLOCK);
    /* Set file system blocks to be occupied */
    for (i = 0; i <= sbp->block_bitmap_last; ++i) {
        bitmap_set(sbp->block_bitmap, i);
    }
    /* Set inode 0 to be occupied */
    bitmap_set(sbp->inode_bitmap, 0);
    /* Push updates to disk */
    for (i = sbp->inode_bitmap_first; i <= sbp->inode_bitmap_last; ++i) {
        bpush(i, (char*) sbp->inode_bitmap + (i - sbp->inode_bitmap_first)
              * DISK_BLOCK_SIZE);
    }
    for (i = sbp->block_bitmap_first; i <= sbp->block_bitmap_last; ++i) {
        bpush(i, (char*) sbp->block_bitmap + (i - sbp->block_bitmap_first)
              * DISK_BLOCK_SIZE);
    }

    /* Count free inodes and free blocks */
    mainsb->free_inodes = bitmap_count_zero(mainsb->inode_bitmap,
                                            mainsb->total_inodes);
    mainsb->free_blocks = bitmap_count_zero(mainsb->block_bitmap,
                                            mainsb->disk_num_blocks);

    fs_unlock(sbp);
    return 0;
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: diegohdorta/C
int main(void)
{
	int count;
	int semaphore_id;
	int g_shm_id;
	int *g_shm_addr;

	pid_t pid = 1;
	pid_t pids[NO_OF_CHILDREN];

	semaphore_id = semaphore_new(SEM_KEY);
	v(semaphore_id, 1);
		
	g_shm_id = create_shared_memory(SHM_KEY);
	g_shm_addr = associate_shared_memory(g_shm_id);
	*g_shm_addr = 0;

	for (count = 0; count < NO_OF_CHILDREN; count++) {

		if (pid) {
		
			if ((pid = fork()) < 0) {
				fprintf(stderr, "The fork() function has failed: %s\n", strerror(errno));
				return EXIT_FAILURE;
			}
			
			pids[count] = pid;
			
			if (pid)
				fprintf(stderr, "#%d# Created pid process: %d\n", getpid(), (pids[count]));		
		}
		else
			break;
	}

	if (!pid)
		print_characters(semaphore_id, g_shm_addr);

	else {
		usleep(WAIT_CHILDREN);
		printf(NEW_LINE);
		
		for (count = 0; count < NO_OF_CHILDREN; count++) {
			
			kill(pids[count], SIGKILL);
			fprintf(stderr, "#%d# Killing pid process: %d\n", getpid(), pids[count]);
		
		}
		
		shared_memory_destroy(g_shm_id);
		semaphore_destroy(semaphore_id); 
	}
	exit(EXIT_SUCCESS);
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: diegohdorta/C
static void init(void)
{

	semaphore_1 = semaphore_new(SEMAPHORE_1_KEY, 2);
	semaphore_2 = semaphore_new(SEMAPHORE_2_KEY, 2);

	v(semaphore_1, BUFFER_SIZE, 0);
	v(semaphore_2, ONE, 0);	
	v(semaphore_2, ONE, 1);

	/*free_id = semaphore_new(FREE_KEY);
	busy_id = semaphore_new(BUSY_KEY);
	producer_lock = semaphore_new(PRODUCT_KEY);
	consumer_lock = semaphore_new(CONSUMER_KEY);*/
	stderr_lock = semaphore_new(STDERR_KEY, 1);

	/*v(free_id, BUFFER_SIZE);	
	v(producer_lock, ONE);
	v(consumer_lock, ONE);*/
	v(stderr_lock, ONE, 0);
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
    int s, ns;
    int semaphore_id;
    int shared_memory_id = 0;
    uint16_t port;
    pid_t pid;
    struct sockaddr_in client;
    char *tmp_shm_addr;
    shm_t *g_shm;

    check_args(&argc, argv, 0);

    port = (uint16_t) atoi(argv[1]);
    create_tcp_socket(&s, port);

    semaphore_id = semaphore_new(SEM_KEY);
    v(semaphore_id, 1);

    shared_memory_id = create_shared_memory(SHM_KEY);
    tmp_shm_addr = associate_shared_memory(shared_memory_id);
    g_shm = (shm_t *) tmp_shm_addr;


    while (true) {

        accept_new_connection(&s, &ns, &client);

        if ((pid = fork()) == 0)

            shandler(&ns, semaphore_id, client, g_shm);

        else {
            if (pid > 0)

                fprintf(stderr, "#%d# Father - Created child process: %d\n", getpid(), pid);

            else {
                fprintf(stderr, "The fork() function has failed: %s!\n", strerror(errno));
                shared_memory_destroy(shared_memory_id);
                semaphore_destroy(semaphore_id);
                exit(EXIT_FAILURE);
            }
        }
    }

    shared_memory_destroy(shared_memory_id);
    semaphore_destroy(semaphore_id);

    exit(EXIT_SUCCESS);
}
Exemplo n.º 6
0
/* Initialize the file system structure from disk */
int
fs_init(mem_sblock_t sbp)
{
    blocknum_t inode_bitmap_size;
    blocknum_t block_bitmap_size;
    blocknum_t i;
    sbp->filesys_lock = semaphore_new(1);
    if (NULL == sbp->filesys_lock) {
        return -1;
    }

    fs_lock(sbp);
    sblock_get(maindisk, sbp);
    sblock_put(sbp);
    if (sblock_isvalid(sbp) != 1) {
        sblock_print(sbp);
        kprintf("File system is not recognized. ");
        kprintf("Recommend running './mkfs <blocks>'.\n");
        return -2;
    }

    inode_bitmap_size = sbp->inode_bitmap_last - sbp->inode_bitmap_first + 1;
    sbp->inode_bitmap = malloc(inode_bitmap_size * DISK_BLOCK_SIZE);
    block_bitmap_size = sbp->block_bitmap_last - sbp->block_bitmap_first + 1;
    sbp->block_bitmap = malloc(block_bitmap_size * DISK_BLOCK_SIZE);
    if (NULL == sbp->block_bitmap || NULL == sbp->inode_bitmap) {
        semaphore_V(sbp->filesys_lock);
        return -1;
    }

    /* Get disk bitmap */
    for (i = sbp->inode_bitmap_first; i <= sbp->inode_bitmap_last; ++i) {
        bpull(i, (char*) sbp->inode_bitmap + (i - sbp->inode_bitmap_first)
              * DISK_BLOCK_SIZE);
    }
    for (i = sbp->block_bitmap_first; i <= sbp->block_bitmap_last; ++i) {
        bpull(i, (char*) sbp->block_bitmap + (i - sbp->block_bitmap_first)
              * DISK_BLOCK_SIZE);
    }

    /* Count free inodes and free blocks */
    mainsb->free_inodes = bitmap_count_zero(mainsb->inode_bitmap,
                                            mainsb->total_inodes);
    mainsb->free_blocks = bitmap_count_zero(mainsb->block_bitmap,
                                            mainsb->disk_num_blocks);

    fs_unlock(sbp);

    return 0;
}
Exemplo n.º 7
0
thread_t *thread_new_sized(const char *name, size_t work_queue_capacity) {
  assert(name != NULL);
  assert(work_queue_capacity != 0);

  thread_t *ret = osi_calloc(sizeof(thread_t));
  if (!ret)
    goto error;

  ret->reactor = reactor_new();
  if (!ret->reactor)
    goto error;

  ret->work_queue = fixed_queue_new(work_queue_capacity);
  if (!ret->work_queue)
    goto error;

  // Start is on the stack, but we use a semaphore, so it's safe
  struct start_arg start;
  start.start_sem = semaphore_new(0);
  if (!start.start_sem)
    goto error;

  strncpy(ret->name, name, THREAD_NAME_MAX);
  start.thread = ret;
  start.error = 0;
  pthread_create(&ret->pthread, NULL, run_thread, &start);
  semaphore_wait(start.start_sem);
  semaphore_free(start.start_sem);

  if (start.error)
    goto error;

  return ret;

error:;
  if (ret) {
    fixed_queue_free(ret->work_queue, osi_free);
    reactor_free(ret->reactor);
  }
  osi_free(ret);
  return NULL;
}
thread_t *thread_new(const char *name) {
  assert(name != NULL);

  // Start is on the stack, but we use a semaphore, so it's safe
  thread_t *ret = calloc(1, sizeof(thread_t));
  if (!ret)
    goto error;

  ret->reactor = reactor_new();
  if (!ret->reactor)
    goto error;

  ret->work_queue = fixed_queue_new(WORK_QUEUE_CAPACITY);
  if (!ret->work_queue)
    goto error;

  struct start_arg start;
  start.start_sem = semaphore_new(0);
  if (!start.start_sem)
    goto error;

  strncpy(ret->name, name, THREAD_NAME_MAX);
  start.thread = ret;
  start.error = 0;
  pthread_create(&ret->pthread, NULL, run_thread, &start);
  semaphore_wait(start.start_sem);
  semaphore_free(start.start_sem);
  if (start.error)
    goto error;
  return ret;

error:;
  if (ret) {
    fixed_queue_free(ret->work_queue, free);
    reactor_free(ret->reactor);
  }
  free(ret);
  return NULL;
}
Exemplo n.º 9
0
static bool lazy_initialize(void) {
  assert(alarms == NULL);

  pthread_mutex_init(&monitor, NULL);

  alarms = list_new(NULL);
  if (!alarms) {
    LOG_ERROR("%s unable to allocate alarm list.", __func__);
    return false;
  }

  struct sigevent sigevent;
  memset(&sigevent, 0, sizeof(sigevent));
  sigevent.sigev_notify = SIGEV_THREAD;
  sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback;
  if (timer_create(CLOCK_ID, &sigevent, &timer) == -1) {
    LOG_ERROR("%s unable to create timer: %s", __func__, strerror(errno));
    return false;
  }

  alarm_expired = semaphore_new(0);
  if (!alarm_expired) {
    LOG_ERROR("%s unable to create alarm expired semaphore", __func__);
    return false;
  }

  callback_thread_active = true;
  callback_thread = thread_new("alarm_callbacks");
  if (!callback_thread) {
    LOG_ERROR("%s unable to create alarm callback thread.", __func__);
    return false;
  }

  thread_set_priority(callback_thread, CALLBACK_THREAD_PRIORITY_HIGH);
  thread_post(callback_thread, callback_dispatch, NULL);
  return true;
}
Exemplo n.º 10
0
int main(int argc, char**argv)
{

  GtkWidget *hboxg, *vbox1,*vbox2;

  /*Inicialización de variables y semaforos*/
  sfd = 0;
  connected = 0;
  in_channel = 0;
  queried = 0;
  port = 0;
  server_called = 0;
  readers_num = 0;
  their_calling_port = 0;

  if ((readers = semaphore_new()) == ERROR){
    syslog(LOG_ERR, "Failed while creating a new semaphore: %s\n", strerror(errno));
    return EXIT_FAILURE;
  }

  if ((writer = semaphore_new()) == ERROR){
    syslog(LOG_ERR, "Failed while creating a new semaphore: %s\n", strerror(errno));
    semaphore_rm(readers);
    return EXIT_FAILURE;
  }

  if ((mutex_access = semaphore_new()) == ERROR){
    syslog(LOG_ERR, "Failed while creating a new semaphore: %s\n", strerror(errno));
    semaphore_rm(readers);
    semaphore_rm(writer);
    return EXIT_FAILURE;
  }

  if ((mutex_rvariables = semaphore_new()) == ERROR){
    syslog(LOG_ERR, "Failed while creating a new semaphore: %s\n", strerror(errno));
    semaphore_rm(readers);
    semaphore_rm(writer);
    semaphore_rm(mutex_access);
    return EXIT_FAILURE;
  }


  g_type_init (); /* Necesario para tener funcionalidad de hilos */
  gdk_threads_init ();
  gdk_threads_enter ();
  gtk_init(&argc, &argv); /* Inicia gnome */

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL); /* Ventana principal */
 
  gtk_window_set_title(GTK_WINDOW(window), "IRC Chat"); /* Título ventana principal */
  gtk_window_set_default_size(GTK_WINDOW(window), 800, 350); /* Tamaño ventana principal */
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); /* Posición ventana principal */

  /* Estructura global */
  hboxg = gtk_hbox_new(FALSE,5);
  vbox1 = gtk_vbox_new(FALSE,5);
  vbox2 = gtk_vbox_new(FALSE,5);
  gtk_container_add(GTK_CONTAINER(window), hboxg);
  gtk_box_pack_start(GTK_BOX(hboxg), vbox1,FALSE,FALSE,1);
  gtk_box_pack_start(GTK_BOX(hboxg), vbox2,FALSE,FALSE,1);
  ConnectArea(vbox1);
  StateArea(vbox1);
  ChatArea(vbox2);

  g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(ordered_exit), NULL);
  g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window); /* Presentación de las ventanas */

  gdk_threads_leave (); /* Salida de hilos */
  gtk_main(); /* Administración de la interacción */

  return 0;
}