示例#1
0
文件: 1-1.c 项目: shubmit/shub-ltp
int main() {
        int result, child_pid, tmp_errno, policy;
	int min_priority, new_priority, old_priority;
	struct sched_param param;

        /* Create a child process which wait SIGUSR1 */
        child_pid = fork();
        if (child_pid == -1) {
		perror("An error occurs when calling fork()");
		return PTS_UNRESOLVED;
        } else if (child_pid == 0) {
		child_proc();
        }

	if (sched_getparam(child_pid, &param) != 0) {
		perror("An error occurs when calling sched_getparam()");
		kill(child_pid, SIGUSR1);
		return PTS_UNRESOLVED;
	}

	/* Assume that the process have permission to change priority of
	   its child */
	old_priority = param.sched_priority;

	policy = sched_getscheduler(0);
	if (policy == -1) {
		perror("An error occurs when calling sched_getscheduler()");
		return PTS_UNRESOLVED;
	}
	min_priority = sched_get_priority_min(policy);

	new_priority = param.sched_priority == min_priority ?
		(param.sched_priority = sched_get_priority_max(policy)) :
		(param.sched_priority = min_priority);

	result = sched_setparam(child_pid, &param);
	tmp_errno = errno;

	if (sched_getparam(child_pid, &param) != 0) {
		perror("An error occurs when calling sched_getparam()");
		kill(child_pid, SIGUSR1);
		return PTS_UNRESOLVED;
	}

	if (result == 0 && param.sched_priority == new_priority) {
		printf("Test PASSED\n");
		kill(child_pid, SIGUSR1);
		return PTS_PASS;
	} else if (result == 0 && param.sched_priority == old_priority) {
		printf("The param does not change.\n");
		kill(child_pid, SIGUSR1);
		return PTS_FAIL;
	} else if (result == -1 && tmp_errno == EPERM) {
		printf("The process have not permission to change the param of its child.\n");
		kill(child_pid, SIGUSR1);
		return PTS_UNRESOLVED;
	} else if (result == -1 && tmp_errno == EINVAL) {
		/* the new priority may be to big */
		/* test with a new priority lower than the old one */
		param.sched_priority = (new_priority -= 2);
		result = sched_setparam(child_pid, &param);

		if (result == 0 && param.sched_priority == new_priority) {
			printf("Test PASSED");
			kill(child_pid, SIGUSR1);
			return PTS_PASS;
		}
	}

	perror("Unknow error");
	kill(child_pid, SIGUSR1);
	return PTS_FAIL;
}
示例#2
0
文件: samp1.c 项目: cbh34680/jbx
int main_proc(void)
{
	int syserr = -1;
	int fd = -1;
	void* data = MAP_FAILED;

	fd = open("/home/iret/video.dat", O_RDONLY);
	if (fd == -1)
	{
		perror("open");
		goto EXIT_LABEL;
	}

	struct stat st = { 0 };
	syserr = fstat(fd, &st);
	if (syserr)
	{
		perror("fstat");
		goto EXIT_LABEL;
	}

	off_t data_len = st.st_size;
	data = mmap(NULL, data_len, PROT_READ, MAP_PRIVATE, fd, 0);
	if (data == MAP_FAILED)
	{
		perror("mmap");
		goto EXIT_LABEL;
	}

	srand(time(NULL));

	for (int i=0; i<CHILDS_NUM; i++)
	{
		int start = rand() % ((int)st.st_size / 2);

		pid_t pid = fork();
		assert(pid != -1);

		if (! pid)
		{
			child_proc(data, start);
		}
	}

	munmap(data, data_len);
	data = MAP_FAILED;

	close(fd);
	fd = -1;

	int suc = 0;

	while (1)
	{
		int status = 0;
		pid_t pid = wait(&status);
		if (pid == -1)
		{
			break;
		}

		if (WIFEXITED(status))
		{
			suc += WEXITSTATUS(status);
		}
	}

	fprintf(stderr, "DONE suc=%d\n", suc);

EXIT_LABEL:

	if (data != MAP_FAILED)
	{
		munmap(data, data_len);
		data = MAP_FAILED;
	}

	if (fd != -1)
	{
		close(fd);
		fd = -1;
	}

	return 0;
}