linux programming

来源:互联网 发布:mysql从入门到精通 pdf 编辑:程序博客网 时间:2024/05/20 16:35


APUE上的一个例子,实现的ls的功能


#include<sys/types.h>#include <dirent.h>#include <stdio.h>int main(int argc,char *argv[]){        DIR *dp;        struct dirent *dirp;        if(argc!=2){                printf("not enough argument\n");                return 0;        }        if((dp=(opendir(argv[1])))==NULL)                printf("not a directory\n");        while((dirp=readdir(dp))!=NULL)                printf("%s\n",dirp->d_name);        closedir(dp);        return 0;}

 

 


 


#include<unistd.h>#include<stdio.h>#define BUFFSIZE 4096int main(void){        int n;        char buf[BUFFSIZE];        while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0 )                if(write(STDOUT_FILENO,buf,n)!=n)                        printf("write error\n");        if(n<0)                printf("read error\n");        exit(0);}

该段程序完成功能是从标准输入中输入的字符存到buf中,然后把buf中的内容输出到标准输出中显示出来

The file descriptor for standard input is 0 (zero); the POSIX <unistd.h> definition is STDIN_FILENO; the corresponding <stdio.h> variable is FILE* stdin.

STDIN_FILENO是int类型的

 

 

 

#include<unistd.h>#include<stdio.h>int main(void){        printf("hello world from process ID %d\n",getpid());        exit(0);}~


getpid() returns the process ID of the calling process. (This is often used by routines that generate unique temporary filenames.)getppid() returns the process ID of the parent of the calling process.



#include<sys/wait.h>#include<unistd.h>#include<stdio.h>#define MAXLINE 4096int main(){        char buf[MAXLINE];        pid_t pid;        int status;        printf("%% ");        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){                        printf("fork error\n");                }else if(pid==0){    /*child process */                        execlp(buf,buf,(char *)0);                        exit(127);                }                /*parent*/                if((pid=waitpid(pid,&status,0))<0)                        printf("waitpid error\n");                printf("%% ");        }        exit(0);}

写代码的时候注释符号*与/之间隔了一个空格,一直找不到错误,结果过了很久才找到

char *fgets(char *s, int size, FILE *stream)

fgets() reads in at most one less than size characters fromstream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.


fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.


int execlp(const char *file, const char *arg, ...);

The exec() family of functions replaces the current process image with a new process image.

The const char *arg and subsequent ellipses(省略号) in the execl(), execlp(), and execle() functions can be thought of as arg0arg1, ..., argn.Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program.The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.



_POSIX_C_SOURCE
              Defining this macro causes header files to expose definitions as
              follows:
              The value 1 exposes definitions conforming to POSIX.1-1990 and ISO C
                 (1990).

  _POSIX_SOURCE
              Defining this obsolete macro with any value is equivalent to defining
              _POSIX_C_SOURCE with the value 1.