Thread: fork problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    Istanbul
    Posts
    11

    Exclamation fork problem

    hi everyone;
    i want to create 2 prrocesses with fork command and first child will print the number from 0 to 9 with 1 second delay (and then will exit) and the other child will write the same number simultaneously to a file (and then will terminate). The parent process will be waiting for one child to exit and print something basically.

    Here is my code.

    Code:
    #include <unistd.h>	/* defines fork()      */
    #include <sys/wait.h>	/* defines the wait() system call. */
    #include <sys/types.h>  /*defines pid_t */
    #include <stdio.h>
    
    void catch_child(int sig_num)
    {
        /* when we get here, we know there's a zombie child waiting */
        int child_status;
    
        wait(&child_status);
        printf("child exited.\n");
    }
    
    int main(int argc, char* argv[])
    {
    	FILE *fp;
    	pid_t child_pid; 
    	pid_t child_pid2;
    	//int child_status;
    	int i;	
    
    	fp = fopen("labwork2.txt","w");
    	
    	child_pid = fork();
    	
    	signal(SIGCHLD, catch_child);
    
    	switch(child_pid)
    	{
    		case -1:	                         /* fork() failed */
    			perror("fork");	                       /* print a system-defined error message */
    			exit(1);
    		case 0:	                        /* fork() succeeded, we're inside the child process */
    			for(i = 0; i < 10; i++)
    			{
    				fprintf(fp,"%d\n",i);
    				fflush(fp);
    				sleep(1);
    			}
    			fclose(fp);
    			//waitpid(child_pid,0,0);
    			exit(0);
    		default:	
    			child_pid2 = fork();
    			if(child_pid2 == 0)
    			{
    				execl("/usr/bin/tail","tail","-f","file.txt",(char*)0);
    			}
    			else if(child_pid2 > 0)
    			{
    				printf("Kill all\n");
    			}	
    	}
    }
    i did not understand how to kill a specific process. i used a cathc_child function but it was a sample code(i did not write it) so i did not understand how this function works either.
    when i compile and run the code, at the first time it prints the number both to the screen and to the file but if i run it again it starts printing number twice or more.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    i did not understand how to kill a specific process.
    With kill().

    I don't quite understand the intent of your program, but if you want to kill something, you use kill(). From what I can guess, you do want that waitpid() that you have commented out, but you want it in the parent. Catching SIGCHLD is useful only if you don't care about which child you're reaping. It seems to me that your first child is the one you really want to keep track of, since it will run for roughly 10 seconds and you need to do something after it finishes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

Tags for this Thread