且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux c学习--进程基础学习

更新时间:2022-08-14 07:47:23

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(int argc, char const *argv[])
{
    printf("I am process %ld\n", (long)getpid() );
    printf("My parent is %ld\n", (long)getppid());
    return 0;
}

 上面的例子输出了进程的ID和他的父进程的ID

#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
    printf("my real user id is %ld\n", (long)getuid());
    printf("my effective user id is %ld\n",(long)geteuid());
    printf("My real group id is %ld\n", (long)getgid());
    printf("My effective group id is %ld\n",(long)getegid() );
    return 0;
}
/*
this program run's result as follows :
my real user id is 1000
my effective user id is 1000
My real group id is 1000
My effective group id is 1000
*/

 上面的这个程序打印进程的各种ID和组ID

#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
    int x;
    x=0;
    fork();
    printf("I am process %ld and my x is %d\n", (long)getpid() ,x);
    return 0;
}
 
/*
this program run's result as follows:
 
I am process 5416 and my x is 0
I am process 5417 and my x is 0
*/<br><br>简单的fork的例子
#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
    pid_t childpid;
 
    childpid=fork();
    if(childpid== -1){
        perror("Failed to fork\n");
        return 1;
    }
 
    if(childpid==0){
        printf("I am child %ld\n", (long)getpid());
    }else{
        printf("I am parent %ld\n",(long)getpid() );
    }
    return 0;
}
/*
this program run's result as follows:
I am parent 5467
I am child 5468
 
*/

 上面的例子输出了执行fork之后,父进程和子进程分别输出自身的ID

#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
    pid_t mypid;
    pid_t childpid;
 
    mypid=getpid();
    childpid=fork();
    if(childpid==-1){
        perror("Failed to frok\n");
        return 1;
    }
 
    if(childpid==0){
        // child codes
        printf("I am child %ld, ID= %ld\n",(long)getpid(),(long)mypid);
    }else{
        // parent codes
        printf("I am parent  %ld, ID=%ld\n",(long)getpid(),(long)mypid );
    }
    return 0;
}
 
/*
this program run's result as follows:
 
I am parent  5517, ID=5517
I am child 5518, ID= 5517
 
*/

 父进程在调用fork之前,将mypid设置为自己的进程ID,执行fork之后,子进程获得了包含所有变量在内的父进程的地址空间的一份拷贝,由于子进程没有重置mypid,所以打印出的指不一样。

 

#include <stdio.h>
#include <unistd.h>
 
int main(int argc, char const *argv[])
{
    pid_t childpid=0;
    int i,n;
 
    /*check for vaild number of common-line arguments*/
    if(argc!=2){
        fprintf(stderr, "usage: %s process \n", argv[0] );
        return 1;
    }
 
    n=atoi(argv[1]);
    for ( i = 0; i < n; ++i)
    {
        if(childpid=fork()){
            break;
        }
 
        fprintf(stderr, "i:%d process ID: %ld parent ID: %ld child ID: %ld\n", i,
            (long)getpid(),(long)getppid(),(long)childpid);
    }
    return 0;
}
 
/*
this program run's result as follows:
 
i:0 process ID: 5603 parent ID: 1 child ID: 0
i:1 process ID: 5604 parent ID: 1 child ID: 0
i:2 process ID: 5605 parent ID: 1 child ID: 0
i:3 process ID: 5606 parent ID: 1 child ID: 0
*/

 上面的程序简单的创建了一个进程链。1->2->3->4...,另外大家要注意我在这里使用的是stderr,而不是stdout,因为系统会对写入stdout的进行缓冲。这样,特定的消息不能在printf之后立即显示。写入stderr则不会进行缓冲,而是立即写出。

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
int main(int argc, char const *argv[])
{
    pid_t childpid=0;
    int i,n;
 
    if (argc!=2)
    {
        fprintf(stderr, "usage: %s processes\n",argv[0] );
        return 1;
    }
 
    n=atoi(argv[1]);
    for ( i = 0; i < n; ++i)
    {
        if((childpid=fork())<=0){
            break;
        }
        fprintf(stderr, "i:%d process ID:%ld, parent ID: %ld, child ID: %ld\n",
            i,(long)getpid(),(long)getppid(),(long)childpid );
    }
    return 0;
}

 上面的例子简单的创建了一个进程扇。

 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
 
// this program will print some information after all the child process exits;
 
pid_t  r_wait(int* stat_lco);
 
int main(int argc, char const *argv[])
{
    pid_t childpid=0;
    int i,n;
 
    if (argc!=2)
    {
        fprintf(stderr, "usage: %s processes\n",argv[0] );
        return 1;
    }
 
    n=atoi(argv[1]);
    for ( i = 0; i < n; ++i)
    {
        if((childpid=fork())<=0){
            break;
        }
        while(r_wait(NULL)>0);  /*wait for all of your children*/
        fprintf(stderr, "i:%d process ID:%ld, parent ID: %ld, child ID: %ld\n",
            i,(long)getpid(),(long)getppid(),(long)childpid );
    }
    return 0;
}
 
pid_t  r_wait(int* stat_lco){
    int retval;
    while(((retval=wait(stat_lco)==-1)&& (errno==EINTR));
        return retval;
}

 上面的进程扇使得所有子进程都推出之后,打印消息。

 

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
 
// can you discribe all of this program run's results
 
int main(int argc, char const *argv[])
{  
    pid_t childpid;
 
    // set up signal handlers here
 
    childpid=fork();
    if(childpid==-1){
        perror("Faild to fork\n");
        return 1;
    }
 
    if(childpid==0){
        fprintf(stderr, "I am child %ld\n",(long)getpid() );
    }else if(wait(NULL)!=childpid){
        fprintf(stderr, "A signal must have interrupted the wait\n");
    }else{
        fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(),(long)childpid);
    }
 
     
    return 0;
}

 你能描述一下上面例子的所有可能输出吗?

 

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <errno.h>
 
int main(int argc, char const *argv[])
{
    pid_t childpid=0;
    int i,n;
 
    if(argc!=2){
        fprintf(stderr, "Usage: %s processes\n", argv[0] );
        return 1;
    }
 
    n=atoi(argv[1]);
    for (i = 0; i < n; ++i)
    {  
        if((childpid=fork())<=0){
            break;
        }
        for(;;){
            childpid=wait(NULL);
            if((childpid== -1)&&(errno != EINTR)){
                break;
            }
        }
    }
    fprintf(stderr, "I am process %ld, my parent is %ld\n",(long)getpid(),(long)getppid() );
    return 0;
}

 上面的例子创建了一个进程扇,所有被创建的进程都是原始进程的子进程。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
 
int main(int argc, char const *argv[])
{
    pid_t childpid;
    childpid=fork();
    if(childpid== -1){
        perror("Failed to fork\n");
        return 1;
    }               /*child code*/
    if(childpid==0){
        execl("/bin/ls","ls","-l",NULL);
        perror("child Failed to execl ls\n");
        return 1;
    }               /*parent code*/
    if(childpid!=wait(NULL)){
        perror("parent Failed to wait due to signal or error\n");
        return 1;
    }
    return 0;
}

 创建子进程来执行: ls -l命令。

 

 


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/06/02/2532001.html,如需转载请自行联系原作者