Programmstart unter Linux
-
Hier mal ein Beispiel, so zu sagen das "Hallo Welt" für fork/exec
#include <unistd.h> #include <sys/wait.h> #include <stdio.h> int main() { switch(vfork()) { case 0: //!WICHTIG: ein beliebter Fehler ist es nicht als ersten Programm Parameter/zweiter execl* Parameter) // den Aufrufsparameter der Funktion anzugeben if(execlp("echo","echo","hallo","welt!",NULL)==-1) { perror("execlp"); return 1; } break; case -1: perror("fork"); return 1; break; default: { /*blub blub*/ int status; wait(&status); } break; } }
> gcc -Wall -W -std=c99 -O3 exec-hello-world.c > ./a.out hallo welt! >
sollte dich vielleicht interessienten, wenn du in die Unix Programmierung einsteigen willst: http://www.tldp.org/LDP/lpg/index.html, weitere Hilfe findest du in den man: manpages und in der FAQ
-
Danke für die Antworten!!!!!
Ich moechte einen Simulator für meinen Roboter starten, was der mit execlp auch macht.
Ich kann diesen Simulator aber auch mit einer festen Karte/Umgebung starten lassen
"SRIsim -map Karte.wld" .
Wie kann ich dem Programm die Parameter -map Karte.wld übergeben.ThanX
frank
-
über die Parameter der Funktion execlp , siehe man: execlp(3) und mein Beispiel (da ruf ich das Programm echo mit den Parametern "echo" "hallo" "welt!" auf). Ist kinderleicht, denk immer nur daran, dass du als ersten Parameter den Namen des Programmes übergeben muss (argv[0] wird nicht implizit für das Programm gesetzt!)
-
Nur nebenbei, fuer forks welche man spaeter so oder so durch ein exec ersetzt, sollte man vfork verwenden.
-
[0x[90]| schrieb:
Nur nebenbei, fuer forks welche man spaeter so oder so durch ein exec ersetzt, sollte man vfork verwenden.
oh ja, hab das mal angepasst im Beispiel.
-
Nochmals besten Dank!!
Kann meinen Simulator prima starten und alles......
Klasse, bis dennFrank
-
vfork ist böse[tm]:
man vfork schrieb:
Historic Description
Under Linux, fork() is implemented using copy-on-write pages, so the only penalty incurred by fork() is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child. However, in the bad old days a fork() would require making a complete copy of the caller's data space, often needlessly, since usually immediately afterwards an exec() is done. Thus, for greater efficiency, BSD introduced the vfork system call, that did not fully copy the address space of the parent process, but borrowed the parent's memory and thread of control until a call to execve() or an exit occurred. The parent process was suspended while the child was using its resources. The use of vfork was tricky - for example, not modifying data in the parent process depended on knowing which variables are held in a register.Bugs
It is rather unfortunate that Linux revived this spectre from the past. The BSD manpage states: "This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork as it will, in that case, be made synonymous to fork. "Formally speaking, the standard description given above does not allow one to use vfork() since a following exec might fail, and then what happens is undefined.
Details of the signal handling are obscure and differ between systems. The BSD manpage states: "To avoid a possible deadlock situation, processes that are children in the middle of a vfork are never sent SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and input attempts result in an end-of-file indication."
Currently (Linux 2.3.25), strace(1) cannot follow vfork() and requires a kernel patch.
History
The vfork() system call appeared in 3.0BSD. In BSD 4.4 it was made synonymous to fork(), but NetBSD introduced it again, cf. http://www.netbsd.org/Documentation/kernel/vfork.html . In Linux, it has been equivalent to fork() until 2.2.0-pre6 or so. Since 2.2.0-pre9 (on i386, somewhat later on other architectures) it is an independent system call. Support was added in glibc 2.0.112.
-
Nun, die Sache ist eben die: Wenn execv fehlschlaegt muss sofort beendet werden (exit(-1)) um schwerwiegende Fehler zu vermeiden. Es empfiehlt sich vfork, da es im gegensatz zu fork den Stack/Heap und das Datensegment des Parents nicht in den Child kopiert, sondern den Child den Adressraum des Parents mitbenutzen laesst. Das kopieren der genannten Elemente waere sinnlos, wegen execv.
Das ganze dort oben trifft natuerlich auf Systemen die mit der COW Sematik arbeiten nicht zu, dort ist vfork identisch mit fork.
-
Die Nachteile von vfork wiegen die Vorteile bei weitem auf. Ich würde vfork nur dann benutzen, wenn ein fork fehlschlägt, weil zu wenig Ressourcen da sind.
-
vfork wurde nicht ohne Grund in Linux explizit eingeführt, nachdem es lange ein alias auf fork war. Man muss sich eben nur bewusst sein, dass man vfork benutzt
http://www.uwsg.iu.edu/hypermail/linux/kernel/9911.0/0230.html
http://www.uwsg.iu.edu/hypermail/linux/kernel/9911.0/0510.html
http://www.uwsg.iu.edu/hypermail/linux/kernel/0003.2/1464.html
http://www.uwsg.iu.edu/hypermail/linux/kernel/9911.0/0076.html
...