UNIX环境高级编程 第一章

代码笔记,仅供自己学习使用。

下面是通过调用调用dirent的系统库实现,查看目录下内容的模块

#include "apue.h"
#include <dirent.h>

int main(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
", dirp->d_name);
    }
    closedir(dp);
    exit(0);
}

书中说明了每个进程都有一个工作目录。

1.5输入和输出

#include "apue.h"

#define  BUFFSIZE 4096

int
main(void)
{
    int n;
    char buf[BUFFSIZE];
    /*通过系统的unistd.h的头文件, 不带缓冲的io进行操作.读到文件的末端返回0*/
    while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
        if (write(STDIN_FILENO, buf, n) != n) {
            err_sys("write error");
        }
    }
    
    if (n < 0) {
        err_sys("read error");
    }
    
    exit(0);
    
}

下面是通过表示stdio进行输入输出的操作,这里不需要自己手动设置缓冲区。

#include "apue.h"

int
main(void){
    int c;
    while ((c = getchar()) != EOF) {
        if (putc(c, stdout) == EOF) {
            err_sys("output error");
        }
    }
    
    // 读写模式不匹配报这个错误
    if (ferror(stdin)) {
        err_sys("input error");
    }
    
    exit(0);
    
}

1.6 程序与进程

程序(program)是一个存储在磁盘上莫个目录中的可执行,程序的执行实例被称为进程(process),每个进程独有一个进程id

#include "apue.h"

int main(void)
{
    printf("hello world from process ID %ld
", (long)getpid());
    printf("ppid = %ld
", (long)getppid());
    exit(0);
}

模拟程序从标准输入读入并执行命令。

#include "apue.h"
#include <sys/wait.h>



int main(void)
{
    
    char  buf[MAXLINE];
    pid_t pid;
    int status;
    
    printf("%% ");  // 等待接收命令的提示
    while (fgets(buf, MAXLINE, stdin) != NULL) {     //fgets会将std中的所有内容全部读进去,包括

        if (buf[strlen(buf) - 1] == '
'){
            buf[strlen(buf) - 1] = 0;    /*  加入读取有换行符去除换行符 */
        }
        if ((pid = fork()) < 0) {     /* 这里将产生一个pid为0的子进程   */
            err_sys("fork error");
        }else if(pid == 0){   // 子进程在这里执行
            execlp(buf, buf, (char*)0);
            err_ret("couldn't execute: %s", buf);
            exit(127);
        }
        
        if ((pid = waitpid(pid, &status, 0)) < 0) {
            err_sys("waitpid error");
        }
        printf("%% ");
    }
    exit(0);
}

1.7出错处理

#include "apue.h"
#include <errno.h>



int main(int argc, char *argv[])
{
    
    fprintf(stderr, "EACCES: %s
", strerror(EACCES)); // 传入指定的错误值
    errno = ENOENT;     // 赋值系统error值为指定的错误值
    perror(argv[0]);
    
    exit(0);
}

1.8用户标识

#include "apue.h"



int main(void)
{
    printf("uid = %d, gid = %d
", getuid(), getgid())
    
    exit(0);
}

1.9信号

#include "apue.h"
#include <sys/wait.h>

static void sig_int(int);


int main(void)
{
    
    char  buf[MAXLINE];
    pid_t pid;
    int status;
    
    if (signal(SIGINT, sig_int) == SIG_ERR) {
        err_sys("signal error");
    }
    
    printf("%% ");  // 等待接收命令的提示
    while (fgets(buf, MAXLINE, stdin) != NULL) {     //fgets会将std中的所有内容全部读进去,包括

        if (buf[strlen(buf) - 1] == '
'){
            buf[strlen(buf) - 1] = 0;    /*  加入读取有换行符去除换行符 */
        }
        if ((pid = fork()) < 0) {     /* 这里将产生一个pid为0的子进程   */
            err_sys("fork error");
        }else if(pid == 0){   // 子进程在这里执行
            execlp(buf, buf, (char*)0);
            err_ret("couldn't execute: %s", buf);
            exit(127);
        }
        
        if ((pid = waitpid(pid, &status, 0)) < 0) {
            err_sys("waitpid error");
        }
        printf("%% ");
    }
    exit(0);
}

void
sig_int(int signo){
    printf("interrupt
%% ");
}

不是很理解,

1.10时间值

日历时间,记录用时间戳

进程时间:三个

时钟时间,用户时间,系统CPU时间, 用time函数可以查看。

原文地址:https://www.cnblogs.com/sidianok/p/15360330.html