LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2005, 03:37 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Question how to fork/ exec


i am trying to use fork and exec to print out hello, do an ls -a, then print world.

my program prints hello, does an ls -a twice on each file in pwd, then exits without printing world (output below).

how to make exec return and continue running my program.

thanks,
schneidz



schneidz@lq> uname -a -M -p
AIX lq 2 5 00yyyyyy4C00 powerpc IBM,7040-671
schneidz@lq> cat schneidz.c
#include "stdio.h"
#include "unistd.h"

main(int argc, char *argv[])
{
printf("\thello \n");
f_fork();
execlp("ls", "ls", "-a", 0);
printf("\tworld \n");
}
schneidz@lq> cc -g schneidz.c -o schneidz.x
schneidz@lq> schneidz.x
hello
. .. hello.tmp schneidz.c schneidz.x shh.txt
. .. hello.tmp schneidz.c schneidz.x shh.txt
 
Old 05-24-2005, 03:59 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
execlp() is being called in the parent and the child. I have no idea what f_fork() is, but normally you'd do something like if(!fork()) { execlp(whatever); } else { printf("\tworld \n"); }

That way execlp() is only called in the parent. fork() returns the PID of the child process to the parent, but always returns 0 to the child.
 
1 members found this post helpful.
Old 05-24-2005, 04:06 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: how to fork/ exec

Quote:
Originally posted by schneidz
how to make exec return and continue running my program.
You cannot make execlp() "return". It only returns if there was an error executing the new program.

execlp() replaces the current process with the new program to execute. The program that the process was running is removed from memory, and does not come back.

The only way around this is to first create another process using fork(). Then call execlp(), but only in the child process. This child process is then running "ls" instead of your program. The "ls" program will of course not print "World", so you'll need to do that in the parent process, which should still be running the original program.

fork() duplicates the current program in a new process. So after fork() 2 processes are running concurrently. This means the last message ("World") printed by the parent process may appear in the middle of the output of "ls -a". If you do not want this to happen, you can wait for the child to finish, and then print "World". You can use wait() for a child process to finish. See "man 2 wait" for more info.

This "fork() a child proces, exec() another program, and wait for it to finish" is needed quite often. Therefore there is a library function that does all of this at once: system()

Here's an example how use system() to do what you are trying:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    printf("\thello \n");
    system("ls -a");
    printf("\tworld \n");
    return 0;
}
(if the teacher really wants you to use fork() and execlp() and not system(), check out their man pages and the hints above)
 
1 members found this post helpful.
Old 05-24-2005, 06:06 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks guys:

system() will work just good for me.

the aix man page says to use f_fork if an exec is called directly after. i assumed it was to help return to the calling program.

take care,
schneidz
 
Old 05-24-2005, 09:26 PM   #5
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Actually it's probably similar to the old vfork which would not copy the parent's pages and page tables to the child (since you're just going to overlay them with an exec call anyhow). Modern systems that implement proper copy on write and don't need to copy pages and page table entries don't need such hints.
 
Old 06-21-2005, 12:52 AM   #6
mvan83
LQ Newbie
 
Registered: Jun 2005
Distribution: Fedora
Posts: 19

Rep: Reputation: 0
Quote:
Originally posted by schneidz
thanks guys:

system() will work just good for me.

the aix man page says to use f_fork if an exec is called directly after. i assumed it was to help return to the calling program.

take care,
schneidz
Just some advice since I had to deal with this issue a few months ago. system() is generally inefficient and can be dependant on the specific installation. system() actually invokes a new shell to execute the given command. For something like ls -a this is overkill. It is better to use an exec() variant. The last time I used system() was to execute something like ls *.mp3 and I couldn't figure out how to invoke regexp expansion using exec, so I used system instead.
 
Old 06-21-2005, 06:26 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by mvan83
The last time I used system() was to execute something like ls *.mp3 and I couldn't figure out how to invoke regexp expansion using exec, so I used system instead.
FYI: You can find more about wildcard expansion in "man 3 glob" and "man 3 wordexp".
 
Old 06-21-2005, 07:54 PM   #8
mvan83
LQ Newbie
 
Registered: Jun 2005
Distribution: Fedora
Posts: 19

Rep: Reputation: 0
Quote:
Originally posted by Hko
FYI: You can find more about wildcard expansion in "man 3 glob" and "man 3 wordexp".
Thank you. I did not know of those functions (although I guessed something like them existed). This seems to be a problem with a lot of people (including myself obviously). We need a function that does something common, but don't know what it's called or how to find it easily.
 
Old 08-30-2005, 08:41 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
system - how to pass a string

can someone tell me how to use a variable as a parameter to the system function? please see code and output below:
thx,

Code:
schneidz@lq> cat schneidz.c
#include <stdio.h>
#include "string.h"

int main(int argc, char *argv[])
{
    printf("\tprintf argv-1 = %s\n", argv[1]);
    system("ls -a -l %s", argv[1]);
    return 0;
}
schneidz@lq> ll
Directory of /u/schneidz/sp/labratt/temp:
total 40
drwxrwx---   2 schneidz  schneidz           512 Aug 30 09:35 ./
drwxrwx---   3 schneidz  schneidz           512 Aug 30 09:19 ../
-rw-rw----   1 schneidz  schneidz           176 Aug 30 09:34 schneidz.c
-rw-rw----   1 schneidz  schneidz             0 Aug 30 09:35 schneidz.empty
-rwxrwx---   1 schneidz  schneidz          4731 Aug 30 09:35 schneidz.x*
schneidz@lq> schneidz.x schneidz.empty
        printf argv-1 = schneidz.empty
ls: 0653-341 The file %s does not exist.

Last edited by schneidz; 08-30-2005 at 08:45 AM.
 
Old 08-30-2005, 10:06 AM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
char buf[some_sufficiently_big_number];

sprintf(buf, "ls -a -l %s", argv[1]);
system(buf);
 
1 members found this post helpful.
Old 08-30-2005, 11:50 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks itsme,

worked like a charm
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming: fork, exec and system memory atienza Programming 2 08-24-2005 07:26 AM
about fork() kpachopoulos Programming 2 02-14-2005 12:24 AM
what is the difference between Fork & exec augustus123 Linux - Newbie 1 11-29-2004 07:32 AM
fork() in c++ deveraux83 Programming 5 11-13-2004 03:12 PM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration