wait4プログラム

wait3に引数pidが増えただけです。

#include < sys/types.h >
#include < sys/time.h >
#include < sys/resource.h >
#include < sys/wait.h >

#include < unistd.h >
#include < stdio.h >

int
main()
{
    int status;
    pid_t pid;
    struct rusage usage;
    
    if ((pid = fork()) < 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "chlid process\n", 14);
        _exit(12);
    }
    write(1, "parent process\n", 15);
    
    if ((pid = wait4(pid, status, 0, &usage)) < 0){
        perror("wait4");
        return 1;
    }
    if(WIFEXITED(status)){
        printf(
        "pid = %d exited with status = %d\n",
               (int)pid, WEXITSTATUS(status));
    }
    
    printf(
    "user time = %ld.%06ld\n"
    "system time = %ld.%06ld\n",
    usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
           usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
    return 0;
}

wait3プログラム

#include < sys/types.h >
#include < sys/time.h >
#include < sys/resource.h >
#include < sys/wait.h >

#include < unistd.h >
#include < stdio.h >

int main()
{
    int status;
    pid_t pid;
    struct rusage usage;
    
    if ((pid = fork())< 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "child process\n", 14);
        _exit(12);
    }
    write(1, "parent process\n", 15);
    
    if ((pid = wait3(&status, 0, &usage)) < 0) {
        perror("wait3");
        return 1;
    }
        if(WIFEXITED(status)){
            printf(
            "pid=%d exited with status = %d\n",
                   (int)pid, WEXITSTATUS(status));
        }
    printf(
    "user time = %ld.%06ld\n"
           "system time = %ld.%06ld\n",
           usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
           usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
    return 0;
        
}

wait pid

#include < sys/types.h >
#include < sys/wait.h >

#include < unistd.h >
#include < stdio.h >

int mian()
{
    int status;
    pid_t pid;
    
    if ((pid = fork()) < 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "child process\n", 14);
        _exit(12);
    }
    write(1, "parent process\n", 15);
    
    if((pid = waitpid(pid, &status, 0)) < 0){
        perror("waitpid");
        return 1;
    }
    if(WIFEXITED(status)){
        printf(
        "pid = %d exited with status = %d\n",
               (int)pid, WEXITSTATUS(status));
    }
    return 0;
}

execveファイル

#include < unistd.h >

#include < sys/types.h >
#include < stdio.h >

int
main()
{
    pid_t pid;
    char *argv[3];
    extern char **environ;
    
    if((pid = fork())< 0){
        perror("fork");
        return 1;
    } else if(pid == 0){
        argv[0]="echo";
        argv[1]="Hello, from child";
        argv[2]= NULL;
        execve("/bin/echo", argv, environ);
        _exit(1);
    }
    write(1, "Hello, from parent\n", 19);
    return 0;
}

forkによる新しい子プロセスの作成

新しい子プロセスの作成には、forkを使います。

#include < sys/types.h >
#include < unistd.h >

#include 

int
main()
{
    pid_t pid;
    
    if ((pid = fork()) < 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "child process\n", 14);
        _exit(0);
    }
    write(1, "parent process\n", 15);
    return 0;
}

ハッシュサーチ

ハッシュサーチは検索は圧倒的に高速です。データをテーブルに格納する際に簡単な式でキーを割り当て、そのキーでダイレクトに飛べる場所にデータを格納しておきます。

#include 
#include 
#include 

#define HASH_SIZE 4096

static char **Str[HASH_SIZE];
static int Strmax[HASH_SIZE];

