Exemplo n.º 1
0
int main () {
  int semaphore_id;
  int segment_id;
  char* shared_memory;
  struct shmid_ds shmbuffer;
  int segment_size, i, j;
  pid_t child_pid;

  segment_id = shmget (IPC_PRIVATE, getpagesize(),
           IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

  semaphore_id = binary_semaphore_allocation(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

  binary_semaphore_initialize(semaphore_id);

  shared_memory = (char*) shmat (segment_id, 0, 0);

  sprintf (shared_memory, "%d", 100);
  printf("%s\n", shared_memory);

  child_pid = fork ();
  if (child_pid != 0) {
    for (i = 0; i < 1000000; ++i) {
      binary_semaphore_wait(semaphore_id);
      sprintf (shared_memory, "%d", atoi(shared_memory) + 1);
      sprintf (shared_memory, "%d", atoi(shared_memory) - 1);
      printf("%s\n", shared_memory);
      binary_semaphore_post(semaphore_id);
    }

  }
  else {
    for (i = 0; i < 1000000; ++i) {
      binary_semaphore_wait(semaphore_id);
      sprintf (shared_memory, "%d", atoi(shared_memory) + 1);
      sprintf (shared_memory, "%d", atoi(shared_memory) - 1);
      printf("%s\n", shared_memory);
      binary_semaphore_post(semaphore_id);
    }
    return 0;
  }


  printf("\nValor final: %s\n", shared_memory);

  shmdt (shared_memory);

  shmctl (segment_id, IPC_RMID, 0);

  binary_semaphore_deallocate(semaphore_id);

  return 0;
}
int main()
{
    int semid = binary_semaphore_allocation(KEY, 0666);
    fprintf(stderr, "binary_semaphore_allocation success! semid: %d\n", semid);
    
    binary_semaphore_post(semid);
    fprintf(stderr, "post successful\n");
    return 0;
}
Exemplo n.º 3
0
int application_request(int fd,data_person * users,int * newid)
{
    binary_semaphore_wait(semaphore);
    person person;
    simple_person * simple_person;
    int ans=0;
    clsvbuff buff;

    if(read_msg(fd,&buff)<0)
      return -1;

    switch(buff.opc)
    {
      case GET_INFO:
          ans = get_info(&buff,users,newid,&person);
          ans_get_info(fd,&buff,ans,&person);
          break;
      case GET_MATCHS:
          ans = get_matchs(&buff,users,&simple_person);
          ans_get_matchs(fd,ans,&buff,&simple_person);
          break;
      case SHOW_PEOPLE:
          ans = show_people(&buff,users,newid,&simple_person);
          ans_show_people(fd,ans,&buff,&simple_person);
          break;
      case REGISTER:
          ans=register_user(&buff,users,newid);
          ans_register_user(fd,ans,&buff);
          break;
      case LOGIN: /*In this case ans doubles as opOK*/
          ans = login(&buff,users,newid);
          ans_login(fd,ans,&buff);
          break;
      case EVALUATION:  /*In this case ans doubles as opOK*/
          users[buff.id_client-1].last_evaluation=buff.data.clsv_evaluation.id;
          if(buff.data.clsv_evaluation.like==true)
          {
            ans = evaluation(&buff,users);
            ans_evaluation(fd,ans,&buff);
          }
          else
           ans_evaluation(fd,0,&buff);

          break;
      default: /*OPC NOT RECOGNIZED*/
          return -1;
        break;
    }

    binary_semaphore_post(semaphore);



    return 0;

}
Exemplo n.º 4
0
/*******************************************************************************
 Function name   : EWLReleaseHw
 Description     : Release HW resource when frame is ready
*******************************************************************************/
void EWLReleaseHw(const void *inst)
{
    u32 val;
    hx280ewl_t *enc = (hx280ewl_t *) inst;

    assert(enc != NULL);

    val = EWLReadReg(inst, 0x38);
    EWLWriteReg(inst, 0x38, val & (~0x01)); /* reset ASIC */

    PTRACE("------>EWLReleaseHw: PID %d trying to release ...\n", getpid());

#ifdef USE_LINUX_LOCK_IOCTL
    // release the encoder

    if(binary_semaphore_post(enc->fd_enc, 0) != 0)
#else
	if(binary_semaphore_post(enc->semid, 0) != 0)
#endif
    {
        PTRACE("------>binary_semaphore_post error: %s\n", strerror(errno));
        assert(0);
    }

#ifdef LOCK_POST_PROCESSOR
    /* we have to release postprocessor also */

#ifdef USE_LINUX_LOCK_IOCTL
	if(binary_semaphore_post(enc->fd_dec, 1) != 0)
#else
    if(binary_semaphore_post(enc->semid, 1) != 0)
#endif
    {
        PTRACE("------>binary_semaphore_post error: %s\n", strerror(errno));
        assert(0);
    }
#endif
    PTRACE("------>EWLReleaseHw: HW released by PID %d\n", getpid());
}
Exemplo n.º 5
0
/*******************************************************************************
 Function name   : EWLReserveHw
 Description     : Reserve HW resource for currently running codec
*******************************************************************************/
i32 EWLReserveHw(const void *inst)
{
    hx280ewl_t *enc = (hx280ewl_t *) inst;

    assert(enc != NULL);

    PTRACE("EWLReserveHw: PID %d trying to reserve ...\n", getpid());

    /* Check invalid parameters */
    if(inst == NULL)
        return EWL_ERROR;

    if(binary_semaphore_wait(enc->semid, 0) != 0)
    {
        PTRACE("binary_semaphore_wait error: %s\n", strerror(errno));
        return EWL_ERROR;
    }

    PTRACE("Codec semaphore locked\n");

    /* we have to lock postprocessor also */
    if(binary_semaphore_wait(enc->semid, 1) != 0)
    {
        PTRACE("binary_semaphore_wait error: %s\n", strerror(errno));
        if(binary_semaphore_post(enc->semid, 0) != 0)
        {
            PTRACE("binary_semaphore_post error: %s\n", strerror(errno));
            assert(0);
        }
        return EWL_ERROR;
    }
    PTRACE("Post-processor semaphore locked\n");
    PTRACE("EWLReserveHw: HW locked by PID %d\n", getpid());

    EWLWriteReg(inst, 0x38, 0);

    return EWL_OK;
}
Exemplo n.º 6
0
int main ()
{	
	pid_t pid,sid;
	int count;
	double load,loadavg = 0;

	signal(SIGTERM, sig_handler);
	signal(SIGINT, sig_handler);
	
	pid = fork();
	if(pid < 0) {
		fprintf(stderr, "Error: fork() failed\n");
		exit(EXIT_FAILURE);
	}
	if(pid > 0) {
		exit(EXIT_SUCCESS);
	}

	umask(0);
	
	openlog("PM_Load Daemon:", LOG_CONS, LOG_LOCAL0);
	sid = setsid();
	if(sid < 0) {
		fprintf(stderr,"Error: Cannot create session\n");
		exit(EXIT_FAILURE);
	}

	if((chdir("/tmp/migrate/")) < 0) {
		fprintf(stderr,"Error: /tmp/migrate/ not found\n");
		exit(EXIT_FAILURE);
	}

	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);

	syslog(LOG_INFO,"Load daemon started");

	create_shm_seg(&segment_id);
	
	create_bin_sem(&sem_id);

	write_int_to_file(SHM_ID_PATH,&segment_id);
	write_int_to_file(SEM_ID_PATH,&sem_id);
	


	shared_memory = (char *) shmat(segment_id, 0, 0);
	init_binary_semaphore(&sem_id,0);
	syslog(LOG_INFO,"Migrate daemon has started");
	syslog(LOG_INFO,"IPC setup complete");
	syslog(LOG_INFO,"Load calculation and exchange started");
	while(1) {
		for(count = 0; count < 454; count++) {
			read_double_from_pipe(&load,SYS_LOAD);
			CALC_LOADAVG(loadavg,load);
		}
		binary_semaphore_wait(sem_id);
		sprintf(shared_memory,"%0.2f",loadavg);
		binary_semaphore_post(sem_id);
	}
	return 0;
}
Exemplo n.º 7
0
int main()
{
	char *mesg[4],*infobuf[2];
	char *shared_memory;
	char string[1024],temp[1024];
	FILE *file;
	Nodeinfo *mynode;
	int segment_id, sem_id, i = 1024, k = 1024, line, j, count;
	void *buf;
	
	signal(SIGTERM, cleanup_handler);
	signal(SIGINT, cleanup_handler);

	req.tv_nsec = 5000000;
	cursesWin = initscr();
	cdkscreen = initCDKScreen(cursesWin);

	initCDKColor();
	sprintf(temp,"<C></B/3>Process Migration - GUI for Statistics and Testing");
	mesg[0] = copyChar(temp);
	title = newCDKLabel(cdkscreen, CENTER, TOP, mesg,1,1,0);
      	setCDKLabel (title, mesg, 1, 1);
      	drawCDKLabel (title, 1);
	freeChar (mesg[0]);

	loadDaemon = newCDKSwindow (cdkscreen, (COLS - 50) * 0.10, (LINES - 12) * 0.40, 12, 50, "<C></B/5>Load Daemon", 1000, 1, 0);
	commsDaemon = newCDKSwindow (cdkscreen, (COLS - 50) * 0.90, (LINES - 12) * 0.40, 12, 50, "<C></B/5>Communication Daemon", 1000, 1, 0);
	migrateDaemon = newCDKSwindow (cdkscreen, (COLS - 50) * 0.10, (LINES - 12) * 0.90, 12, 50, "<C></B/5>Migration Control Daemon", 1000, 1, 0);
	extraInfo = newCDKSwindow (cdkscreen, (COLS - 50) * 0.90, (LINES - 12) * 0.90, 12, 50, "<C></B/5>Information", 1000, 1, 0);


	//setCDKSwindowBackgroundColor(loadDaemon, "</5>");
	//setCDKSwindowBackgroundColor(commsDaemon, "</5>");
	//setCDKSwindowBackgroundColor(migrateDaemon, "</5>");
	//setCDKSwindowBackgroundColor(extraInfo, "</5>");

	file = fopen("/var/log/load","r");
	while(fgets(string, 1025, file) != NULL) {
		substr(&temp[0],&string[0]);
		addCDKSwindow (loadDaemon, temp, BOTTOM);
	}
	fclose(file);
	
	
	file = fopen("/var/log/migrate","r");
	while(fgets(string, 1025, file) != NULL) {
		substr(&temp[0],&string[0]);
		addCDKSwindow (migrateDaemon, temp, BOTTOM);
	}
	fclose(file);
	
	buf = malloc(33);

	file = fopen(INFO_SHM_ID_PATH,"r");
	fgets(string,33,file);
	segment_id = atoi(string);
	fclose(file);

	file = fopen(INFO_SEM_ID_PATH,"r");
	fgets(string,33,file);
	sem_id = atoi(string);
	fclose(file);
	
	info = newCDKLabel(cdkscreen, 0, (LINES - 5) * 0.15, infobuf,3,0,0);
	shared_memory = (void *) shmat(segment_id, 0, 0);
	//setCDKLabelBackgroundColor(info, "</55>");
	refreshCDKScreen(cdkscreen);
	binary_semaphore_post(sem_id);
	cp_loadlog();
	cp_migratelog();
	for(;;) {
		if(i-- == 0) {
			cp_loadlog();
			i = 1024;
		}
		file = popen("diff /tmp/migrate/load.temp /var/log/load | sed 1d | wc -l","r");
		strcpy(string,"\0");
		fgets(string, 1025, file);
		count = atoi(string);
		pclose(file);
		sprintf(string,"count: %d",count);
		if(count != 0) {
			file = popen("diff /tmp/migrate/load.temp /var/log/load | sed 1d","r");
			for(j = 0; j < count; j++) {
				fgets(string, 1025, file);
				substr(&temp[0],&string[0]);
				addCDKSwindow (loadDaemon, temp, BOTTOM);
			}
			pclose(file);
			i = 0;
		}
		/*if(k-- == 0) {
			cp_migratelog();
			k = 1024;
		}
		file = popen("diff /tmp/migrate/migrate.temp /var/log/migrate | sed 1d | wc -l","r");
		strcpy(string,"\0");
		fgets(string, 1025, file);
		count = atoi(string);
		pclose(file);
		sprintf(string,"count: %d",count);
		if(count != 0) {
			file = popen("diff /tmp/migrate/migrate.temp /var/log/migrate | sed 1d","r");
			for(j = 0; j < count; j++) {
				fgets(string, 1025, file);
				substr(&temp[0],&string[0]);
				addCDKSwindow (migrateDaemon, temp, BOTTOM);
			}
			pclose(file);
			k = 0;
		}*/
		binary_semaphore_wait(sem_id);
		memcpy(buf, shared_memory, 33);
		binary_semaphore_post(sem_id);
		mynode = nodeinfo__unpack(NULL, 33, buf);
		if(mynode == NULL) {
			fprintf(stderr,"error unpacking message\n");
			exit(1);
		}
		sprintf(temp,"\t\t\t\t\t\t\t</B/U/7>STATISTICS");
		infobuf[0] = copyChar(temp);
		sprintf(temp,"\t</B>Load:<!B> %0.2f\t</B>No. of peers connected:<!B> %d\t</B>No. of processes migrated:<!B> %d\t</B>No. of processes accepted:<!B> %d\t",mynode->load,mynode->np,mynode->npm,mynode->npa);
		infobuf[1] = copyChar(temp);
		sprintf(temp,"\t\t\t\t</B>Upper Threshold:<!B> %.2f\t</B>Lower Threshold:<!B> %.2f",mynode->uthresh,mynode->lthresh);
		infobuf[2] = copyChar(temp);
		setCDKLabel (info, infobuf, 3, 0);
		drawCDKLabel (info, 0);
		freeChar (infobuf[0]);
		freeChar (infobuf[1]);
		freeChar (infobuf[2]);
		sleep(1);
	}
	exit(0);
}