码迷,mamicode.com
首页 > 其他好文 > 详细

linuc c 代码示例

时间:2014-10-30 00:01:53      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   for   sp   strong   

 

fork的应用:

#include "stdio.h"
#include "string.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#include <unistd.h>

#define Max(a,b) ({int a1=a,b1=b; a1>b1?a1:b1;})

int main()
{

    int i,j,k,*q;
    char *p="abc";
    int pids[10];
    int pid;

    q=pids;

    //printf("%d \n", getpid());
    //sleep (3);

for(i=0;i<3;i++)
{

    printf("for-%d\n",i);
    pid=fork();
    if(pid) {
        *(q++) = pid;    
    }else if(pid == 0){
        sleep(5);
        printf("child:%d \n", getpid());
        break;
    }
}
    if(pid>0)
    {
        for(i=0;i<3;i++)
        {
        //    waitpid(pids[i], NULL, 0);    
            printf("parent: %d, %d \n", getpid(),pids[i]);

        }
    }
    printf("song \n");

}

没有waitpid时输出:

bubuko.com,布布扣

有waitpid时:

bubuko.com,布布扣

 

linuc c 代码示例

标签:style   blog   http   io   color   ar   for   sp   strong   

原文地址:http://www.cnblogs.com/siqi/p/4060834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!