[C] exec system call

Hi again :wink: Now I want to make a program that will execute the programs with exec, asking the user if he wants the program to run in background or foreground.

scanf("%c",&caracter);
if (caracter=='y'){
	printf("Has decidido ejecutarlo en background\n");
	if((pid=fork())==0) {// fork para ejecutar en bg
		for (i=1; i<argc; i++)
			execl(argv,argv, 0);
		exit(0);
	}
}
else	{
	printf("Has decidido ejecutarlo en foreground\n");
	for (i=1; i<argc; i++)
		execl(argv,argv, 0);
	}

Problem is, it will only execute 1 program. If i try to write ./execute /bin/ls /bin/ls only once will be shown. What's worse, when I try to debug it with ddd, ddd will also crash with a segmentation fault (this happens on foreground code). :wall:

Edit: related topics below my post may have given me the answer: is it because when you call exec(), the code of my program won't execute any more?

Yes, that's it. exec() replaces your current program. You have to fork, whether you want it in the foreground or not. Just wait() for processes to finish if they're in the "foreground".

There's more to fg vs bg than just waiting, there's also things to do with terminal control I'm I'm only barely aware of. If I can't find the right man page, hopefully someone can fill us in.

1 Like

As always, I'm thankful for you answer :wink:

A process is running in the foreground if its process group is the same as its terminal's foreground process group.

/*
  To be complete and safe, you should open /dev/tty and pass the
resulting descriptor to tcgetpgrp, instead of assuming 0. But for the sake of
simplicity, it's OK here.
*/
int
isfg()
{
  return (tcgetpgrp(0) == getpgrp());
}
int
main(int argc, char *argv[])
{
  printf("Running in %s\n", isfg() ? "foreground" : "background");
  return 0;
}
bash-4.1$ gcc -o demo demo.c
bash-4.1$ ./demo
Running in foreground
bash-4.1$ ./demo &
[1] 3393
bash-4.1$ Running in background

[1]+  Done                    ./demo
bash-4.1$ 


To put a process in background, you can create a new process group for your process (with setpgid, for example), and then change the controlling terminal's foreground process group to a different one (like the process group id of the parent process, for example). This program will run in the foreground if you pass no arguments to it, and in the background if you pass any arguments:

int
main(int argc, char *argv[])
{

 if(argc > 1){
  setpgid(0,0) ;
  tcsetpgrp(0,getpgid(getppid()));
}
  printf("Running in %s\n", isfg() ? "foreground" : "background");
  return 0;
}
bash-4.1$ gcc -o demo demo.c
bash-4.1$ ./demo
Running in foreground
bash-4.1$ ./demo x
Running in background
bash-4.1$ 

But as Corona688 said: that's more on that. I tried to show you only the basic idea and code on this subject. Get your Unix Programming book and read it to understand this subject in deep.

2 Likes