os实验1 ——进程控制 - webdancer's Blog

os实验1 ——进程控制

webdancer posted @ 2010年4月06日 07:18 in 工具 with tags OS , 1124 阅读
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<stdlib.h>
typedef  void (*sighandler_t) (int);
void sincon()
{
}
int main()
{
    pid_t p0;
    int sta0,sta1;
    signal(SIGINT,(sighandler_t)sincon);
    while(1)
    {
        if((p0=fork())<0)
        {
            perror("error!fork first child");
            exit(0);
        }
        else if(p0==0)
        {
            pause();
            sta0=execlp("/bin/ls","ls",NULL);
            exit(0);
        }
        else
        {
            pid_t p1;
            if((p1=fork())<0)
            {
                perror("error!fork second child");
                exit(0);
            }
            else if(p1==0)
            {
                printf("exe the second child\n");
                sta1=execlp("/bin/ps","ps",NULL);
                exit(0);
            }
            else
            {
                if(waitpid(p1,&sta1,0)!=p1)
                    perror("error!");
                if((kill(p0,SIGINT))>=0)
                    printf("wake up first child\n");
                sleep(3);
            }
        }
    }
    return 0;
}

 

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee