fork(), your help needed!!!


 
Thread Tools Search this Thread
Top Forums Programming fork(), your help needed!!!
# 1  
Old 11-05-2011
fork(), your help needed!!!

Hi friends,
First I am going to type the c program, then we will discuss it.

Code:
     1  #include <sys/types.h>
     2  #include <stdio.h>
     3  #include <unistd.h>
     4  
         int main()
     5  {
     6     pid_t pid;
     7  
         /* fork a child process */
     8  pid = fork();
     9  
    10  if(pid<0)
    11  { /* error occured */
    12  fprintf(stderr, "\Fork Failed\n");
    13  exit(-1);
    14  }
    15  else if(pid==0)
    16  { /* child process */
    17  execlp("/bin/ls","ls",NULL);
    18  }
    19  else
    20  {  /* parent process */
    /* parent will wait for the child to complete */
    21  wait(NULL);
    22  printf("\nChild complete\n");
    23  exit(0);
    24  }
    25  }

And here comes my questions
1. Why does
Code:
pid

variable contain two values, or how could it have two values, less than 0, equal to 0, and greater than zero. They say fork() returns different values for parent process and child process. but how is it possible for one variable to hold two values at the same time?
2. Please explain the syntax of
Code:
execlp("/bin/ls","ls",NULL);

. What does it get NULL at the end?
3. Syntax of exit()???. Somestimes it gets -1, 0 and EXIT_FAILURE and things like that.
4. What does wait() do, why is it taking NULL argument here?
And here is a another program

Code:
     1  #include <stdio.h>     /* printf, stderr, fprintf */
     2  #include <sys/types.h> /* pid_t */
     3  #include <unistd.h> /* _exit, fork */
     4  #include <stdlib.h> /* exit */
     5  #include <errno.h> /* errno */
     6  int main()
     7  {
     8          pid_t pid;
     9          pid = fork();
    10          if(pid == -1)
    11          {
    12          fprintf(stderr, "can't fork, error %d\n", errno);
    13          exit(EXIT_FAILURE);
    14          }
    15          if(pid == 0)
    16          {
    17          int j;
    18          for(j = 0;j<10;j++)
    19          {
    20          printf("child: %d\n", j);
    21          sleep(1);
    22          }
    23          _exit(0);
    24          }
    25          else
    26          {
    27          int i;
    28          for(i = 0;i<10;i++)
    29          {
    30          printf("parent: %d\n",i);
    31          sleep(1);
    32          }
    33          exit(0);
    34          }
    35          return 0;
    36  }

And it's output
Code:
$ ./fork
child: 0
parent: 0
child: 1
parent: 1
child: 2
parent: 2
child: 3
parent: 3
child: 4
parent: 4
child: 5
parent: 5
child: 6
parent: 6
child: 7
parent: 7
child: 8
parent: 8
child: 9
parent: 9

1. Why and how there is a switching between child process and parent process?
2. What is the different between exit() and _exit()

And a small program as well,

Code:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
        pid_t pid;
        pid = fork();
        printf("%d\n",pid);
        return 0;
}

And here is the output
Code:
$ ./fork4
0
1055

Why does the second value, 1055 gets printed, I didn't use printf to print this value, where did it come from? And if I remove the printf function from this program, like
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
pid_t pid;
pid = fork();
return 0;
}
then there is no output at all, why is that so?

Please bear with my nonsensical questions, I am an absolute novice???

Looking forward to your kind and helpful replies!

You guys rock!

Thanks

Last edited by gabam; 11-05-2011 at 05:56 AM..
# 2  
Old 11-05-2011
Have a look at rule #6 here: The UNIX and Linux Forums - Forum Rules
# 3  
Old 11-05-2011
Did you take a look at the description/manpages of the functions you're asking about? There's a lot of useful information in there, and it'll help you asking more specific questions, which will yield more informative answers. Besides, the most important trait needed in IT is the ability to learn new things, and how to look up information.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Fork!

I understand that fork create a child but I need very simple example that make child useful.... I mean how will make the program faster anyone explain with code plz using C plz (2 Replies)
Discussion started by: fwrlfo
2 Replies

2. UNIX for Dummies Questions & Answers

fork()

I'm trying to run a simple test on how to use fork(), i'm able to execute the child process first then the parent, but how can I execute parent then child..? Thanks! (1 Reply)
Discussion started by: l flipboi l
1 Replies

3. Programming

Fork()

does fork() spawn only the parent process, what if fork() is looped, does it spawn the parent and the child? (4 Replies)
Discussion started by: Peevish
4 Replies

4. Programming

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each... (2 Replies)
Discussion started by: xyz123456
2 Replies

5. UNIX for Advanced & Expert Users

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each of... (1 Reply)
Discussion started by: xyz123456
1 Replies

6. Programming

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... (3 Replies)
Discussion started by: alexicopax
3 Replies

7. Programming

Fork or what?

Hello all. I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
Discussion started by: crippe
0 Replies

8. Programming

fork()

#include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 void main(void) { pid_t pid; int i; char buf; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf,... (2 Replies)
Discussion started by: MKSRaja
2 Replies

9. Programming

fork() fd

I run this code, actually I want to both processes print the message from "data". But only one does. What happens? Anyone can help? #include <stdio.h> main(){ int fd, pid; char x; fd = open("data",0); /* open file "data" */ pid = fork(); if(pid != 0){ wait(0); ... (2 Replies)
Discussion started by: Herman
2 Replies

10. UNIX for Dummies Questions & Answers

Fork

What is a fork? Why would one create a fork? What are the advantages and disadvantages of using a fork? Please advise. Thank You. Deepali (5 Replies)
Discussion started by: Deepali
5 Replies
Login or Register to Ask a Question