ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

UNIX环境高级编程 第一章

2021-10-01 22:34:36  阅读:139  来源: 互联网

标签:err int 编程 pid 第一章 UNIX exit include buf


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

 

下面是通过调用调用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\n", 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\n", (long)getpid());
    printf("ppid = %ld\n", (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中的所有内容全部读进去,包括\n
        if (buf[strlen(buf) - 1] == '\n'){
            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\n", strerror(EACCES)); // 传入指定的错误值
    errno = ENOENT;     // 赋值系统error值为指定的错误值
    perror(argv[0]);
    
    exit(0);
}

 

1.8用户标识

#include "apue.h"



int main(void)
{
    printf("uid = %d, gid = %d\n", 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中的所有内容全部读进去,包括\n
        if (buf[strlen(buf) - 1] == '\n'){
            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\n%% ");
}

不是很理解,

 

1.10时间值

日历时间,记录用时间戳

进程时间:三个

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

 

标签:err,int,编程,pid,第一章,UNIX,exit,include,buf
来源: https://www.cnblogs.com/sidianok/p/15360330.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有