1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pbolem with forking the process

Discussion in 'C' started by Unitedroad, Sep 20, 2007.

  1. Unitedroad

    Unitedroad New Member

    Joined:
    Apr 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    HI everyone ,
    I am just experimenting with the fork function . I am having some problem with my code :

    HTML:
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <stdlib.h>
    
    childprocess(int pid, int pnum);
    main () {
    	int pid, pid0, pid1, status, FD, tochild[2], toparent[2];
    	FD = creat("new file.txt", S_IRWXU);
    	if((pid = fork()) < 0) {
    		printf("Could not fork process");
    	} else {
    		if(pid !=0)
    			[COLOR=Red]printf("\nProcess forked");[/COLOR]
    	}
    	
    	if(pid == 0) childprocess(pid,1);
    	//if(pid != 0) {
    		pid = wait(&status);
    		[COLOR=Red]printf("\nProcess (id %d ) exited ", pid);[/COLOR]
    		
    	//}
    	
    	if((pid1 = fork()) < 0) {
    		printf("Could not fork second process");
    	}else{
    		if(pid1 !=0)
    			[COLOR=Red]printf("\nSecond process forked");[/COLOR]
    	}	
    	if(pid == 0) childprocess(pid,2);
    	
    //	if(pid != 0) {
    		pid = wait(&status);
    		[COLOR=Red]printf("\nProcess (id %d ) exited " , pid);[/COLOR]
    //	} 
    	close(FD);
    	exit(0);
    }
    
    childprocess(int pid, int pnum) {
    	float i = 0;
    	int index = 0;
    	for(index = 0; index < 900; index++) {
    	}
    	//printf("\nPID %d matches index : %d", pid, pnum); 
    	exit(0);
    }
    Here I have forked the process twice. It waits after each subsequent fork call while the childprocess goes to a new method where it eventually quits.

    However the output is showing some strange behaviour. THis is the output of the code :

    root@unitedroad-desktop:~/Desktop# ./wait3.out

    Process forked
    Process (id 13402 ) exited Process (id 13402 ) exited
    Second process forked
    Process (id 13403 ) exited

    This seems a little stran as the process id 13402 sems to exit twice and inspite of me using the newline character at the begining of each string that I am printing, the second "exit line" does not start on the new line.
    KIndly help me in identifing the reason for this.

    Regards,
    Dhruwat
     
  2. Unitedroad

    Unitedroad New Member

    Joined:
    Apr 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I actually osted the code Ihad changed a little to see if it could work but left in the middle I am sorry for that and also for maiking the code so hard to read with stupid applicatipn of tags.

    The code I compiled is :
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <stdlib.h>
    
    childprocess(int pid, int pnum);
    main () {
    	int pid, pid0, pid1, status, FD, tochild[2], toparent[2];
    	FD = creat("new file.txt", S_IRWXU);
    	if((pid = fork()) < 0) {
    		printf("Could not fork process");
    	} else {
    		if(pid !=0)
    			printf("\nProcess forked");
    	}
    	
    	if(pid == 0) childprocess(pid,1);
    	//if(pid != 0) {
    		pid = wait(&status);
    		printf("\nProcess (id %d ) exited ", pid);
    		
    	//}
    	
    	if((pid = fork()) < 0) {
    		printf("Could not fork second process");
    	}else{
    		if(pid !=0)
    			printf("\nSecond process forked");
    	}	
    	if(pid == 0) childprocess(pid,2);
    	
    //	if(pid != 0) {
    		pid = wait(&status);
    		printf("\nProcess (id %d ) exited " , pid);
    //	} 
    	close(FD);
    	exit(0);
    }
    
    childprocess(int pid, int pnum) {
    	float i = 0;
    	int index = 0;
    	for(index = 0; index < 900; index++) {
    	}
    	//printf("\nPID %d matches index : %d", pid, pnum); 
    	exit(0);
    }
     
    Last edited by a moderator: Sep 20, 2007

Share This Page