[C] exec system call


 
Thread Tools Search this Thread
Top Forums Programming [C] exec system call
# 1  
Old 12-12-2011
[C] exec system call

Hi again Smilie 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.

Code:
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[i],argv[i], 0);
		exit(0);
	}
}
else	{
	printf("Has decidido ejecutarlo en foreground\n");
	for (i=1; i<argc; i++)
		execl(argv[i],argv[i], 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). Smilie

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?

Last edited by lamachejo; 12-12-2011 at 10:39 AM..
# 2  
Old 12-12-2011
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-12-2011
As always, I'm thankful for you answer Smilie
# 4  
Old 12-12-2011
A process is running in the foreground if its process group is the same as its terminal's foreground process group.

Code:
/*
  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());
}

Code:
int
main(int argc, char *argv[])
{
  printf("Running in %s\n", isfg() ? "foreground" : "background");
  return 0;
}

Code:
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:

Code:
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;
}

Code:
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.
These 2 Users Gave Thanks to pflynn For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

need help with system call

hi everyone i wrote a system call and compiled the kernel succesfully... my system call is in a file in the kernel folder named my_syscall1.c (kernel/my_syscall1.c) the header file for this system call i added it in the folder include like this include/my_syscall1/my_syscall1.h my problem is... (2 Replies)
Discussion started by: demis87
2 Replies

2. Programming

system call

I have a cgi script which is called after certain time interval, which has this: system ("ls -l /tmp/cgic* | grep -v \"cgicsave.env\" | awk '{print $5}'"); During the execution of this script,the output is 0 sometimes. But due to this the system call is not working at all and doesnt o/p... (2 Replies)
Discussion started by: xs2punit
2 Replies

3. UNIX for Dummies Questions & Answers

How to run two commands from a exec call in a c program

Hi, I have to run two commands one after another from a c program. How can i do this with exec system calls. i tried giving them as argument to execv but it is not working.please help thanks (3 Replies)
Discussion started by: suryashikha
3 Replies

4. Shell Programming and Scripting

Perl variables in exec or system

I am new in Perl. I am working in simple script and the varibles are working well outside the exec or system command. but they don't work as parameters to exec or system command. The script is attached. please help. (8 Replies)
Discussion started by: ahmed_zaher
8 Replies

5. Shell Programming and Scripting

system call

Hi, How to write a system calls in a script ? > cd $HOME > ls -ltr thanks in advance.. (10 Replies)
Discussion started by: hegdeshashi
10 Replies

6. Shell Programming and Scripting

How to execute piped command using exec or system

Hi All, I want to execute a piped command like 'ls /opt | grep xml' using array as parameters list. How can I do that? (2 Replies)
Discussion started by: bharadiaam
2 Replies

7. UNIX for Advanced & Expert Users

exec to call specific function in C prog

I would like to call a particular function in a C program using execl(). Is this possible using execl or anyother function ? Thanks (2 Replies)
Discussion started by: vpraveen84
2 Replies

8. Programming

alternatives of exec() system function

Hi , Can anybody name any System Function in C/C++ for Sun-Solaris (unix) platform which can serve the alternative of execl() system function. Actually I am calling a fork-execl() pair and then making an interprocess communication between these two(parent-child process). But the problem is... (3 Replies)
Discussion started by: Raj Kumar Arora
3 Replies

9. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

10. Programming

exec() system call

hi there, i was reading about the exec() function. and if i m not wrong, exec() kills your present process and starts a new process in its place. the process id remains the same. then it says if exec is successful the text data and stack are overlayed by new file! - i dont get this part "only... (2 Replies)
Discussion started by: a25khan
2 Replies
Login or Register to Ask a Question