APUE学习(一)基础知识
代碼段一:
#include "apue.h"#include <dirent.h>intmain(int argc, char *argv[]){DIR?????????????*dp;struct dirent???*dirp;
if (argc != 2)err_quit("usage: ls directory_name");
if ((dp = opendir(argv[1])) == NULL)err_sys("can't open %s", argv[1]);while ((dirp = readdir(dp)) != NULL)printf("%s\n", dirp->d_name);
closedir(dp);exit(0);}
make后在../lib生成libapue.a,然后
gcc ls1.c -L ../lib -lapue -I ../include/連接庫(kù)和頭文件,可以模擬ls命令, man 1 ls //查看命令都是man 1exit()通常用0表示正常終止
代碼段二:
#include "apue.h"#define BUFFSIZE 4096
int main(void){ ssize_t n; char buf[BUFFSIZE]; while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0){ if(write(STDOUT_FILENO,buf,n)!=n){ err_sys("write error");}} if(n<0){ err_sys("read error");} return 0;}
gcc iotest.c -L ../lib -lapue -I ../include/
輸入什么就得到什么輸出
stdin,stdout,stderr是行緩沖的,回車(chē)刷新,可以使用>或>>重定向到文件
系統(tǒng)io是不帶緩沖的,例如read,write,lseek,open,close
標(biāo)準(zhǔn)io是帶緩沖的,例如fprint,fread,fwrite
代碼段三:
#include "apue.h"intmain(void){int?????c;
while ((c = getc(stdin)) != EOF)if (putc(c, stdout) == EOF)err_sys("output error");
if (ferror(stdin))err_sys("input error");
exit(0);}
效果同上
代碼段4
#include "apue.h"intmain(void){printf("hello world from process ID %ld,parent process ID %ld\n", (long)getpid(),(long)(getppid()));exit(0);}
運(yùn)行
sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ gcc hello.c -L ../lib/ -lapue -I ../include/ sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ ./a.out hello world from process ID 13135,parent process ID 22719 sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ ps -aux |grep 22719 sun 13151 0.0 0.0 14540 1044 pts/0 S+ 19:14 0:00 grep 22719 sun 22719 0.0 0.0 22940 4760 pts/0 Ss 16:47 0:00 /bin/bash代碼段5
#include "apue.h"#include <sys/wait.h>intmain(void){char????buf[MAXLINE];???/* from apue.h */pid_t???pid;int?????status;
printf("%% ");??/* print prompt (printf requires %% to print %) */while (fgets(buf, MAXLINE, stdin) != NULL) {if (buf[strlen(buf) - 1] == '\n')buf[strlen(buf) - 1] = 0; /* replace newline with null */
if ((pid = fork()) < 0) {err_sys("fork error");} else if (pid == 0) {??????/* child */execlp(buf, buf, (char *)0);err_ret("couldn't execute: %s", buf);exit(127);}
/* parent */if ((pid = waitpid(pid, &status, 0)) < 0)err_sys("waitpid error");printf("%% ");}exit(0);}
運(yùn)行:
sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ ./a.out % date Tue May 1 19:46:56 CST 2018 % pwd /home/sun/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro % abcde couldn't execute: abcde: No such file or directory % sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ extern int execlp (const char *__file, const char *__arg, ...)execlp()會(huì)從PATH 環(huán)境變量所指的目錄中查找符合參數(shù)file的文件名, 找到后便執(zhí)行該文件, 然后將第二個(gè)以后的參數(shù)當(dāng)做該文件的argv[0]、argv[1]……, 最后一個(gè)參數(shù)必須用空指針(NULL)作結(jié)束,例如
execlp("ls","ls","-al","/zhmc",(char *)0);
waitpid(pid_t pid,int* status,int option);阻塞等待進(jìn)程號(hào)為pid的子線程結(jié)束,并回收進(jìn)程號(hào)等資源
代碼段6
#include "apue.h"#include <errno.h>
intmain(int argc, char *argv[]){fprintf(stderr, "EACCES: %s\n", strerror(EACCES));errno = ENOENT;perror(argv[0]);exit(0);}
運(yùn)行
sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ gcc testerror.c -L ../lib/ -lapue -I ../include/ sun@sun-PC:~/Downloads/linux/UNIX環(huán)境高級(jí)編程.tar/apue.3e/intro$ ./a.out EACCES: Permission denied ./a.out: No such file or directoryint?fprintf (FILE*?stream,?const char*format, [argument])
char*? strerror(int erorno);
void perror(char*);
代碼段7
#include <stdio.h> #include <unistd.h> #include <sys/types.h>int main(void) {uid_t uid=getuid();gid_t gid=getgid();printf("uid:%d,gid:%d\n",(int)uid,(int)gid);return 0; }運(yùn)行:
[root@localhost apue]# ./a.out uid:0,gid:0 [root@localhost apue]# su sun [sun@localhost apue]$ ./a.out uid:500,gid:500 代碼段8#include "apue.h"#include <sys/wait.h>
static void?sig_int(int);???????/* our signal-catching function */
intmain(void){char????buf[MAXLINE];???/* from apue.h */pid_t???pid;int?????status;
if (signal(SIGINT, sig_int) == SIG_ERR)err_sys("signal error");
printf("%% ");??/* print prompt (printf requires %% to print %) */while (fgets(buf, MAXLINE, stdin) != NULL) {if (buf[strlen(buf) - 1] == '\n')buf[strlen(buf) - 1] = 0; /* replace newline with null */
if ((pid = fork()) < 0) {err_sys("fork error");} else if (pid == 0) {??????/* child */execlp(buf, buf, (char *)0);err_ret("couldn't execute: %s", buf);exit(127);}
/* parent */if ((pid = waitpid(pid, &status, 0)) < 0)err_sys("waitpid error");printf("%% ");}exit(0);}
voidsig_int(int signo){printf("interrupt\n%% ");}
其中signal()如下
#include "apue.h"/* Reliable version of signal(), using POSIX sigaction(). */Sigfunc *signal(int signo, Sigfunc *func){struct sigaction????act, oact;
act.sa_handler = func;sigemptyset(&act.sa_mask);act.sa_flags = 0;if (signo == SIGALRM) {#ifdef??SA_INTERRUPTact.sa_flags |= SA_INTERRUPT;#endif} else {act.sa_flags |= SA_RESTART;}if (sigaction(signo, &act, &oact) < 0)return(SIG_ERR);return(oact.sa_handler);}
信號(hào)詳說(shuō)
總結(jié)
以上是生活随笔為你收集整理的APUE学习(一)基础知识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ArcGIS基于爬虫数据绘制人口分布密度
- 下一篇: 网红王思聪数字时钟动态壁纸【电脑壁纸】