#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep Power
    0

    print process id and the current time in unix


    I have a program which creates a child process, I want the child process to print its id and the current time to stdout once a second, for ten seconds.

    I have created a function for child process but I'm not getting the right result. I'm new to programming so it may just be something small that I can't see:
    void first-child(){

    for (int i=0;i<10;i++)
    {
    sleep(1);
    printf("My id is%d" , pid);
    date();

    }

    I would appreciate any help.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    I'm assuming that you just forked.

    How did you set the value of the variable pid?

    If it was the return value of fork, then as the child it would be set to zero (for the parent, fork returns the PID of the child, which doesn't help you here anyway).

    If you set pid with getpid() before you forked, then it would still contain the parent's PID, not your own (id est, the child's).

    Have the child call getpid() in order to get its own PID:
    Code:
    void first-child(){ 
    
    for (int i=0;i<10;i++) 
    { 
        sleep(1); 
        printf("My id is%d\n" , getpid()); 
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep Power
    0
    Hi,
    Here is how I set the pid:

    int pid;
    x=pipe(apipe);
    pid=fork();
    if( pid==0){
    //goto first_child
    first-child();
    }

    void first-child(){
    for (int i=0;i<10;i++)
    {
    sleep(1);
    printf("My id is%d\n" , getpid());
    date();
    }
    Thanks, I will use getpid() instead of pid.
    In order for the child to print to the screen once a second for 10 seconds I have used the for loop from 0 to 9 and I used the sleep function per 1 second.
    I just wanted to be sure whether my idea was right?
    Thanks for your help.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    The thing about fork is that the child process is an exact clone of the parent, so both processes need some way to know which of them is the clone and which is the child (like Schwarzenegger in "The Sixth Day"). The return value from the fork is what tells them that: the parent gets the new child's PID and the child gets the meaningless value of zero -- as you had tested for. Hence the child needs to do a getpid() to get its own PID and could even do a getppid() to get its parent's PID.

    Yes, your approach is basically right. The only thing I would change would be to do the getpid() outside of the loop, since the PID should not change. Just a programming habit of mine that I don't like to call a function within a loop unless I need its side-effects or its return value could change.

    My version of your loop would be:
    Code:
    void first-child()
    { 
        pid_t  pid;
    
        pid = getpid();
        for (int i=0;i<10;i++) 
        { 
            sleep(1); 
            printf("My id is%d\n" , pid); 
            date(); 
        } 
    }
    I assume that you have your own date() function, since I'm not familiar with a built-in function by that name.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep Power
    0
    Thank you for your help.

    I don't have my own date function. I looked in the Unix man pages and I found date().

    DATE(1)

    NAME
    date - print or set the system date and time

    Is it okay if I use that?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    Originally posted by azi
    Thank you for your help.

    I don't have my own date function. I looked in the Unix man pages and I found date().

    DATE(1)

    NAME
    date - print or set the system date and time

    Is it okay if I use that?
    That's the date command from the command line, not a C function -- normally, all of the (1) man pages pertain to shell commands. You could invoke it from within a C program in a few different ways, but there are also a number of time functions in C that would do the job much more efficiently.

    Since I need to get on with some work I came into the office for, here's a quick example from the Visual C++1.52 help pages that should point you in the right direction.
    Code:
    /* ASCTIME.C: This program places the system time
     * in the long integer aclock, translates it into the
     * structure newtime and then converts it to string
     * form for output, using the asctime function.
     */
    #include <time.h>
    #include <stdio.h>
    struct tm *newtime;
    time_t aclock;
    void main( void )
    {
       time( &aclock );                 /* Get time in seconds */
       newtime = localtime( &aclock );  /* Convert time to struct tm form  */
       /* Print local time as a string */
       printf( "The current date and time are: %s", asctime( newtime ) );
    }
    I would also recommend that you peruse the time.h functions to see what you can do with them. Including creating your own format for the date/time string.

IMN logo majestic logo threadwatch logo seochat tools logo