备忘学习笔记

第一章:出错处理,善于使用errno,errno特性


第三章:open函数创建文件检查文件的原子操作;open函数返回file id最小未使用的id

第三章:sync未刷到磁盘,只到输入队列;fsync刷到磁盘;fdatasync与fsync相同,但是不包含文件属性


第四章:读取文件需要文件夹的x权限


第七章:setjmp longjmp变量值问题

第七章:atexit(),exit是标准库函数,_exit是一个系统调用

#include <stdio.h>
#include <string.h>

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <unistd.h>
#include <time.h>

#include <stdlib.h>

void test_exit()
{
    printf("%s", __func__);
}

int main(int argc, char *argv[])
{
    printf("test");

    atexit(test_exit);

    //exit(1);
    _exit(1);
    return 0;
}

第八章:sizeof strlen区别,strlen在运行时调用函数,sizeof编译时就确定了长度

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char sbuffer[] = "abcd efg\n";
char sbuffer1[1024] = "abcd efg\n";

int main(int argc, char *argv[])
{
    int i = 0;
    for(i = 0; i < argc; i++)
    {
        printf("argv[%d] = %s\r\n", i, argv[i]);
    }

// printf("abc" "def");
// strlen不计算null,sizeof计算null
    printf("sizeof=%d, strlen=%d\r\n", sizeof(sbuffer), strlen(sbuffer));
    snprintf(sbuffer, sizeof(sbuffer), "%s", "xdeasdfewragadf");
    printf("%s\r\n", sbuffer);
    return 0;
}


第八章:fork父子进程共享文件描述符,关闭文件要注意

第八章:SIGCHLD,一个子进程退出,系统向其父进程发送这个信号

第八章:僵尸进程,子进程已经退出,父进程还未调用wait waitpid来收尸,子进程将变成僵尸进程;两次fork可以避免僵尸进程或者父进程;调用signal(SIGCHLD,SIG_IGN)也可以避免出现僵尸进程

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid = fork();

    if(pid == 0)
    {
        //child
        exit(123);
    }
    else if(pid > 0)
    {
        //parent
        printf("child pid = %d\r\n", pid);

        //child process is zombie

        sleep(10);

        printf("waitpid\r\n");
        int ns = 0;

        pid_t np = waitpid(pid, &ns, 0);
        //最后一个参数 0 ==> WNOHANG非阻塞

        printf("%d,%d\r\n", np, ns);

        printf("正常终止则为真:%d, 异常终止则为真:%d, 暂停则为真:%d, xxx:%d\r\n", WIFEXITED(ns), WIFSIGNALED(ns), WIFSTOPPED(ns), WIFCONTINUED(ns));

        if(WIFEXITED(ns))
        {
            printf("exit返回值:%d\r\n", WEXITSTATUS(ns));
        }

        if(WIFSIGNALED(ns))
        {
            printf("终止信号编号:%d, 是否产生core:%d\r\n", WTERMSIG(ns), WCOREDUMP(ns));
        }

        if(WIFSTOPPED(ns))
        {
            printf("子进程暂停信号编号:%d\r\n", WSTOPSIG(ns));
        }

        //child process is really exit

        sleep(10);

        printf("exit...\r\n");
    }
    else
    {
        printf("fork error\r\n");
    }
    return 0;
}



第十章:signal函数第二个参数为SIG_IGN表示忽略,为SIG_DFL表示使用默认
第十章:pause函数,使调用进程休眠直到有信号
第十章:信号可能引起阻塞的系统调用中断

第十章:alarm信号覆盖问题


第十三章:守护进程编程规则

第十三章:单实例文件锁 var-run-xxxfile


第十五章:管道:半双工,父子进程限制。FIFO命名管道:半双工,无父子进程限制;unix域套接字无限制
第十五章:XSI IPC分别为消息队列,信号量,共享内存的优缺点:由系统控制,进程消亡,内存还在;在文件系统中没有名字,需要新系统调用支持,不能ls,rm,chmod,不得不增加ipcs ipcrm命令
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值