void createProc(int number, pid_t* pid_array){
	int i = 0;
	pid_t pid;
	int *array;
	
	//Preparing random order squence
	array = (int*)malloc(sizeof(int)*number);

	for(i = 0; i < number; ++i)
		array[i] = i;
	shuffle(array, number);

	unlockSem(0); //aby mohol prvy bezat
	for(i = 0; i < number; ++i){
		switch(pid = fork()){
			case 0:
      			doWork(array[i], getNext(array, i + 1, number));
			exit(EXIT_SUCCESS);
    		case -1:
      			perror("fork");
      			exit(EXIT_FAILURE);
    		default:
				pid_array[i] = pid;
      			break;
		}
	}
	free(array);
}
void doWork(int position, int position1){
	char buf[4096];
	char buf1[4096];
	char temp[20];
	struct sigaction sa;
	sigset_t sigsetMask;

	buf[0] = '\0';
 
	sigfillset(&sigsetMask);
	sigdelset(&sigsetMask, SIGUSR1);
	sigprocmask( SIG_BLOCK, &sigsetMask, NULL);
	
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = &ChildSignalHandler;
	CHECK(sigaction(SIGUSR1, &sa, NULL) ==  0);

	while(quit){
		
		lockSem(position);
		read_((char*)&buf1); //precita obsah pamate
		sprintf((char*)&temp, "%u\n", getpid()); ///vytvori string z PID
		// fprintf(stderr, "PID:%s", temp);
		strcat((char*)&buf, (char*)&buf1); //spoji dokopy
		// fprintf(stderr, "@SHARED:%s", buf);
		strcat((char*)&buf, (char*)&temp);
		// fprintf(stderr, "2SHARED:%s", buf);
		write_((char*)&buf, strlen(buf));
		sleep(1);
		unlockSem(position1);
		buf[0] = '\0'; //premazanie
	}
	fprintf(stderr, "Killed %u\n", getpid());
}
Exemplo n.º 3
0
int writeFd(LOG_OPT* pLogOpt, const char* data)
{
	int fd = 0;
	time_t tnow;
	struct tm tm;
	struct tm* ptm;
    struct stat l_stat;
	char time_buf[MAX_TIME_MSG_LEN] = {0};
	char* pFileName = NULL;
	char msg[MAX_SINGLE_MSG_LEN] = {0};
	
	if (NULL == data|| strlen(data) <= 0)
		return 0;
	
	tnow = time(NULL);
	ptm = localtime_r(&tnow, &tm);

	strftime(time_buf, sizeof(time_buf), TIME_FORMAT, ptm);
	snprintf(msg, sizeof(msg), "%s%s%s", time_buf, data, LOG_EOL);

	if (pLogOpt->sem_flag)
		lockSem(pLogOpt->sem_id);
	
	if (0 == pLogOpt->is_daemon){
		printf("%s", msg);
	}

	if (-1 == write(pLogOpt->file_fd, msg, strlen(msg))){
		if (pLogOpt->file_name){
			fd = open(pLogOpt->file_name, O_WRONLY|O_APPEND|O_CREAT, 0666);
			if (fd > 0){
				write(pLogOpt->file_fd, msg, strlen(msg));
				pLogOpt->file_fd = fd;
			}
		}
	}

	if (pLogOpt->sem_flag)
		unlockSem(pLogOpt->sem_id);

	if (NULL != pLogOpt->file_name && pLogOpt->max_size > 0){
		stat(pLogOpt->file_name, &l_stat);
		if (l_stat.st_size > pLogOpt->max_size){
			close(pLogOpt->file_fd);
			free(pLogOpt->file_name);
			if (NULL == (pFileName = (char*)calloc(1, MAX_LOG_PATH_LEN * sizeof(char)))){
				pLogOpt->err_code = LOG_ERR_MALLOC_NULL;
				return -1;
			}
			getFileName(pFileName);
			if ((fd = open(pLogOpt->file_name, O_WRONLY|O_APPEND|O_CREAT, 0666)) < 0){
				pLogOpt->err_code = LOG_ERR_FD_WRONG;
				return -1;
			}
			pLogOpt->file_fd = fd;
			pLogOpt->file_name = pFileName;
		}
	}

	if (pLogOpt->sock_type == LOG_TYPE_TCP || pLogOpt->sock_type == LOG_TYPE_UDP || pLogOpt->sock_type == LOG_TYPE_FUN)
		return writeSock(pLogOpt, msg);
	
	return 0;
}
Exemplo n.º 4
0
int closeMemory(mymemory *  mem)
{
    return unlockSem(mem->semDescr, mem->semId);
}