LINUX xia fuzijinchengbingxing

MAKEFILE
head = pctl.h
srcs = pctl.c
objs = pctl.o
opts = -g -c
all	:	pctl
pctl	:	$(objs)
		gcc $(objs) -o pctl
pctl.o	:	$(srcs) $(head) 
		gcc $(opts) $(srcs) 
clean	:	rm pctl *.o




*.H

#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

typedef void (*sighandler_t)(int);

void sigcat(){
    printf("%d Process continue\n",getpid());
}

*.C

/*
    *Filename    :pctl.c
    *Function    :\
*/
#include "pctl.h"
int main(int argc, char *argv[])
{
    int i;
    int seconds = 10;

    int pid1;
    int pid2;    
    int status1;
    int status2;
    char* args1[] = {"/bin/ls","-all",NULL};    
    char* args2[] = {"/bin/ps","-l",NULL};    
    signal(SIGINT,(sighandler_t)sigcat);
    pid1 = fork();
    if(pid1 < 0){
        printf("Create Process fail !\n");
        exit(EXIT_FAILURE);    
    }
    if(pid1 == 0){
        printf("I am Child process %d\nMy father is %d\n",getpid(),getppid());
        pause();
        status1 = sleep(seconds);
        printf("%d child will Running: \n",getpid());
        /*if(argv[1] != NULL){
            for(i=1; argv[i]!=NULL; i++) printf("%s",argv[i]);
            printf("\n");        
            status = execve(argv[1], &argv[1], NULL);
        }
        else{*/
            for(i=0; args1[i]!=NULL; i++) printf("%s",args1[i]);
            printf("\n");        
            status1 = execve(args1[0], args1, NULL);
            exit(EXIT_SUCCESS);        
//}
        
    }
    else{
        printf("\nI am Parent process  %d\n",getpid());
        pid2 = fork();
        if(pid2 < 0){
            printf("Create Process fail !\n");
            exit(EXIT_FAILURE);    
        }
        if(pid2 == 0){
            printf("I am Child process %d\nMy father is %d\n",getpid(),getppid());
            if(kill(pid1,SIGINT)>=0) printf("%d Wakeup %d child.\n",getpid(),pid1);
            printf("%d don't Wait for child done.\n\n",getpid());    
            status2 = execve(args2[0], args2, NULL);
            exit(EXIT_SUCCESS);        
        }
    }
    return EXIT_SUCCESS;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章