How To Use Fork In C
The fork() Organization Call
System call fork() is used to create processes. Information technology takes no arguments and returns a process ID. The purpose of fork() is to create a new procedure, which becomes the child process of the caller. Afterward a new kid process is created, both processes will execute the next instruction following the fork() system call. Therefore, nosotros accept to distinguish the parent from the child. This can be done by testing the returned value of fork():
- If fork() returns a negative value, the creation of a child process was unsuccessful.
- fork() returns a goose egg to the newly created child process.
- fork() returns a positive value, the process ID of the kid procedure, to the parent. The returned procedure ID is of type pid_t divers in sys/types.h . Normally, the process ID is an integer. Moreover, a procedure tin can apply function getpid() to retrieve the process ID assigned to this process.
Permit us accept an example to make the above points clear. This example does not distinguish parent and the child processes. Click here to download this file fork-01.c .
Suppose the above program executes upwards to the signal of the phone call to fork() (marked in carmine colour):#include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 void primary(void) { pid_t pid; int i; char buf[BUF_SIZE]; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf, "This line is from pid %d, value = %d\n", pid, i); write(1, buf, strlen(buf)); } }
If the call to fork() is executed successfully, Unix will
- make 2 identical copies of address spaces, ane for the parent and the other for the child.
- Both processes will start their execution at the side by side argument following the fork() call. In this case, both processes will offset their execution at the assignment statement every bit shown below:
Both processes start their execution correct afterward the system phone call fork(). Since both processes have identical simply divide accost spaces, those variables initialized before the fork() call have the same values in both address spaces. Since every process has its own address space, any modifications will be independent of the others. In other words, if the parent changes the value of its variable, the modification will only bear upon the variable in the parent procedure's accost space. Other address spaces created past fork() calls will not be affected even though they take identical variable names.
What is the reason of using write rather than printf? It is because printf() is "buffered," meaning printf() will group the output of a process together. While buffering the output for the parent process, the child may also utilise printf to print out some information, which will also be buffered. As a event, since the output will not be send to screen immediately, you may non get the correct lodge of the expected effect. Worse, the output from the two processes may be mixed in strange ways. To overcome this problem, you lot may consider to use the "unbuffered" write.
If y'all run this plan, yous might see the following on the screen:
Procedure ID 3456 may exist the one assigned to the parent or the child. Due to the fact that these processes are run meantime, their output lines are intermixed in a rather unpredictable way. Moreover, the order of these lines are determined past the CPU scheduler. Hence, if you run this program again, you may go a totally unlike result................. This line is from pid 3456, value thirteen This line is from pid 3456, value 14 ................ This line is from pid 3456, value 20 This line is from pid 4617, value 100 This line is from pid 4617, value 101 ................ This line is from pid 3456, value 21 This line is from pid 3456, value 22 ................
Consider i more elementary example, which distinguishes the parent from the child. Click here to download this file fork-02.c .
In this programme, both processes print lines that indicate (1) whether the line is printed by the child or by the parent process, and (2) the value of variable i. For simplicity, printf() is used.#include <stdio.h> #include <sys/types.h> #define MAX_COUNT 200 void ChildProcess(void); /* child process paradigm */ void ParentProcess(void); /* parent process prototype */ void master(void) { pid_t pid; pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); } void ChildProcess(void) { int i; for (i = 1; i <= MAX_COUNT; i++) printf(" This line is from kid, value = %d\due north", i); printf(" *** Child process is done ***\n"); } void ParentProcess(void) { int i; for (i = 1; i <= MAX_COUNT; i++) printf("This line is from parent, value = %d\n", i); printf("*** Parent is done ***\northward"); }
When the chief program executes fork(), an identical re-create of its address infinite, including the program and all data, is created. System call fork() returns the child process ID to the parent and returns 0 to the kid process. The following figure shows that in both address spaces there is a variable pid. The one in the parent receives the child's process ID 3456 and the one in the child receives 0.
At present both programs (i.e., the parent and child) volition execute independent of each other starting at the side by side argument:
In the parent, since pid is non-zero, information technology calls function ParentProcess(). On the other hand, the child has a zero pid and calls ChildProcess() equally shown below:
Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child procedure will run for some fourth dimension before the command is switched to the other and the running procedure volition print some lines before yous can come across any line printed past the other process. Therefore, the value of MAX_COUNT should be big plenty and then that both processes will run for at least two or more fourth dimension quanta. If the value of MAX_COUNT is and then small that a process tin can cease in i time quantum, you will encounter two groups of lines, each of which contains all lines printed past the same process.
How To Use Fork In C,
Source: https://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
Posted by: waggoneramust1994.blogspot.com
0 Response to "How To Use Fork In C"
Post a Comment