Sponsored Content
Homework and Emergencies Homework & Coursework Questions fork system call understanding Post 302400091 by MrUser on Tuesday 2nd of March 2010 07:19:11 AM
Old 03-02-2010
fork system call understanding

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

i have a problem in understanding the behaviour of fork .

i understood fork as to create a new process and all the statements that fall after fork are executed by both old and new process.

here's the code i have written and i dont understand how its produced so - called out put.



2. Relevant commands, code, scripts, algorithms:


3. The attempts at a solution (include all code and scripts):
Code:
#include<stdio.h>

int main( void ) {
        int i ;
        i = 0 ;
        int x;
        while ( i < 2) {
                printf("before fork():getpid()= %d \n", getpid());
                x = fork();
                if ( x == 0 ) {
                        printf("in child when i = %d ", i);
                        printf("getpid()= %d", getpid());
                        printf("getppid()= %d\n",getppid());
                }
                else {
                        printf("in parent when i = %d ", i);
                        printf("getpid()= %d", getpid());
                        printf("getppid()= %d\n",getppid());
                }
                i++;
                printf("\n");
        }
return 0;
}

the out put what i am getting is :

Quote:
before fork():getpid()= 6864
in parent when i = 0 getpid()= 6864getppid()= 1777

before fork():getpid()= 6864
in child when i = 0 getpid()= 6865getppid()= 6864

before fork():getpid()= 6865
in child when i = 1 getpid()= 6866getppid()= 6864

in child when i = 1 getpid()= 6867getppid()= 6865

in parent when i = 1 getpid()= 6864getppid()= 1777

in parent when i = 1 getpid()= 6865getppid()= 6864


please just provide the meaning full description or modify the code so that i can understand it easily.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

VSU,Bangalore,India,Govardhan.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

URGENT Help required regarding the use of FORK system call

I desperately wanted one of the UNIX Gurus to help me resolve my problem asap(I have to deliver the code to the client by Monday 08-oct). I have a file with around 5 million records (50 lakhs). Now my original process was taking around 30 hours to read the complete file, process each and every... (4 Replies)
Discussion started by: kkumar1975
4 Replies

2. Programming

Fork() system call time?

One more question. How can i calculate the time that system needs to make fork() system call? I need to make it with times function but i really don't know how. :( (2 Replies)
Discussion started by: davidoff
2 Replies

3. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

4. UNIX for Dummies Questions & Answers

fork() system call

Can anyone explain me what really happens when a system call fork() is called ? I like to know what happens internally. Thanks in Advance. - Arun (1 Reply)
Discussion started by: arunviswanath
1 Replies

5. UNIX for Dummies Questions & Answers

fork system call

Hi folks, I want to know how this below program works? #include <stdio.h> int main() { printf("A\n"); fork(); printf("B\n"); fork(); fork(); printf("D\n"); fork(); printf("C\n"); } This is just for example. How this type of programs where fork is used many places, how the... (1 Reply)
Discussion started by: u_peerless
1 Replies

6. Shell Programming and Scripting

fork system call and \n

hi, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() { printf("demo of fork"); (0 Replies)
Discussion started by: pnirmala
0 Replies

7. Programming

Problem with execution of fork system call if i use \n

hi all, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() (3 Replies)
Discussion started by: pnirmala
3 Replies

8. UNIX for Advanced & Expert Users

Doubt with fork() system call

Hi I wrote a simple fork program to illustrate the fork() system cal. here it is #include<stdio.h> #include<sys/stat.h> #include<sys/types.h> main() { int flag; flag=fork(); if(flag==0) { printf("Child \n"); printf("Process id= %d\n",getpid()); ... (3 Replies)
Discussion started by: badsha6642
3 Replies

9. Shell Programming and Scripting

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. Duplicate Post - Continue Here - Please Do Not Cross Post... (0 Replies)
Discussion started by: Xiaoya
0 Replies

10. Ubuntu

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. (2 Replies)
Discussion started by: Xiaoya
2 Replies
getpid(2)							   System Calls 							 getpid(2)

NAME
getpid, getpgrp, getppid, getpgid - get process, process group, and parent process IDs SYNOPSIS
#include <unistd.h> pid_t getpid(void); pid_t getpgrp(void); pid_t getppid(void); pid_t getpgid(pid_t pid); DESCRIPTION
The getpid() function returns the process ID of the calling process. The getpgrp() function returns the process group ID of the calling process. The getppid() function returns the parent process ID of the calling process. The getpgid() function returns the process group ID of the process whose process ID is equal to pid, or the process group ID of the calling process, if pid is equal to 0. RETURN VALUES
The getpid(), getpgrp(), and getppid() functions are always successful and no return value is reserved to indicate an error. Upon successful completion, getpgid() returns the process group ID. Otherwise, getpgid() returns (pid_t)-1 and sets errno to indicate the error. ERRORS
The getpgid() function will fail if: EPERM The process whose process ID is equal to pid is not in the same session as the calling process, and the implementation does not allow access to the process group ID of that process from the calling process. ESRCH There is no process with a process ID equal to pid. The getpgid() function may fail if: EINVAL The value of the pid argument is invalid. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ |Standard |See standards(5). | +-----------------------------+-----------------------------+ SEE ALSO
Intro(2), exec(2), fork(2), getsid(2), setpgid(2), setpgrp(2), setsid(2), signal(3C), attributes(5), standards(5) SunOS 5.11 27 Jan 2009 getpid(2)
All times are GMT -4. The time now is 11:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy