parent and child processes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jewel87
    New Member
    • Jan 2007
    • 62

    parent and child processes

    Hi everyone!
    I'm writing some code in C under UNIX, which should give some output like this:
    PARENT: pid = 10063
    CHILD: my pid = 10064
    CHILD: my parent's pid = 10063
    CHILD: Sleeping...
    PARENT: my child's pid = 10064
    PARENT: Waiting for the child to exit...
    CHILD: Done sleeping...
    PARENT: child is dead.

    The output I am getting looks like this:

    CHILD: my pid = 3891
    CHILD: my parent's pid = 3890
    PARENT: my pid = 3890
    PARENT: my child's pid = 3891
    PARENT: Waiting for the child to exit...PARENT: the child is dead.
    loki.brunel.ac. uk% CHILD: Sleeping...CHIL D: Done sleeping...

    Here is the code I have done:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(){
    
    pid_t pid;
    
    switch (pid = fork())
    {
         case -1:
                 perror("couldn't fork");
                 break;
         case 0:
                 printf("CHILD: my pid = %d\n", getpid());
                 printf("CHILD: my parent's pid = %d\n",getppid());
                 printf("CHILD: Sleeping...");
                 sleep(10);
                 printf("CHILD: Done sleeping...");
                 break;
    
         default:
                 printf("PARENT: my pid = %d\n", getpid());
                 printf("PARENT: my child's pid = %d\n", pid);
                 printf("PARENT: Waiting for the child to exit...");
                 waitpid(pid);
                 printf("PARENT: the child is dead.\n");
                 break;
     }
     return 0;
    
    }

    What is wrong here, why it goes to the parent process in the middle of the childs process commands? How do I change this?
  • Arulmurugan
    New Member
    • Jan 2008
    • 90

    #2
    Originally posted by jewel87
    Hi everyone!
    I'm writing some code in C under UNIX, which should give some output like this:
    PARENT: pid = 10063
    CHILD: my pid = 10064
    CHILD: my parent's pid = 10063
    CHILD: Sleeping...
    PARENT: my child's pid = 10064
    PARENT: Waiting for the child to exit...
    CHILD: Done sleeping...
    PARENT: child is dead.

    The output I am getting looks like this:

    CHILD: my pid = 3891
    CHILD: my parent's pid = 3890
    PARENT: my pid = 3890
    PARENT: my child's pid = 3891
    PARENT: Waiting for the child to exit...PARENT: the child is dead.
    loki.brunel.ac. uk% CHILD: Sleeping...CHIL D: Done sleeping...

    Here is the code I have done:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(){
    
    pid_t pid;
    
    switch (pid = fork())
    {
         case -1:
                 perror("couldn't fork");
                 break;
         case 0:
                 printf("CHILD: my pid = %d\n", getpid());
                 printf("CHILD: my parent's pid = %d\n",getppid());
                 printf("CHILD: Sleeping...");
                 sleep(10);
                 printf("CHILD: Done sleeping...");
                 break;
    
         default:
                 printf("PARENT: my pid = %d\n", getpid());
                 printf("PARENT: my child's pid = %d\n", pid);
                 printf("PARENT: Waiting for the child to exit...");
                 waitpid(pid);
                 printf("PARENT: the child is dead.\n");
                 break;
     }
     return 0;
    
    }

    What is wrong here, why it goes to the parent process in the middle of the childs process commands? How do I change this?
    Hi jewel,
    This is synchronization problem, as we know unix is multiprocessing OS when quantum time completes for one process the OS scheduler can schedules other process. This is reason you are getting a messed output.

    So, you have to choose a synchronization method to get your perfect output what you needed. If you tell me, why do you want this?, it would be great for me to understand for you problem and help further more.

    Regards
    Arul

    Comment

    • jewel87
      New Member
      • Jan 2007
      • 62

      #3
      Thank you very much for your answer, Arul.
      Actually, the wanted output I gave in the beginning, is what the task set by the teacher required. Would this code be the right answer or I can improve it somehow to ensure best marks? What do you think??

      P.S. Here is what the rest task is saying:

      Write a program that uses fork() to create two processes.
      In the child process, do the following:
      • print it's own PID
      • print it's parent's PID
      • sleep for 10 seconds using the sleep() syscall.
      • call the exit function to terminate.
      In the parent process do the following:
      • print it's own PID
      • print it's child's PID
      • wait for the child to die using the wait() syscall.
      • when the child is dead, print a message that says so.

      Comment

      • Arulmurugan
        New Member
        • Jan 2008
        • 90

        #4
        Originally posted by jewel87
        Thank you very much for your answer, Arul.
        Actually, the wanted output I gave in the beginning, is what the task set by the teacher required. Would this code be the right answer or I can improve it somehow to ensure best marks? What do you think??

        P.S. Here is what the rest task is saying:

        Write a program that uses fork() to create two processes.
        In the child process, do the following:
        • print it's own PID
        • print it's parent's PID
        • sleep for 10 seconds using the sleep() syscall.
        • call the exit function to terminate.
        In the parent process do the following:
        • print it's own PID
        • print it's child's PID
        • wait for the child to die using the wait() syscall.
        • when the child is dead, print a message that says so.
        Hi Jewel,
        If it is a assignment, i think your o/p would be ok.But one thing for better o/p aligmnment put all your "\n" at the beginning of the printf.(eg printf("\nHello ");.

        Any have if you need more input on process syn or IPC you can find something from the following link.
        http://www.advancedlin uxprogramming.c om/alp-folder
        http://www.cs.cf.ac.uk/Dave/C/
        Regards
        Arul

        Comment

        • jewel87
          New Member
          • Jan 2007
          • 62

          #5
          Thank you very much for your help, Arul !!!

          Comment

          Working...