zoukankan      html  css  js  c++  java
  • Operating systems Chapter 5

    Interlude: Process API

    • the fork() system call
      this function will create a new process and exact copy of calling-process, that is, as same as what process invoke the function, However, the new process own its virtual memory. If create successfully, return the child(the new process) pid to the father(the process that invoke the function), the new process will begin at the fork() function and fork() will return 0 to the child. If create unsuccess, return -1 to sign.

    • the wait() system call(or waitpid()) (reference)
      using fork(), the run order is based on the CPU-scheduler, either child first or father first is valid, so use wait() to make father block, then child will complete before father.
      the function prototype is pid_t wait(int *status),while invoke it, the process will block itself until any child exit to become "zombie", the state will receive the exit status and return the child's pid.
      the similar function is waitpid pid_t waitpid(pid_t pid,int *status,int options), with different parameter, the behave different.

      • pid:
        • pid > 0: wait of the specific child with pid no matter how others exit
        • pid = 0: wait the child of same process group unless the child is in other process group
        • pid < -1: wait any child of specific process group that have same id of abs(pid)
        • pid = -1: wait any child exit
      • that is, wait(NULL) = waitpid(-1,NULL,0)
    • the exec() family (man page)
      the man page have more details about function prototype, in my words, the function will invoke other program, and load its all detail, that is, re-initialized the process. So we can use it to create a new process and make different behave of its father.
      so the shell will achieve like this: receive instruction and invoke fork(), the father invoke wait(), the child using exec() to do the instruciton and return.

    • the pipe() system call (man page) (reference)
      use it, we can achieve the interprocess communication, the pipe, like a queue, one side read, other side write, it have four status that will block or exit, more details in the man page.

    • Homework

      • How to ensure the child will run before father without using wait()?
        Obviously, using the pipe and its block behave will achieve it
      • using pipe() to achieve interprocess communication between two child with same father
        using for and index to distinguish the child pid, other same as before homework

    code

    int
    main(int argv[], char *args[]) {
    	int fd[2];
    	char readbuffer[100];
    	int result = pipe(fd);
    	char hello[] = "hello";
    	char bye[] = "goodbye";
    	int rc = fork();
    	if(rc < 0) {
    		fprintf(stderr, "Nop
    ");
    		exit(1);
    	} else if(rc == 0) {
    		close(fd[0]);
    		write(fd[1], hello, sizeof(hello));
    		close(fd[1]);
    	} else {
    		close(fd[1]);
    		read(fd[0], readbuffer, sizeof(readbuffer));
    		close(fd[0]);
    		printf("%s
    %s
    ", readbuffer, bye);
    	}
    	return 0;
    }
    
    #include<stdio.h>
    #include<unistd.h>
    #include<sys/wait.h>
    #include<string.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #define MAXSIZE 10240
    
    int
    main(int argv[], char *args[]) {
    	int fd[2], i, pid;
    	pipe(fd);
    	for(i = 0; i < 2; ++i)
    		if((pid = fork()) == 0) break;
    		else if(pid < 0) {
    			fprintf(stderr, "Nop
    ");
    			exit(1);
    		}
    	if(i == 0) { //first child
    		close(fd[1]);
    		char receive[1024*128];
    		read(fd[0], receive, sizeof(receive));
    		printf("I m first, I get some massage from my 2nd bro, that's %s
    ", receive);
    		close(fd[0]);
    	} else if(i == 1) { //second
    		close(fd[0]);
    		char *convey = strdup("Hi,my bro");
    		write(fd[1], convey, sizeof(convey));
    		printf("I m second, I send some massage to my 1st bro
    ");
    		close(fd[1]);
    	} else {
    		for(i = 0; i < 2; ++i) wait(NULL);
    		printf("Even if Im father, what happened to my children?
    ");
    	}
    	
    	return 0;
    
    }
    
  • 相关阅读:
    mysql基础之日志管理(查询日志、慢查询日志、错误日志、二进制日志、中继日志、事务日志)
    jQuery1.9.1针对checkbox的调整
    管理 sendmail 的邮件队列
    将以管理员方式运行cmd运行方式放到win7任务栏
    转 git操作小结
    [转]前端CSS规范整理
    jQuery 1.9不支持$.browser 怎么判断浏览器类型和版本
    js闭包陷阱问题
    关于NGINX下开启PHP-FPM 输出PHP错误日志的设置(已解决)
    PHP中cURL错误号对照[转]
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12386534.html
Copyright © 2011-2022 走看看