0

I'm a student studying C and how to create processes using fork() Can you please explain what is the difference between these two codes because I've tried the both and they didn't work as expected.

child = fork();
child1 = fork();
if (child == 0 && child1 == 0){//Parent}
else if (child > 0 && child1 == 0){//first child}
else if (child == 0 && child1 > 0){//second child}
else {third child}

is this is the right way to create the children or is it the one below?

child = fork();
if (child == 0)
{
    child1 = fork();
    if (child1 == 0)
    {// grandchild
    }
    else
    {//child
    }
}
else 
{//parent
}

These are examples written by me on what confused me. Here's the code i'm having trouble with

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char ** argv)
{
    pid_t child;
    pid_t child1;
    // at least, there should be 3 arguments
    // 2 for the first command, and the rest for the second command
    //printf("%d\n", argc);
    if (argc < 4) {
        fprintf(stderr, "Usage: %s cmd1 cmd1_arg cmd2 [cmd2_args ..]\n", argv[0]);
        return 1;
    }
    child = fork();
    //pid_t child1;
    // TODO
    if (child < 0)
    {
        perror("fork()");
        exit(EXIT_FAILURE);
    }
    if (child == 0)//child1
    {
        //printf("exited=%d exitstatus=%d\n", WIFEXITED(exitStatus), WEXITSTATUS(exitStatus));
        child1 = fork();

        if (child1 == 0)//grandchild
        {
            execlp(argv[1],argv[1],argv[2],NULL); 
            perror("execlp()");
            exit(EXIT_FAILURE);
        }

        else //first child
        {
            int status1;
            waitpid(child1, &status1, 0);
            printf("exited=%d exitstatus=%d\n", WIFEXITED(status1), WEXITSTATUS(status1));
            execvp(argv[3], (argv + 3));
            perror("execlp()");
            exit(EXIT_FAILURE);
        }
    }
    else//parent
    {
        int status;
        waitpid(child, &status, 0);
        printf("exited=%d exitstatus=%d\n", WIFEXITED(status), WEXITSTATUS(status));
    }
    return 0;
}

I'm able to get the code to work as intended but i'm confused on how to use WIFEXITED and WEXITSTATUS. My code output when I run this code is

execlp(): No such file or directory
Makefile
exited=1 exitstatus=0

And the right output is:

execlp(): No such file or directory
Makefile
exited=1 exitstatus=1
exited=1 exitstatus=0

Arguments used in this test case

cal -3 ls Makefile

Why am I missing the second exit print?

CSE_Husky
  • 19
  • 2
  • You have the comment `if (child == 0 && child1 == 0){//Parent}` — that's wrong; the parent has both PID values non-zero. The process with both PID values zero is the grandchild. – Jonathan Leffler Mar 16 '20 at 05:56
  • @JonathanLeffler Sorry that was a typo, I do understand what you are saying but I'm not sure what is the best way to implement solving the problem I have on hand. I want to run two exe commands and print the exit status. Can you give me an idea on what I'm doing wrong? – CSE_Husky Mar 16 '20 at 06:21
  • Your sample command line is puzzling. You say `cal -3 ls Makefile`, and your usage message says `Usage: %s cmd1 cmd1_arg cmd2 [cmd2_args ..]`. Assuming your program is called `cal` (note that there is, at least on many systems, a standard program called `cal` — try `cal 9 1752`), then your `cmd1` is `-3`, the argument to it is `ls`, and the `cmd2` is `Makefile`, which is not very conventional. I'd expect something more like `./cal ls -l cat Makefile` as an invocation. Would we be correct in assuming you want `cmd1 cmd1arg` run and completed before `cmd2 [cmd2arg ...]` is run? – Jonathan Leffler Mar 16 '20 at 18:15

1 Answers1

1

Your first two code snippets are not same at all.

First case,

enter image description here

Second case,

enter image description here

  • I now understand what this code means and I was able to fix most of my problems. The only problem I'm still facing is how to control which exe command runs first? It doesn't seem to work if I flip them for some reason – CSE_Husky Mar 16 '20 at 06:18