![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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... |
| Forum Sponsor | ||
|
|
|
||||
|
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);
}
}
|
||||
| Google The UNIX and Linux Forums |