The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shm sem fork etc... Please help Dana73 High Level Programming 1 02-28-2006 04:51 AM
Fork () iwbasts High Level Programming 5 11-09-2005 12:39 AM
Fork or what? crippe High Level Programming 0 03-08-2005 01:21 AM
fork() MKSRaja High Level Programming 2 02-07-2005 07:55 AM
Fork Deepali UNIX for Dummies Questions & Answers 5 08-26-2001 05:14 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-06-2007
Registered User
 

Join Date: Mar 2007
Posts: 2
Stumble this Post!
fork() help

Hi everybody,
I wanna write a code to understand how fork works.

my target
--------------

-Parent creates a file(called temp) and writes into this file "1".Then it closes the file.

-Then parent creates a child and wait until execution of this child ends.

-Then child opens the same file and reads the last integer in it. Then, it will multiply this integer by 2 and add 1. Then child appends the result as a new line at the end of the file. After closing the file this child will fork another child process and wait until it finishes its execution.

-Then child which calculates the Nth integer will not fork anymore, and return after writing the result into the file.( N can be taken from user )

Thanks a lot men...
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-06-2007
Registered User
 

Join Date: Mar 2007
Posts: 2
Stumble this Post!
Uımmm.
So, how can i fork in a child process for a new child process?
Reply With Quote
  #3 (permalink)  
Old 03-06-2007
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,325
Stumble this Post!
This looks like homework. Is it? Why don't you just write code that forks once and where the parent prints that it is the parent and the child prints that it is the child? This will get you the code that you need to figure out which is a parent process and which is a child. Then you can use this code to run fork in the child process.
Reply With Quote
  #4 (permalink)  
Old 03-08-2007
Hitori's Avatar
Registered User
 

Join Date: Jun 2006
Posts: 356
Stumble this Post!
Code:
/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <sys/wait.h>   /* Wait for Process Termination */
#include <stdlib.h>     /* General Utilities */
 
int main()
{
    pid_t childpid; /* variable to store the child's pid */
    int retval;     /* child process: user-provided return code */
    int status;     /* parent process: child's exit status */

    /* only 1 int variable is needed because each process would have its
       own instance of the variable
       here, 2 int variables are used for clarity */
        
    /* now create new process */
    childpid = fork();
    
    if (childpid >= 0) /* fork succeeded */
    {
        if (childpid == 0) /* fork() returns 0 to the child process */
        {
            printf("CHILD: I am the child process!\n");
            printf("CHILD: Here's my PID: %d\n", getpid());
            printf("CHILD: My parent's PID is: %d\n", getppid());
            printf("CHILD: The value of my copy of childpid is: %d\n", childpid);
            printf("CHILD: Sleeping for 1 second...\n");
            sleep(1); /* sleep for 1 second */
            printf("CHILD: Enter an exit value (0 to 255): ");
            scanf(" %d", &retval);
            printf("CHILD: Goodbye!\n");    
            exit(retval); /* child exits with user-provided return code */
        }
        else /* fork() returns new pid to the parent process */
        {
            printf("PARENT: I am the parent process!\n");
            printf("PARENT: Here's my PID: %d\n", getpid());
            printf("PARENT: The value of my copy of childpid is %d\n", childpid);
            printf("PARENT: I will now wait for my child to exit.\n");
            wait(&status); /* wait for child to exit, and store its status */
            printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
            printf("PARENT: Goodbye!\n");             
            exit(0);  /* parent exits */       
        }
    }
    else /* fork returns -1 on failure */
    {
        perror("fork"); /* display error message */
        exit(0); 
    }
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0