Example #1
0
int main(int argc, char ** argv) {

	//初始化两个信号集,一个只有usr1,一个只有usr2
	sigemptyset(&t_sigset_usr);
	sigemptyset(&t_sigset_old);
	sigemptyset(&t_sigset_zero);

	sigaddset(&t_sigset_usr, SIGUSR1);
	sigaddset(&t_sigset_usr, SIGUSR2);

	//注册信号处理程序,
	signal(SIGUSR1, do_sig_usr);
	signal(SIGUSR2, do_sig_usr);

	//父进程阻塞usr信号
	sigprocmask(SIG_SETMASK, &t_sigset_usr, NULL );

	pid_t child_pid;

	//父进程fork一个子进程出来
	child_pid = fork();
	if (child_pid == 0) {
		//子进程,从2开始写
		num = 2;

		//注册信号处理程序,
		signal(SIGUSR1, do_sig_usr);
		signal(SIGUSR2, do_sig_usr);

		//子进程阻塞usr信号
		sigprocmask(SIG_SETMASK, &t_sigset_usr, NULL );

		//子进程准备完毕,通知父进程去写
		tell_parent();

		while (1) {
			//始终等待父进程唤醒
			wait_parent();
			write_num_to_file();
			tell_parent();
		}

	} else {
		//父进程
		while (1) {
			//始终等待子进程唤醒
			wait_child();
			write_num_to_file();
			tell_child(child_pid);
		}

	}

	//子进程写入数据,并且告知父进程,然后投入睡眠,等待父进程唤醒

	//父进程等待子进程

	exit(0);
}
Example #2
0
void
test_waitpid(void)
{
	wait_badpid(-8, "wait for pid -8");
	wait_badpid(-1, "wait for pid -1");
	wait_badpid(0, "pid zero");
	wait_badpid(NONEXIST_PID, "nonexistent pid");

	wait_nullstatus();
	wait_badstatus(INVAL_PTR, "wait with invalid pointer status");
	wait_badstatus(KERN_PTR, "wait with kernel pointer status");

	wait_unaligned();

	wait_badflags();

	wait_self();
	wait_parent();
	wait_siblings();
}
Example #3
0
int main(int argc, char* argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[1]);
    exit(1);
  }

  int fd;
  fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE);
  write(fd, "abcdef", 6);

  struct stat statbuf;
  fstat(fd, &statbuf);
  fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID);

  tell_wait();

  pid_t pid;
  pid = fork();

  if (pid > 0) {
    write_lock(fd, 0, SEEK_SET, 0);
    tell_child(pid);
    waitpid(pid, NULL, 0);
  } else {
    wait_parent();
    set_fl(fd, O_NONBLOCK);

    if (read_lock(fd, 0, SEEK_SET, 0) != -1)
      err_sys("child: read_lock successed");
    printf("read_lock of already-locked region returns %d\n", errno);

    lseek(fd, 0, SEEK_SET);

    char buf[5];
    if (read(fd, buf, 2) < 0)
      err_ret("read failed (mandatory locking works)");
    else
      printf("read OK (no mandatory locking), buf = %2.2s\n", buf);
  }
}
Example #4
0
File: shm.c Project: waterylife/SFC
int child_process()
{
	int shm_id;
	void* shm_ptr = NULL;

	if(-1 == wait_parent()) {
		printf("[C]fail to wait_parent\n");
		return -1;
	}


	shm_id = shmget(SHM_KEY, SHM_SIZE, SHM_MODE);
	if(-1 == shm_id) {
		printf("[C]Fail to open shm, errno: %d, errmsg: %s\n", errno, strerror(errno));
		return -1;
	}

	shm_ptr = shmat(shm_id, 0, 0);
	if((void*)-1 == shm_ptr) {
		printf("[C]Fail to attach to shm, errno: %d, errmsg: %s\n", errno, strerror(errno));
		return -1;
	}

	char buf[1024] = {0};
	memcpy((void*)buf, shm_ptr, strlen(shm_ptr));
	printf("[C]Shm contents: %s\n", buf);

	shmdt(shm_ptr);

	if(-1 == tell_parent()) {
		printf("[C]Fail to tell_parent\n");
		return -1;
	}

	return 0;
}
Example #5
0
void
test_waitpid(void)
{
	int ntests = 0, lost_points = 0;
	int result;

	ntests++;
	result = wait_badpid(-8, "wait for pid -8");
	handle_result(result, &lost_points);

	ntests++;
	result = wait_badpid(-1, "wait for pid -1");
	handle_result(result, &lost_points);

	ntests++;
	result = wait_badpid(0, "pid zero");
	handle_result(result, &lost_points);

	ntests++;
	result = wait_badpid(NONEXIST_PID, "nonexistent pid");
	handle_result(result, &lost_points);


	ntests++;
	result = wait_nullstatus();
	handle_result(result, &lost_points);

	ntests++;
	result = wait_badstatus(INVAL_PTR, "wait with invalid pointer status");
	handle_result(result, &lost_points);

	ntests++;
	result = wait_badstatus(KERN_PTR, "wait with kernel pointer status");
	handle_result(result, &lost_points);


	ntests++;
	result = wait_unaligned();
	handle_result(result, &lost_points);


	ntests++;
	result = wait_badflags();
	handle_result(result, &lost_points);


	ntests++;
	result = wait_self();
	handle_result(result, &lost_points);

	ntests++;
	result = wait_parent();
	handle_result(result, &lost_points);

	ntests++;
	result = wait_siblings();
	handle_result(result, &lost_points);

	if(!lost_points)
		success(TEST161_SUCCESS, SECRET, "/testbin/badcall");
}
Example #6
0
int csopen(char *name, int oflag)
{
	pid_t pid;
	int len;
	char buf[10];
	struct iovec iov[3];
	static int fd[] = {-1, -1};
	struct sockaddr_un addr;
	socklen_t addr_len;
	int new_fd = -1;

	//init server address
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, SERVER);
	addr_len = offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path);

	if (0 > new_fd)
	{
		tell_wait();
		if (0 > (pid = fork()))
		{
			perror("fork");
			return EXIT_FAILURE;
		} else if (0 == pid)
		{
			//open socket and connect to server
			if (-1 == (fd[1] = socket(AF_UNIX, SOCK_STREAM, 0)))
			{
				perror("socket child");
				return EXIT_FAILURE;
			}
			wait_parent();
			if (-1 == connect(fd[1], (struct sockaddr*)&addr, addr_len))
			{
				perror("connect");
				return EXIT_FAILURE;
			}

			if (fd[1] != STDIN_FILENO && dup2(fd[1], STDIN_FILENO) != STDIN_FILENO)
			{
				perror("dup2 on stdin");
				return EXIT_FAILURE;
			}
			if (fd[1] != STDOUT_FILENO && dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
			{
				perror("dup2 on stdin");
				return EXIT_FAILURE;
			}
			if (0 > execl("bin/opend1", "opend1", (char*)0))
			{
				perror("execl");
				return EXIT_FAILURE;
			}
			return EXIT_SUCCESS;
		}
		//create a server and accept connections on it
		if (-1 == (fd[0] = socket(AF_UNIX, SOCK_STREAM, 0)))
		{
			perror("socket parent");
			return EXIT_FAILURE;
		}
		if (-1 == unlink(SERVER))
		{
			perror("unlink");
			return EXIT_FAILURE;
		}
		if (-1 == bind(fd[0], (struct sockaddr*)&addr, addr_len))
		{
			perror("bind");
			return EXIT_FAILURE;
		}
		if (-1 == listen(fd[0], 3))
		{
			perror("listen");
			return EXIT_FAILURE;
		}
		tell_child();
		if (-1 == (new_fd = accept(fd[0], (struct sockaddr*)&addr, &addr_len)))
		{
			perror("accept");
			return EXIT_FAILURE;
		}
	}
	sprintf(buf, " %d", oflag);
	iov[0].iov_base = CL_OPEN " ";//string concatenation
	iov[0].iov_len = strlen(CL_OPEN)+1;
	iov[1].iov_base = name;
	iov[1].iov_len = strlen(name);
	iov[2].iov_base = buf;
	iov[2].iov_len = strlen(buf)+1;
	len = iov[0].iov_len+iov[1].iov_len+iov[2].iov_len;
	if (len != writev(new_fd, iov, 3))
	{
		perror("writev");
		return EXIT_FAILURE;
	}

	return recv_fd(new_fd, write);
}