第一章:出错处理,善于使用errno,errno特性
第三章: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;
}
第十章:pause函数,使调用进程休眠直到有信号
第十章:信号可能引起阻塞的系统调用中断
第十章:alarm信号覆盖问题
第十三章:单实例文件锁 var-run-xxxfile
第十五章:XSI IPC分别为消息队列,信号量,共享内存的优缺点:由系统控制,进程消亡,内存还在;在文件系统中没有名字,需要新系统调用支持,不能ls,rm,chmod,不得不增加ipcs ipcrm命令