Help with Execl system call in a C program?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    "Your a7.c program should use printf to print a nice message. (You can decide what to say.) Then the process should use the fork system call to create a child process. Then, your code should do different things depending on whether the process is the parent or the child:

The parent should print one more message (using printf) saying that it is the parent, what its PID is, and what its child's PID is. It should then contain the command sleep(4).

The child process should use the excel system call to load and run your a7.sh script.

The a7.sh script is the first half of the homework that I have already completed and it simply is a script that contains a function that uses three different parameters that are integers, such as 17 5 and 23. There are multiple conditions that I had to be met and if the parameters met any of these conditions, it would return a certain exit status that I set myself, such as return 0, return 1, return 2, etc..

I bolded the last past because that is where I'm stuck on. I cannot figure out how to use the execl system call to load and run my a7.sh script.

  1. Relevant commands, code, scripts, algorithms:
    Everything is being in a bash shell.

This is a7.sh:

sameNos() {
     if [ $# -eq 3 ];
          if [[ $1 == $2 && $1 == $3 && $2 == $3 ]];
               return 0;
          elif [[ ( $1 == $2 ) || ( $1 == $3 || ( $2 == $3 ) ]];
               return 1;
          else
               return 2;
          fi
     else
               return 3;
     fi
}

a=17
b=5
c=23
sameNose $a $b $c
estat=$?
echo $estat
if [ $estat -eq 3 ]; then
     echo "There are not three parameters"
elif [ $estat -eq 0 ]; then
     echo "All three parameters have equal values"
elif [ $estat -eq 1 ]; then
     echo "At least two of three parameters have equal values"
else
     echo "Conditions are not met"
fi
  1. The attempts at a solution (include all code and scripts):

Now here's the code I have for a7.c, the second half of the assignment.

#include <stdio.h> //print()
#include <unistd.h> //fork()
#include <stdlib.h>

int main(void){
     printf("It is a nice day today. ");
     pid_t p;

     p = fork();
     if (p > 0){  //this is PARENT
          printf("This is the PARENT, the PID is %d , the CHILD PID is %d\n", getpid(), p);
          sleep(4);
     }

     else if (p == 0) //this is CHILD
          execl("/home/dl121/cs371/assn7", "a7.sh", NULL); //First part is the directory a7.sh is in, the second part is the actual script to be run
     }

I bolded the part that is not giving me any results. I compiled a7.c in the file directory using (cc -o a7.out a7.c) and ran a7.out. It gives me output for the PARENT process, and it gives me the correct output for it. However, after it sleeps, nothing happens in the CHILD process. It just ends. It does not run my a7.sh script at all. I made sure that it was even making it that far through the program by printing a statement below the execl system call, and it does make it that far. I am not getting any errors or anything, but it's obviously not running my a7.sh script. I just can't understand how execl works and what exactly needs to be put in the ( ) to run whatever I want to run. The only example done in class by the professor was calling the ps command which is a command and not a script.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Western Illinois University, Macomb, IL, USA, MCQUILLAN, CS371

Any help would be great. This is my final assignment of the semester and I've done great up until this part. I think my problem should be pretty simple to solve. I've spent a lot of time on this assignment and this is the last problem that needs to be fixed. Thanks in advance!

A typical call to execl might be something like this:

execl("/bin/date", "date", 0, 0);

The first argument is a path to a file to be executed. Did I understand that you made the first argument a directory? You can't execute a directory.

1 Like

The thing is I don't know what the direct path to the file would be. What I put was obviously wrong. And yea, I made my first argument a directory. How can I find what the direct path would be?

You are creating the file. Pay attention to where you put it.

Obviously I know where I put it ("/home/dl121/cs371/assn7"), I still am not understanding it. Thanks for the help though..not.

I do not understand how perderabo has earned your ridicule by honestly trying to help you. If you do not understand it than this is probably more your fault than it is his, no?

Having said this: in your script is a typo which prevents it from being executed properly: search for "sameNos" and you should see it yourself.

The "execl()" system call loads (a copy of) an executable file into memory, prepares a runtime environment for it and starts this loaded copy of the file as a process. Obviously to load it into memory it needs the name of the file, not of some directory, no?

I hope this helps.

bakunin

1 Like

I am not saying it is his fault that I do not understand it. I am the one asking for help, but he went on the give me an example of using execl using a built-in command(date) when I specifically said in the OP that I already have been given an example of using execl with a command ("The only example done in class by the professor was calling the ps command which is a command and not a script.") when I'm trying to figure out how to use it with calling a script I have made myself. He then proceeded to tell me to pay attention to what I'm doing which irked me because I feel like I'm paying quite a bit of attention to what I'm doing.

With that said, thanks for pointing out my typo.

Where you say it needs the name of the file, are you saying I should do this?...

execl("/home/dl121/cs371/assn7/a7.sh", "a7.sh", NULL);

Because, I do have the name of the file in the second argument already.

---------- Post updated at 12:26 PM ---------- Previous update was at 12:19 PM ----------

OKAY, got it.

Changed it to:

execl("/home/dl121/cs371/assn7/a7.sh", "a7.sh", NULL);

And it ran my a7.sh script. Although, it printed my output twice in both the parent and child class. Not sure if that's how it's supposed to be, but at least it's running the script which is the main thing I wanted. Thanks.

1 Like

It seems to be clearer now where your problem was: between what you called "built-in" programs (I'd call it "system provided executables" and other executable programs there is absolutely no difference. You can (and you found that out yourself already) treat a shell script the same way you treat any other executable, binary or not.

The only difference is that binaries are directly exectuable: they consist of code natively executable on the processor, so once they get loaded they can be executed by it. In contrast scripts are input files for a certain binary - the shells binary, to be precise.

When you execute a shell script the OS will determine its filetype (there is a set of rules laid down in "/etc/magic") and if it comes to the conclusion it is a shell script it will load its default shell and feed it the script as input. If you want to override this and execute it with a different shell (or any other different binary or because your "/etc/magic" lacks the rule to identify what filetype your script is) you can use a "shebang" to specify the commando processor to feed the script to:

#! /path/to/some/shell/binary
... rest of the script...

will first load this binary and the feed it the scripts text as input. Because for the script language "#!" is still a comment it won't bother the shell at all.

By the way, perderabo has written a whole article about shebangs in the Frequently-Asked-Questions board: The Shebang explained

I hope this helps

bakunin

2 Likes

Makes more sense now, thanks.

The exec() in the kernel uses a simple rule, like the rules in /etc/magic. While /etc/magic is only used by the file command.