冬休み学習13,14日目(linuxプレミアムプログラミング)

4371 ワード

冬休み学習13,14日目(linuxプレミアムプログラミング)ノートまとめ
一、IOとファイルディレクトリ管理
1.preadとlseek+readの違い
pread読み取り後も読み書き位置は変わりません
2.mmapマッピング
/proc/${pid}/memはマッピングできません.mmap関数の最後のパラメータファイルのマッピング開始位置はpagesizeの証明書の倍でなければなりません.No者エラー
3.IOの実ユーザ(real user)と有効ユーザ(effective user)
デフォルト:有効なユーザーは実際のユーザーと一致しますが、設定ビットが設定されていると異なる場合があります.
≪実績ユーザー|Actual User|ldap≫:≪実行ユーザー|Execute User|ldap≫
≪有効なユーザー|Valid Users|ldap≫:≪権限ユーザー|Privileges Users|ldap≫
有効なユーザーと実際のユーザーの取得
        uid_t getuid(void);//実ユーザーuid_t geteuid(void);//有効ユーザー
4.ディレクトリ関連の関数
     int chdir(const char *path);//目次の切り替え
     int mkdir(const char *pathname, mode_t mode);//ディレクトリの作成
     int rmdir(const char *pathname);//ディレクトリの削除
     int unlink(const char *pathname);//ファイルの削除
     mode_t umask(mode_t mask);//ファイル権限シールドビットの設定
     int stat(const char *path, struct stat *buf);       int fstat(int fd, struct stat *buf);//ファイルディレクトリステータスの取得
5.カタログの便利さ
DIR *opendir(const char *name);//ファイルディレクトリを開き、DIRはファイルディレクトリへのポインタ
struct dirent *readdir(DIR *dirp);//ファイルディレクトリの読み込み
struct dirent {                ino_t          d_ino;      /* inode number */               off_t          d_off;      /* offset to the next dirent */               unsigned short d_reclen;    /* length of this record */               unsigned char  d_type;      /* type of file; not supported                                               by all file system types */               char           d_name[256];/* filename */           };
int closedir(DIR *dirp);//ファイルディレクトリを閉じる
void seekdir(DIR *dirp, long offset);
int dirfd(DIR *dirp);
int scandir(const char*dirp,//ディレクトリ名
struct dirent**namelist,//ディレクトリリストに戻る
int(*filter)(const struct dirent*)、//コールバック関数は、ディレクトリをフィルタリングするために使用され、NULLはint(*compar)をフィルタしないことを表す.//ソート関数
//戻るディレクトリの数
例:
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char *argv[])
{
    DIR *d;
    struct dirent *pdir;
    d = opendir("../day12");
    if(d==NULL) printf("opendir error:%m
"),exit(-1); while(pdir=readdir(d)) { printf("%s\t%d
",pdir->d_name,pdir->d_type); } closedir(d); return 0; }
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>

int myfilter(const struct dirent*d)   //       
{
    if(memcmp(d->d_name,".",1)==0){
        return 0;
    }else{
        return -1;
    }
}

int main(int argc, const char *argv[])
{
    struct dirent** d;
    int r;
    int i;
    r=scandir("/test",&d,myfilter,alphasort);//myfilter         ,alphasort         ,
    printf("%d
",r); for(i=0;i<r;++i){ printf("%s
",d[i]->d_name); } return 0; }

二、プロセス
1.プロセスとは
実行するプログラム:必ずコード->メモリ、ファイルなどのリソース->CPUなどを実行できます.
プロセスには多くのデータ・メンテナンスがあります:プロセス・ステータス/プロセスのプロパティ
すべてのプロセス・プロパティは構造体メンテナンスを使用します->実際にはツリー・データ構造です
2.プロセスの作成
       1.コード?メモリにロードしますか?CPUタイムスライスを割り当てますか?
       2.プロセス関連の作成関数
               iint system(const char *command);
独立したプロセスを確立し、独立したコード空間、メモリ空間を持つ.新しいプロセスの実行が完了するまでシステムが戻る(ブロック)
戻り値はプロセスの戻り値に関係し、systemの戻り値のうち8〜15ビットに戻り値が格納される.
上記のスレッドの戻り値は255を超えないでください.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
    int r;
    printf("%d
",getpid()); r=system("./test"); //printf("%d
",r>>8); // printf("%d
",WEXITSTATUS(r)); return 0; }

               FILE *popen(const char *command, const char *type);
プロセスの作成;親子プロセス間のパイプの作成
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
    char buf[1024];
    FILE *f=popen("ls -l","r");
    int fd=fileno(f);

    int r;
    while((r=read(fd,buf,1024))>0)
    {
        buf[r]=0;
        printf("%s",buf);
    }
    close(fd);
    pclose(f);
    return 0;
}

execシリーズ関数
 fork