int strno(char *str)
{
    static char *null="";
    char *p,*ptr,*ptrc;
    int i;
    unsigned n;
    
    if(str==NULL){
        ptr=null;
    }
    else{
        ptr=str;
    }
    
    n=0;
    for(ptrc=ptr;(*ptrc)!='\0';ptrc++){
        n+=(unsigned)(*ptrc);
    }
    n%=HASH_SIZE;
    for(i=0;i
	

バイナリサーチ

バイナリサーチは条件として、データが昇順にソートされている必要があります。ソートは一般的に検索よりも時間がかかります。

#include < stdio.h >
#include < search.h >

int cmp_func(int *a,int *b)
{
    return((*a)-(*b));
}

void main()
{
    static int ary[10]={2,4,1,3,5,7,9,6,8,0};
    static int target=5;
    int *ptr;
    
    qsort(ary,10,sizeof(int),cmp_func);
    
    ptr=(int *)bsearch(&target,ary,10,sizeof(int),cmp=func);
    if(ptr!=NULL){
        printf("target is ary[%d]\n",ptr-ary);
    }
    else{
        printf("target not found\n");
    }
}

線形サーチ

forループで回りながら比較をします。

#include < stdio.h >

void main()
{
    static int ary[10]={2,4,1,3,5,7,9,6,8,0};
    static int target=5;
    int i,no;
    
    no = -1;
    for(i=0;i<10;i++){
        if(ary[i]==target){
            no=i;
            break;
        }
    }
    
    if(no>=0){
        printf("target is ary[%d]\n",no);
    }
    else{
        printf("target not found\n");
    }
}

popenとpclose

#include < stdio.h >
#include < signal.h >
#include < errno.h >

#define R (0)
#define W (1)

static struct pid {
    struct pid *next;
    FILE *fp;
    pid_t pid;
} *pidlist;

FILE *popen_err(char *command,char *option)
{
    struct pid *cur;
    int pipe_c2p_e[2];
    int pid;
    FILE *fp;
    
    if ((*option != 'r') || option[1]){
        fprintf(stderr,"popen_err():option error:return(NULL)\n");
        return (NULL);
    }
    if ((cur = (struct pid *)malloc(sizeof(struct pid))) == NULL){
        fprintf(stderr,"popen_err():malloc error:return(NULL)\n");
        return (NULL);
    }
    if(piep(pipe_c2p_e)<0){
        perror("pipe");
        return(NULL);
    }
    if((pid=fork())<0){
        perror("fork");
        close(pipe_c2p_e[R]);
        close(pipe_c2p_e[W]);
        return(NULL);
    }
    if(pid==0){
        close(pipe_c2p_e[R]);
        dup2(pipe_c2p_e[W],2);
        close(pipe_c2p_e[W]);
        execlp("sh","sh","-c",command,NULL);
        _exit(127);
    }
    close(pipe_c2p_e[W]);
    fp=fdopen(pipe_c2p_e[R],option);
    cur->fp = fp;
    cur->pid = pid;
    cur->next = pidlist;
    pidlist = cur;
    
    return(fp);
}

int pclose_err(FILE *fp)
{
    register struct pid *cur, *last;
#if BSD /* for */
    int omask;
#else /* SYSV */
    sigset_t set,omask;
#endif
    
    int pstat;
    pid_t pid;
    extern int errno;
    
    fclose(fp);
    for(last = NULL, cur = pidlist; cur; last = cur, cur = cur->next){
        if (cur->fp == fp){
            break;
        }
    }
    if (cur == NULL){
        return (-1);
    }
#if BSD /* for BSD */
    omask = sigblock(sigmask(SIGINT|sigmask(SIGQUIT)|sigmask(SIGHUP));
                     do {
                         pid = waitpid(cur->pid, (int *) &pstat, 0);
                     } while (pid == -1 && errno == EINTER);
                     (void)sigsetmask(omask);
}
#else /* SYSV */
                     sigemptyset(&set);
                     sigaddset(&set,SIGINT);
                     sigaddset(&set,SIGQUIT);
                     sigaddset(&set,SIGHUP);
                     sigprocmask(SIG_SETMASK,&set,&omask);
                     do {
                         pid = waitpid(cur->pid, (int *)&pstat, 0);
                     } while (pid == -1 && errno == EINTR);
                     sigprocmask(SIG_SETMASK, &omask,NULL);
#endif
                     if (last == NULL){
                         pidlist = cur->next;
                     }
                     else {
                         last->next = cur->next;
                     }
                     free(cur);

                     return (pid == -1 ? -1 : pstat);
                     }