0

I have been tasked with writing a C program which allows the child code to finish after the parent, using the sleep command.

This is what I have written, the code does not work and it only returns the 'else' part of the code. If anyone could help it would be much appreciated. I believe the problem is how I have used the sleep command.

#include <stdio.h>
#include <unistd.h>

int main() {
fork();
if (fork() ==0){
    sleep(5);
    printf("This will finish after the parent\n");
}
else
    printf("This will finish before the child\n");

return 0;
}
  • 6
    Why do you `fork` twice? – David Schwartz Mar 17 '21 at 21:21
  • 1
    *I have been tasked with writing a C program which allows the child code to finish after the parent, using the sleep command.* ***OUCH***. That's **NOT** a correct use of `sleep()`. Whoever tasked you with this is misguided at best. "Correct" synchronization can only be done with synchronization objects such as mutexes, condition variables, and semaphores. – Andrew Henle Mar 17 '21 at 21:22
  • 1
    @AndrewHenle Although I agree with what you're saying, a simple example using sleep is something I would consider appropriate for a very introductory lesson about processes. – klutt Mar 17 '21 at 21:24
  • Are you running the program at the command line in a shell in a terminal window, or are you running it in a window of its own through some IDE? If you are running it in its own window, what is likely happening is the parent exits and the associated window vanishes, so you never see the output from the children. Running the program in a terminal window would show the desired output. You could also put a `sleep(6)` after the parent’s `printf`. – Eric Postpischil Mar 17 '21 at 21:24
  • 2
    @DavidSchwartz: The program forks three times, not two. – Eric Postpischil Mar 17 '21 at 21:25
  • OP: Remove the first fork call and your code will work – klutt Mar 17 '21 at 21:25
  • Does this answer your question? [understanding fork(), sleep() and processes flux](https://stackoverflow.com/questions/8167258/understanding-fork-sleep-and-processes-flux) – Thomas Dickey Mar 17 '21 at 23:28

3 Answers3

0

try to use the pid instead of fork() twice

#include <stdio.h>
    #include <unistd.h>
    
    int main() {
    pid_t pid=fork();
    if (pid==0){
        sleep(5);
        printf("This will finish after the parent\n");
    }
    else
        printf("This will finish before the child\n");
    
    return 0;
    }
fares
  • 97
  • 1
  • 5
  • This eliminates the duplicate messages but does not resolve the OP’s problem that the child’s output is not seen. (Also, `pid_t` should be used to hold the return value of `fork`, not `int`.) – Eric Postpischil Mar 17 '21 at 21:30
  • @EricPostpischil thank you very much. I was compiling and running the program through Clion (an ide). I ran the program using terminal and it worked. Thanks – Rory Galley Mar 17 '21 at 21:41
-1

This is what I came up with, but it is somewhat hard to tell which is the child class and the parent class.

int main() {
    fork();
    if (fork() ==0){
        // sleep(5);
        printf("This will finish after the parent\n");
    }
    else
    {
    sleep(5);
    printf("This will finish before the child\n");
    
    }
    return 0;
}
-1

Fork system call is used for creating a new process.parent process and child process.the child get pid==0,but the parent get pid==(to the main pid of the child).

#include <stdio.h>
    #include <unistd.h>
    
    int main() {
    pid_t pid=fork();
    if (pid==0){
        sleep(5);
        printf("This will finish after the parent\n");
        printf("child pid is %d\n",getpid());//get the current process pid 
    }
    else
    {
        printf("This will finish before the child\n");
    printf("child pid is %d\n",pid);
    }
    return 0;
    } 
fares
  • 97
  • 1
  • 5