UNIX环境高级编程 第一章

摘要:
下面是通过调用调用dirent的系统库实现,查看目录下内容的模块#include"apue.h"#includeintmain{DIR*dp;structdirent*dirp;if(argc!=NULL){printf;}closedir;exit;}书中说明了每个进程都有一个工作目录。=NULL){//fgets会将std中的所有内容全部读进去,包括if{buf[strlen-1]=0;/*加入读取有换行符去除换行符*/}if{/*这里将产生一个pid为0的子进程*/err_sys;}elseif{//子进程在这里执行execlp;err_ret;exit;}if{err_sys;}printf;}exit;}1.7出错处理#include"apue.h"#includeintmain{fprintf;//传入指定的错误值errno=ENOENT;//赋值系统error值为指定的错误值perror;exit;}1.8用户标识#include"apue.h"intmain{printfexit;}1.9信号#include"apue.h"#includestaticvoidsig_int;intmain{charbuf[MAXLINE];pid_tpid;intstatus;if{err_sys;}printf;//等待接收命令的提示while(fgets(buf,MAXLINE,stdin)!

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

下面是通过调用调用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

intmain(void)
{
    intn;
    charbuf[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"

intmain(void){
    intc;
    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)
{
    
    charbuf[MAXLINE];
    pid_t pid;
    intstatus;
    
    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)
{
    
    charbuf[MAXLINE];
    pid_t pid;
    intstatus;
    
    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);
}

voidsig_int(intsigno){
    printf("interrupt
%% ");
}

不是很理解,

1.10时间值

日历时间,记录用时间戳

进程时间:三个

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

免责声明:文章转载自《UNIX环境高级编程 第一章》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[转]Mac Port 基本用法总结何为硬件设计?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

libubox

lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理。 它的目的是以动态链接库方式来提供可重用的通用功能,给其他模块提供便利和避免再造轮子。 libubox主要提供三部分功能: 1)提供多种基础通用功能接口,包含链表,平衡二叉树,二进制处理,key-value链表,M...

《Unix/Linux系统编程》第八章学习笔记

第八章 使用系统调用进行文件操作 8.1系统调用 在操作系统中,进程以两种不同的模式运行,即内核模式和用户模式,简称Kmode和 Umode。在Umode中,进程的权限非常有限。它不能执行任何需要特殊权限的操作。特殊权限的操作必须在Kmode下执行。系统调用(简称syscall)是一种允许进程进入Kmode以执行Umode不允许操作的机制。复刻子进程、修改...

rootkit:实现隐藏进程

实现隐藏进程一般有两个方法: 1,把要隐藏的进程PID设置为0,因为系统默认是不显示PID为0的进程。 2,修改系统调用sys_getdents()。 Linux系统中用来查询文件信息的系统调用是sys_getdents,这一点可以通过strace来观察到,例如strace ls 将列出命令ls用到的系统调用,从中可以发现ls是通过getdents系统调用...

lnmp使用socket方式连接nginx优化php-fpm性能

lnmp使用socket方式连接nginx优化php-fpm性能 Nginx连接fastcgi的方式有2种:TCP和unix domain socket 什么是Unix domain socket?—— 维基百科 Unix domain socket 或者 IPC socket是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。与管道相比,Un...

Qt 访问网络

一、前言 Qt 中访问网络使用 QNetworkAccessManager,它的 API 是异步的,这样在访问网络的时候不需要启动一个线程,在线程里执行请求的代码。(但这一点在有时候需要阻塞时就是个麻烦了) 需要注意一点的是,请求响应的对象 QNetworkReply 需要我们自己手动的删除,一般都会在 QNetworkAccessManager::fin...

vs2010驱动开发环境配置

1、文件 -> 新建 -> 项目 -> Visual C++ -> 空项目 名称:Driver 2、生成 -> 配置管理器   活动解决方案配置: 新建 名称:Driver Debug 从此处复制设置:Debug 3、视图 -> 属性管理器  展开刚配置的Driver Debug | Win32  ->  右...