APUE
记录学习APUE过程中的知识点,例子,练习题。
ciky2011
这个作者很懒,什么都没留下…
展开
-
10_14.c打印signal mask 的函数。
其实现方式很简单,就是用sigismember函数与每个信号进行比较。一、源代码::cat -n 10_14.c 1 #include "apue.h" 2 #include 3 4 void pr_mask(const char *str) 5 { 6 int saved_errno;原创 2015-12-09 14:31:20 · 466 阅读 · 0 评论 -
8—31对每个命令进行计时,打印从tms中提取的值
一:注意头文件为,time后面有s。源代码:cat -n 8_31.c 1 #include "apue.h" 2 #include 3 4 5 static void pr_times(clock_t,struct tms *,struct tms *); 6 static void do_cmd(char原创 2015-11-03 10:22:42 · 355 阅读 · 0 评论 -
10_22 sigsuspend 函数实例
#include int sigsuspend(const sigset_t *sigmask);sigsuspend为原子操作,先解除原先的block信号,然后进程的信号屏蔽字设置为sigmask,然后挂起,当从sissuspend函数返回时在恢复为原先的mask。之所以存在sigsuspend函数是为了防止在解除mask和pause之间产生信号,而pause之后却再也得不到信号,导原创 2015-12-10 16:12:06 · 311 阅读 · 0 评论 -
防止中断后系统调用重启动的signal函数。
源代码:vim 10_19.c#include "apue.h"Sigfunc * signal_intr(int signo,Sigfunc *func){ struct sigaction act,oact; act.sa_handler = func; sigemptyset(&act.sa_mask);原创 2015-12-10 10:16:49 · 336 阅读 · 0 评论 -
用sigsetjmp和siglongjmp 解决从信号处理程序返回后信号仍被屏蔽问题
通过canjump变量提供一种保护机制,使得在jmpbuf尚未由sigsetjmp初始化时,防止调用信号处理函数。一、源代码::cat -n 10_14.c 10_20.c 1 #include "apue.h" 2 #include 3 4 void pr_mask(const char *str) 5 {原创 2015-12-10 13:49:06 · 499 阅读 · 0 评论 -
10_18 用sigaction实现signal函数
除非设置了SA_RESTART标志,否则sigaction函数不再重启动被中断的系统调用。一、源代码:vim 10_18.c#include "apue.h"Sigfunc * singnal(int signo,Sigfunc *func){ struct sigaction act,oact; act.sa_handler = func;原创 2015-12-10 09:31:29 · 330 阅读 · 0 评论 -
the implement of system function.
1. We should pay attention that : Line 14 and 15 in 8_22.c was not reached whether the cmdstring is valid or invalid.Because both of them will exit after the /bin/sh return.2.For while (waitpid(p原创 2015-10-28 13:39:32 · 453 阅读 · 0 评论 -
自动创建两个临时文件的函数,以及其创建的文件函数的默认属性
char * tmpnam(char *ptr);FILE *tmpfile(void);L_tmpnam 为传递给ptr的数组的最小长度。一、源代码 1 #include "apue.h" 2 #define MAXLINE 1024 3 int main() 4 { 5 char name[L_tmpnam]; 6原创 2015-09-25 10:58:35 · 352 阅读 · 1 评论 -
验证stderr stdout stdin 缓冲类型和缓冲区大小 以及在重定向后缓冲类型的改变
stdin 和stdout在重定向后缓冲类型有line buffered变为full buffered。大小因os而已。在SunOs上为1024.R*_*G:uname -aSunOS bldc 5.8 Generic_Virtual sun4u sparc SUNW,Netra-T12一、源代码: 1 #include "apue.h" 2 3 void pr原创 2015-09-24 15:58:30 · 2088 阅读 · 0 评论 -
8_25 设置用户id或组id的程序绝对不能再调用system函数。安全漏洞
vim 8_24.c#include "apue.h"int main(int argc, char *argv[]){ int status; if(argc err_quit("Please enter the command to be executed."); if ((statu原创 2015-10-28 14:35:02 · 582 阅读 · 0 评论 -
Print argv and environ with fork .
The comdination of 8_17 and 8_16The source code of 8_17.ccat -n 8_17.c 1 #include "apue.h" 2 3 int main(int argc, char * argv[]) 4 { 5 int i; 6原创 2015-10-27 11:21:41 · 275 阅读 · 0 评论 -
自己实现遍历文件层次结构程序
一源代码: 1 #include "apue.h" 2 #include 3 #include 4 #include "fig2.16" 5 6 typedef int Myfunc(const char *, const struct stat *, int); 7 8 static Myfunc myfunc; 9 static i原创 2015-09-24 13:45:46 · 462 阅读 · 0 评论 -
8_17echoall
:vim 8_17.c#include "apue.h"int main(int argc, char * argv[]){ int i; char **envp; extern char **environ; for(i = 0; i printf("argc[原创 2015-10-27 11:12:40 · 329 阅读 · 0 评论 -
8_16.c-->exec
只是取到了一个参数呀。shell下执行which and echo , They will display the results of two parameters.file /tmp 8_16.c/tmp: directory8_16.c: c program text 1 #include "apue.h" 2 #原创 2015-10-27 10:45:23 · 283 阅读 · 0 评论 -
8_20 文件解释器
1. 内核调用exec函数的进程实际执行的并不是该解释器文件,而是解释器文件中第一行指定的文件。2. 参数依次是 argv[0] 解释器的第一行文件名,argv[1] 解释器里可选参数,argv[2] 解释器文件 和execl第2个第3个参数。注意: 解释器文件里对参数个数 长度 都有限制 这里为参数个数限制为一个。 execl第一个参数不显示。cat -n 8_20.c原创 2015-10-27 16:00:52 · 281 阅读 · 0 评论 -
8_30调整nice的值 nice值越高 调用优先级越低。
注意:只有当return 值为-1 并且errno != 0 时 nice调整才出错。cat -n 8_30.c 1 #include "apue.h" 2 #include 3 #include 4 5 #if defined(MACOS) 6 #include 7 8 #eli原创 2015-11-02 15:48:24 · 904 阅读 · 0 评论 -
pthread_create函数 pthread_self 函数
源代码::cat -n 11_2.c 1 #include "apue.h" 2 #include 3 4 pthread_t ntid; 5 6 void printids(char *pstr) 7 { 8 pid_t pid; 9原创 2015-12-28 09:43:39 · 1482 阅读 · 0 评论 -
pthread_join
需要注意:所使用的存储返回信息的结构,在调用者完成调用以后,必须仍然有效。实例程序:#include "apue.h"#include void * thread1_fun(void *arg){ printf("Thread 1 is runing\n"); return ((void *) 1);}void原创 2015-12-30 11:02:14 · 348 阅读 · 0 评论 -
10_15.c先阻塞SIGQUIT信号,再释放。
当在阻塞阶段产生多个SIGQUIT信号时,当释放阻塞后也只catch到一个SIGQUIT信号。还原信号阻塞时最好用SIG_SETMASK。一、源代码:vim 10_15.c 1 #include "apue.h" 2 3 static void sig_quit(int); 4 5 int main() 6 { 7 sigset原创 2015-12-09 15:29:11 · 393 阅读 · 0 评论 -
10_12 sigddset sigdelset 和 sigismember 的实现
一、源代码:cat -n 10_12.c 1 #include 2 #include 3 4 #define SIGBAD(signo) ((signo) NSIG) 5 6 int sigaddset(sigset_t *set,int signo) 7 { 8原创 2015-12-09 13:35:28 · 684 阅读 · 1 评论 -
alarm的一种用法。
alarm函数可以用于防止读阻塞。但如果系统调用是自动重启动的,当从SIGALRM信号处理程序返回时,read并不被中断。在这种情形下,设置时间限制不起作用。一、源代码:cat -n 10_10.c 1 #include "apue.h" 2 3 static void sig_alarm(int sig_no); 4 5原创 2015-12-08 15:35:12 · 900 阅读 · 0 评论 -
10_8 有问题的sleep函数
sleep2函数调用龙jmp使得另外一个信号处理函数sig_int提早终止。一、源代码:cat -n 10_8.c 10_9.c 1 #include 2 #include 3 #include 4 5 static jmp_buf env_alrm; 6 7 static void sig原创 2015-12-08 10:39:07 · 346 阅读 · 0 评论 -
10_6检测子进程状态变化的处理函数。所以确实有这种子进程 错误
因为当已经进入信号处理函数里,说明在子进程里已经执行了_exit(0),在信号处理函数里注册信号SIGCLD的处理函数时,虽然会检测是否有需要的子进程,可是结果没有,因为已经执行_exit(0);所以不会死循环。书中错。一、源代码:cat -n 10_6.c 1 #include "apue.h" 2 #include 3 4 sta原创 2015-12-07 14:41:33 · 265 阅读 · 0 评论 -
10_5 在信号处理函数中调用非可重入函数getpwnam。
因为main函数和信号处理函数中都调用getpwnam函数,而在getpwnam函数中会调用malloc和free,如果主函数malloc之后收到SIGALRM信号进入信号处理函数,在信号处理函数里malloc然后free,而在信号处理函数调用free和main函数也在调用free时,malloc和free维护的数据结构就出现了损坏。所以在信号处理函数中调用非可重入函数,则其结果是不可预知的。原创 2015-12-07 11:13:37 · 419 阅读 · 0 评论 -
10_11alarm函数的作用修复系统自己重启。
使用这种方法无需担心,一个慢速的系统调用是否被中断。一、源代码: 1 #include "apue.h" 2 #include 3 4 static void sig_alarm(int sig_no); 5 static jmp_buf env_alrm; 6 int main() 7 {原创 2015-12-08 15:55:05 · 457 阅读 · 0 评论 -
自己写函数处理SIGTSTP信号
只有在当前shell(echo $SHELL)不忽略SIGTSTP信号时,才去调用自己写的handler。/bin/sh不支持。因为init进程三个进程控制信号SIGTSTP,SIGTTIN,SIGTTOU设置为SIG_IGN,支持者三个进程控制的shell重新设置为SIG_DFL.源代码:cat -n 10_31.c 1 #include "apue.h"原创 2015-12-23 11:05:13 · 1218 阅读 · 0 评论 -
利用设置 还原sigaction和sigprocmask方式实现alarm函数。
设计思路:先替换原先的处理函数然后通过newmask阻塞sigalrm,从susmask里除去sigalrm,然后利用sigsuspend函数捕捉sigalrm等未阻塞信号,最后获得剩余时间,重置handler和mask。源代码:cat -n 10_29.c 1 #include "apue.h" 2 3 void sig_alarm(int sig原创 2015-12-22 16:11:20 · 532 阅读 · 0 评论 -
10_26 当调用system函数时子进程与父进程对信号的处理,尤其是SIGCHLD
posix.1 标准规定,当在执行system函数时,应该阻止对父进程递送SIGCHLD信号。因为如果不阻止,当system创建的子进程结束时,system调用者可能错误的认为,它自己的一个子进程结束了,从而调用一种wait函数以获得子进程退出状态,进而阻止了system函数里获得子进程的终止状态,并将其返回。还因为system函数如果调用了交互式的应用,如本例的ed,其实父进程已经丧失了原创 2015-12-21 10:43:19 · 1054 阅读 · 0 评论 -
pthread_mutex
源代码:vim 11_10.c#include #include struct foo{ pthread_mutex_t mutex; int count; int foo_id;};struct foo * initial(int id){ struct foo *fp;原创 2016-01-04 19:17:15 · 341 阅读 · 0 评论 -
父进程与子进程有竞争条件,相互通信的函数的实现及应用实例
1.先来函数的实现。#include "apue.h"static volatile sig_atomic_t sigflag;static sigset_t newmask,emptymask,oldmask;static void sig_user(int signo){ sigflag = 1;}void原创 2015-12-18 10:38:08 · 496 阅读 · 0 评论 -
使用sigsuspend函数捕捉到信号改变变量的值。
期待的效果是,./jump_out_while^Cinterrupt^Cinterrupt^Cinterrupt^Cinterrupt^\catch SIGQUIT然而是确实这样:(知道到为什么只能只是一次调用自己的handler函数。)R*_*G:./jump_out_while^Cinterrupt^\catch SIGQUITR*_*G:./j原创 2015-12-17 16:45:56 · 283 阅读 · 0 评论 -
线程的清理函数:pthread_cleanup_push / pthread_cleanup_pop
pthread_cleanup_push 为塞进即注册一个清理函数pthread_cleanup_pop 为弹出即取消一个清理函数执行顺序与注册顺序相反。1.源代码:cat -n 11_5.c 1 #include "apue.h" 2 #include 3 4 void cleanup(void *s) 5原创 2015-12-31 11:49:08 · 650 阅读 · 0 评论 -
8_21_awkexample
执行时一定要加上-f 或者在开头加上 #! /bin/awk -f要不回出错。BEGIN { for ( i = 0; i printf "ARGV[%d] = %s\n", i, ARGV[i] exit}awk -f 8_21_awkexample原创 2015-10-27 16:44:48 · 291 阅读 · 0 评论 -
8_1 当fork遇上printf
如果我们把输出重定向到文件,因为printf带缓冲区因此当子进程复制父进程的数据空间时,将该缓冲区也复制过来。因此有两个before fork。一 源代码: 1 #include "apue.h" 2 3 int grobval = 7; 4 char buf[] = "write to stdout\n"; 5原创 2015-10-12 17:01:08 · 277 阅读 · 0 评论 -
vfork 与fork 区别
1.vfork并不将父进程地址空间完全复制到子进程,在exec和exit之前在父进程的空间里运行。2.vfork保证子进程先运行,在它调用exec或exit之后父进程才被调用。验证的原代码:#include "apue.h"int grobval = 7;int main(){ int val = 90; pid_原创 2015-10-12 17:56:52 · 366 阅读 · 0 评论 -
解除文件的链接
一、源代码:#include "apue.h"#include int main(){ if( open("tempfile", O_RDWR) err_sys("open error"); if( unlink("tempfile") err_sys("unlink原创 2015-09-16 16:54:23 · 383 阅读 · 0 评论 -
实战chmod函数 fchmod fchmodat
1 #include "apue.h" 2 #include 3 4 int main() 5 { 6 struct stat statbuf; 7 if( stat("without_umask",&statbuf) 8 err_sys("stat error"); 9 10原创 2015-09-14 16:27:43 · 556 阅读 · 3 评论 -
6_11:strftime 打印系统时间
一.源代码:R*_*G:cat -n 6_11.c 1 #include 2 #include 3 #include 4 5 int main() 6 { 7 time_t t; 8 struct tm *tmp; 9原创 2015-09-30 13:09:53 · 298 阅读 · 1 评论 -
打印文件类型
一、源代码:R*_*G:vim 4_3.c 1 #include "apue.h" 2 #include 1 #include "apue.h" 2 #include 3 int main(int argc, char *argv[]) 4 { 5 int i; 6 char *ptr; 7原创 2015-09-11 13:41:29 · 550 阅读 · 0 评论 -
创建一个空洞文件
一、源代码 1 #include "apue.h" 2 #include 3 4 int main() 5 { 6 char buf1[] = "abcdefj"; 7 char buf2[] = "ABCDEFJ"; 8 int fd; 9 if ( fd = creat (原创 2015-09-06 16:41:34 · 640 阅读 · 1 评论