Ejemplo n.º 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);
}
Ejemplo n.º 2
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);
  }
}
Ejemplo n.º 3
0
Archivo: shm.c Proyecto: waterylife/SFC
int parent_process()
{
	int shm_id;
	void* shm_ptr = NULL;

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

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

	char buf[1024] = "String from parent.";
	memcpy(shm_ptr, (const void*)buf, strlen(buf));

	if(-1 == tell_child()) {
		printf("[P]fail to tell_child, process exit\n");
		shmdt(shm_ptr);
		shmctl(shm_id, IPC_RMID, 0);
		_exit(0);
	}

	if(-1 == wait_child()) {
		printf("[P]fail to wait_child, process exit\n");
		shmdt(shm_ptr);
		shmctl(shm_id, IPC_RMID, 0);
		_exit(0);
	}

	shmctl(shm_id, IPC_RMID, 0);

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char const *argv[])
{
    char buf[MAXSIZE]={'\0'};
    char buf_r[MAXSIZE]={'\0'};

    int fd[2];
    pipe(fd);
    signal(SIGPIPE,fun);
    pid_t pid;
    int len = 0;
    int nr = 0;
    int sum = 0;
    int nread,len_r;
    //signal tongbu
    tell_wait();
    //sgnal tongbu
    pid  = fork();
    if (pid <0) {
        perror("error");
        /* code */
    }else if (pid == 0) {
        /* code */
       
        wait_father();
        printf("the child process.....%d\n",getpid());
        printf("the child process's father process id:%d\n",getppid());
        close(fd[1]);

        while((nread = read(fd[0],buf,sizeof(buf)))!=-1){
            tell_father(pid);
            setbuf(stdout,NULL);//set the stdout no buffer
            printf("the child write to stdout:");
            write(STDOUT_FILENO,buf,nread); //the write to stdout is bo bug ,but the printf is bug

            len_r = strlen(buf);
            printf("len_r :%d\n",len_r );

            for (int i = 0; (buf[i]!='\n'); i++) {
                sum +=i;
                printf("sum:%d\n",sum );
                printf("buf[%d]:%c\t",i,buf[i] );
                if (buf[i]=='\0') {
                    break;
                    
                }

            }
            printf("\n");

            // printf("child read:%s\n",buf ); //the sentence is bug ,it  is  of buffer and \n
        }
        exit(0);      
    }else{
        // sleep(3);
        tell_child(pid);
        printf("the father process.......%d\n",getpid());
        printf("the father process's father process id:%d\n",getppid());
        close(fd[0]);//the sentense to invlole the SIGPIPE take place
        char * p  = NULL; /* code */ 
        while((p = fgets(buf,sizeof(buf),stdin))!=NULL){

            len = strlen(buf);
            printf("the length :%d\n",len );
            if (p[len] =='\n') {
                printf("%c\n",p[len -1] );
                len--;
                p[len ] = '\0';
                /* code */
            }
            if (p[len -1] == '\0') {
                printf("fgets  read the null in buffer\n");
                /* code */
            }

            fflush(stdin);
            int  n = write(fd[1],buf,len);
            if (n==-1) {
                if (errno = EINTR) {
                    printf("the failed is SIGINT\n");
                    /* code */
                }
                printf("err write\n");
            }else
                printf("father write to pipe successfully\n");

            wait_child();
        }

        int status;
        // the waitpid 's return val is the son process pid,if it wait to catch the son state successfully
        int ret = waitpid(pid,&status,0);
        if (ret == -1) {
            printf("wait child failed\n");
            /* code */
        }else
            printf("the wait id:%d\n", ret);//the ret == son pid
        
    }

    
  
    return 0;
}
Ejemplo n.º 5
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);
}