zoukankan      html  css  js  c++  java
  • 进程和课堂总结

    2015.1.29

    父进程等待一个子进程的终止,则它必须调用:wait函数。如果一个进程要等待父进程的终止,则可以向下面这样:缺点是浪费CPU时间
    while(getppid() != 1)
    sleep(1);

    IPC:进程间通信

    int main()
    {
    pid_t pid;
    if((pid = fork()) < 0)
    {
    err_sys("fork error");
    }
    else if(pid == 0)
    {
    if((pid = fork()) < 0 )
    {
    err_sys("fork error");
    }
    else if(pid > 0)
    {
    exit(0);
    }

    sleep(2);
    printf("second chile,parent pid = %d ",getppid());
    exit(0);
    }

    if(waitpid(pid,NULL,0) != pid)
    {
    err_sys("waitpid error");
    }
    exit(0);
    }

    上面程序运行的结果:second chile,parent pid = 1;

    每个进程在执行他的一套初始化操作之后要通知对方,并且在继续运行之前,要等待另一方完成其初始化操作:

    #include "apue.h"
    TELL_WAIT(); 告诉****WAIT***
    if((pid = fork()) < 0)
    {
    err_sys("fork error");
    }
    else if(pif == 0)
    {
    TELL_PARENT(getppid()); /*tell parent we're done*/
    WAIT_PARENT(); /*and wait for parent*/
    exit(0);
    }
    TELL_CHILD(pid);
    WAIT_CHILE();
    exit(0);

    具有竞争条件的程序:
    #include <stdio.h>
    static void charartatime(char *);
    int main(void)
    {
    pid_t pid;
    if((pid = fork()) < 0)
    {
    err_sys("fork error");
    }
    else if(pid == 0)
    {
    charatatime("output from child ");
    }
    else
    {
    charatatime("output from parent ");
    }
    exit(0);
    }

    static void charatatime(char *str)
    {
    char *ptr;
    int c;
    setbuf(stdout,NULL);
    for(ptr = str;(c = *ptr++) != 0;)
    {
    putc(c,stdout);
    }
    }

    修改程序以避免竞争:父进程先运行
    #include <stdio.h>
    static void charartatime(char *);
    int main(void)
    {
    pid_t pid;
    TELL_WAIT(); 告诉****WAIT***

    if((pid = fork()) < 0)
    {
    err_sys("fork error");
    }
    else if(pid == 0)
    {
    WAIT_PARENT(); /*parent goes first*/
    charatatime("output from child ");
    }
    else
    {
    charatatime("output from parent ");
    TELL_CHILD(pid);
    }
    exit(0);
    }

    static void charatatime(char *str)
    {
    char *ptr;
    int c;
    setbuf(stdout,NULL);
    for(ptr = str;(c = *ptr++) != 0;)
    {
    putc(c,stdout);
    }
    }

    更改上面的程序,让子进程先运行:

    else if(pid == 0)
    {
    charatatime("output from child ");
    TELL_PARENT(getppid());
    }
    else
    {
    WAIT_CHILE(); /*child goes first*/
    charatatime("output from parent ")
    }

    回忆一下今天上课的内容:上午复习了一下昨天指针的知识点,包括:指针的基础,指针的运算,多级指针,指针数组,const,
    void,字符指针和字符串,串讲了指针和数组之间的联系和用法。今天主要讲的是,函数这一块的知识点,包括:函数的定义和声明
    函数的调用,参数的传递,和返回值,函数和数组,main函数的参数,指针函数,递归函数。最后讲了一点结构体的知识。说说今天
    听课的收获吧,递归函数我是明白了,函数传参方式我很早就明白,今天从栈和内存的角度进一步了解了函数传参的实质,对指针数组
    和数组指针有了进一步的了解,对后面的赋值方式有了进一步的领悟,int *p[2], int a[2][3], p[0] = a[0]。这段时间上的C,从
    知识点的角度讲,很少有我不知道,但是还是要多练练好点。这段时间上的课让我挺轻松的,这给了我很好的缓冲时间,在放假前我要看完
    UNIX环境高级编程这本书,这样回家后我就能玩ARM开发板,这个才是我想玩的嘛!

  • 相关阅读:
    project
    类欧几里得算法
    还不熟悉的内容
    Hall定理
    FWT学习笔记
    最小树形图
    BZOJ 3027 [Ceoi2004]Sweet
    多项式除法
    VUE 计算属性和侦听器
    VUE 模板语法
  • 原文地址:https://www.cnblogs.com/cnlg/p/4261282.html
Copyright © 2011-2022 走看看