exec

#include 
#include 
#include 

int main(void)
{
    pid_t pid;
    pid = fork();
    if (pid == -1) {
        perror("fork error!\n");
        exit(1);
    } else if (pid > 0) {
        sleep(1);
        printf("parentq\n");
    } else {
        //execlp("ls", "dedwede", "-a", NULL);
        //execlp("ls", "dedwede", "-la", NULL);
        execlp("ls", "ls", "-l", "-a", "-h", NULL);
    }
    
    return 0;
}
xpz@ubuntu:~/demo/exec$ ./a.out 
total 28K
drwxrwxr-x 2 xpz xpz 4.0K Aug 25 20:45 .
drwxrwxr-x 8 xpz xpz 4.0K Aug 25 20:34 ..
-rwxrwxr-x 1 xpz xpz 8.7K Aug 25 20:45 a.out
-rw-rw-r-- 1 xpz xpz  441 Aug 25 20:45 exec_test.cpp
-rw-rw-r-- 1 xpz xpz  441 Aug 25 20:45 exec_test.cpp~
parentq
xpz@ubuntu:~/demo/exec$ 
#include 
#include 
#include 
#include 

int main(void)
{
    int fd;
    fd = open("ps.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
    if (fd <0 ) {
        perror("open ps.out error!");
        exit(1);
    }
    
    dup2(fd, STDOUT_FILENO);
    
    execlp("ps", "ps", "ax", NULL);
    close(fd);
    
    return 0;
}
xpz@ubuntu:~/demo/exec$ ls
a.out  exec_dup2.cpp   exec_test.cpp   ps.out
dup    exec_dup2.cpp~  exec_test.cpp~
xpz@ubuntu:~/demo/exec$ cat ps.out 
   PID TTY      STAT   TIME COMMAND
     1 ?        Ss     0:26 /sbin/init auto noprompt
     2 ?        S      0:00 [kthreadd]
     4 ?        I<     0:00 [kworker/0:0H]
     6 ?        I<     0:00 [mm_percpu_wq]
     7 ?        S      1:26 [ksoftirqd/0]
     8 ?        I      0:27 [rcu_sched]
     9 ?        I      0:00 [rcu_bh]
    10 ?        S      0:00 [migration/0]
    11 ?        S      0:01 [watchdog/0]
    12 ?        S      0:00 [cpuhp/0]
    13 ?        S      0:00 [kdevtmpfs]
    14 ?        I<     0:00 [netns]
    15 ?        S      0:00 [rcu_tasks_kthre]
    16 ?        S      0:00 [kauditd]
    17 ?        S      0:00 [khungtaskd]
    18 ?        S      0:00 [oom_reaper]
    19 ?        I<     0:00 [writeback]
    20 ?        S      0:07 [kcompactd0]
    21 ?        SN     0:00 [ksmd]
    22 ?        SN     0:00 [khugepaged]
    23 ?        I<     0:00 [crypto]
    24 ?        I<     0:00 [kintegrityd]
    25 ?        I<     0:00 [kblockd]
    26 ?        I<     0:00 [ata_sff]
    27 ?        I<     0:00 [md]
    28 ?        I<     0:00 [edac-poller]
    29 ?        I<     0:00 [devfreq_wq]
    30 ?        I<     0:00 [watchdogd]
    33 ?        S      1:40 [kswapd0]
    34 ?        S      0:00 [ecryptfs-kthrea]
    76 ?        I<     0:00 [kthrotld]
    77 ?        I<     0:00 [acpi_thermal_pm]
    78 ?        S      0:00 [scsi_eh_0]
    79 ?        I<     0:00 [scsi_tmf_0]
    80 ?        S      0:00 [scsi_eh_1]
    81 ?        I<     0:00 [scsi_tmf_1]
    87 ?        I<     0:00 [ipv6_addrconf]
    97 ?        I<     0:00 [kstrp]
   115 ?        I<     0:00 [charger_manager]
   168 ?        I<     0:00 [mpt_poll_0]
   169 ?        I<     0:00 [mpt/0]
   170 ?        S      0:00 [scsi_eh_2]
   171 ?        I<     0:00 [scsi_tmf_2]
   172 ?        I<     0:00 [ttm_swap]
   173 ?        S      0:00 [irq/16-vmwgfx]
   175 ?        I<     0:34 [kworker/0:1H]
   176 ?        S      0:00 [scsi_eh_3]
   177 ?        I<     0:00 [scsi_tmf_3]
   178 ?        S      0:00 [scsi_eh_4]
   179 ?        I<     0:00 [scsi_tmf_4]
   180 ?        S      0:00 [scsi_eh_5]
   181 ?        I<     0:00 [scsi_tmf_5]
   182 ?        S      0:00 [scsi_eh_6]
   183 ?        I<     0:00 [scsi_tmf_6]
   184 ?        S      0:00 [scsi_eh_7]
   185 ?        I<     0:00 [scsi_tmf_7]
   186 ?        S      0:00 [scsi_eh_8]
   187 ?        I<     0:00 [scsi_tmf_8]
   188 ?        S      0:00 [scsi_eh_9]
   189 ?        I<     0:00 [scsi_tmf_9]
   190 ?        S      0:00 [scsi_eh_10]
   191 ?        I<     0:00 [scsi_tmf_10]
   192 ?        S      0:00 [scsi_eh_11]
   193 ?        I<     0:00 [scsi_tmf_11]
   194 ?        S      0:00 [scsi_eh_12]
   195 ?        I<     0:00 [scsi_tmf_12]
   196 ?        S      0:00 [scsi_eh_13]
   197 ?        I<     0:00 [scsi_tmf_13]
   198 ?        S      0:00 [scsi_eh_14]
   199 ?        I<     0:00 [scsi_tmf_14]
   200 ?        S      0:00 [scsi_eh_15]
   201 ?        I<     0:00 [scsi_tmf_15]
   202 ?        S      0:00 [scsi_eh_16]
   203 ?        I<     0:00 [scsi_tmf_16]
   204 ?        S      0:00 [scsi_eh_17]
   205 ?        I<     0:00 [scsi_tmf_17]
   206 ?        S      0:00 [scsi_eh_18]
   207 ?        I<     0:00 [scsi_tmf_18]
   208 ?        S      0:00 [scsi_eh_19]
   209 ?        I<     0:00 [scsi_tmf_19]
   210 ?        S      0:00 [scsi_eh_20]
   211 ?        I<     0:00 [scsi_tmf_20]
   212 ?        S      0:00 [scsi_eh_21]
   213 ?        I<     0:00 [scsi_tmf_21]
   214 ?        S      0:00 [scsi_eh_22]
   215 ?        I<     0:00 [scsi_tmf_22]
   216 ?        S      0:00 [scsi_eh_23]
   217 ?        I<     0:00 [scsi_tmf_23]
   218 ?        S      0:00 [scsi_eh_24]
   219 ?        I<     0:00 [scsi_tmf_24]
   220 ?        S      0:00 [scsi_eh_25]
   221 ?        I<     0:00 [scsi_tmf_25]
   222 ?        S      0:00 [scsi_eh_26]
   223 ?        I<     0:00 [scsi_tmf_26]
   224 ?        S      0:00 [scsi_eh_27]
   225 ?        I<     0:00 [scsi_tmf_27]
   226 ?        S      0:00 [scsi_eh_28]
   227 ?        I<     0:00 [scsi_tmf_28]
   228 ?        S      0:00 [scsi_eh_29]
   229 ?        I<     0:00 [scsi_tmf_29]
   230 ?        S      0:00 [scsi_eh_30]
   231 ?        I<     0:00 [scsi_tmf_30]
   232 ?        S      0:00 [scsi_eh_31]
   233 ?        I<     0:00 [scsi_tmf_31]
   234 ?        S      0:00 [scsi_eh_32]
   235 ?        I<     0:00 [scsi_tmf_32]
   298 ?        S      0:23 [jbd2/sda1-8]
   299 ?        I<     0:00 [ext4-rsv-conver]
   330 ?        Ss     0:15 /lib/systemd/systemd-journald
   356 ?        Ss     0:03 /lib/systemd/systemd-udevd
   359 ?        Ssl    0:00 vmware-vmblock-fuse /run/vmblock-fuse -o rw,subtype=vmware-vmblock,default_permissions,allow_other,dev,suid
   382 ?        Ssl    8:48 /usr/bin/vmtoolsd
   436 ?        Ssl    0:02 /lib/systemd/systemd-timesyncd
   789 ?        Ssl    0:09 /usr/lib/accountsservice/accounts-daemon
   793 ?        Ssl    0:03 /usr/sbin/rsyslogd -n
   795 ?        Ss     0:19 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
   807 ?        Ss     0:12 avahi-daemon: running [ubuntu.local]
   810 ?        Ss     0:03 /usr/sbin/acpid
   811 ?        S      0:00 avahi-daemon: chroot helper
   813 ?        Ss     0:00 /usr/bin/VGAuthService
   820 ?        Ssl    0:31 /usr/sbin/NetworkManager --no-daemon
   826 ?        Ss     0:00 /usr/lib/bluetooth/bluetoothd
   840 ?        Ss     0:01 /usr/sbin/cron -f
   844 ?        Ss     0:01 /lib/systemd/systemd-logind
   845 ?        Ssl    0:26 /usr/lib/snapd/snapd
   872 ?        Ssl    0:03 /usr/lib/policykit-1/polkitd --no-debug
   917 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux
   936 ?        SLsl   0:01 /usr/sbin/lightdm
   952 tty7     Ssl+   8:57 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
   990 ?        S      0:06 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/var/run/NetworkManager/dnsmasq.pid --listen-address=127.0.1.1 --cache-size=0 --conf-file=/dev/null --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
  1237 ?        Sl     0:00 lightdm --session-child 12 19
  1305 ?        SNsl   0:12 /usr/lib/rtkit/rtkit-daemon
  1349 ?        S<     0:00 [krfcommd]
  1363 ?        Ssl    0:01 /usr/lib/upower/upowerd
  1408 ?        Ssl    0:00 /usr/lib/colord/colord
  1462 ?        Ssl    0:18 /usr/bin/whoopsie -f
  1726 ?        Ss     0:02 /lib/systemd/systemd --user
  1728 ?        S      0:00 (sd-pam)
  1776 ?        Sl     0:01 /usr/bin/gnome-keyring-daemon --daemonize --login
  1783 ?        Ss     0:03 /sbin/upstart --user
  1865 ?        S      0:00 upstart-udev-bridge --daemon --user
  1873 ?        Ss     0:15 dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-CoLqtWI603
  1885 ?        Ss     0:01 /usr/lib/x86_64-linux-gnu/hud/window-stack-bridge
  1916 ?        S      0:06 upstart-dbus-bridge --daemon --system --user --bus-name system
  1924 ?        S      0:04 upstart-dbus-bridge --daemon --session --user --bus-name session
  1925 ?        S      0:00 upstart-file-bridge --daemon --user
  1930 ?        Ssl    0:26 /usr/lib/x86_64-linux-gnu/bamf/bamfdaemon
  1938 ?        Sl     0:01 /usr/lib/gvfs/gvfsd
  1944 ?        Ssl    0:52 /usr/bin/ibus-daemon --daemonize --xim --address unix:tmpdir=/tmp/ibus
  1945 ?        Sl     0:00 /usr/lib/gvfs/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
  1957 ?        Sl     0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher
  1963 ?        Sl     0:00 /usr/lib/ibus/ibus-dconf
  1964 ?        Sl     0:14 /usr/lib/ibus/ibus-ui-gtk3
  1966 ?        Sl     0:02 /usr/lib/ibus/ibus-x11 --kill-daemon
  1972 ?        S      0:01 /usr/bin/dbus-daemon --config-file=/etc/at-spi2/accessibility.conf --nofork --print-address 3
  1977 ?        Sl     0:02 /usr/lib/at-spi2-core/at-spi2-registryd --use-gnome-session
  1987 ?        Sl     0:14 /usr/lib/ibus/ibus-engine-simple
  2002 ?        Ssl    0:18 /usr/lib/x86_64-linux-gnu/hud/hud-service
  2004 ?        Ssl    0:13 /usr/lib/unity-settings-daemon/unity-settings-daemon
  2015 ?        Ssl    0:02 /usr/lib/gnome-session/gnome-session-binary --session=ubuntu
  2032 ?        Ssl    0:33 /usr/lib/x86_64-linux-gnu/unity/unity-panel-service
  2038 ?        Z      0:00 [check_gl_textur] 
  2067 ?        Ssl    0:00 /usr/lib/x86_64-linux-gnu/indicator-messages/indicator-messages-service
  2068 ?        Ssl    0:00 /usr/lib/x86_64-linux-gnu/indicator-bluetooth/indicator-bluetooth-service
  2071 ?        Ssl    0:01 /usr/lib/x86_64-linux-gnu/indicator-power/indicator-power-service
  2077 ?        Sl     0:01 /usr/lib/dconf/dconf-service
  2078 ?        Ssl    0:16 /usr/lib/x86_64-linux-gnu/indicator-datetime/indicator-datetime-service
  2080 ?        Ssl    0:03 /usr/lib/x86_64-linux-gnu/indicator-keyboard/indicator-keyboard-service --use-gtk
  2082 ?        Ssl    0:01 /usr/lib/x86_64-linux-gnu/indicator-sound/indicator-sound-service
  2089 ?        Ssl    0:02 /usr/lib/x86_64-linux-gnu/indicator-printers/indicator-printers-service
  2091 ?        Ssl    0:02 /usr/lib/x86_64-linux-gnu/indicator-session/indicator-session-service
  2096 ?        Ssl    0:02 /usr/lib/x86_64-linux-gnu/indicator-application/indicator-application-service
  2122 ?        Sl     0:01 /usr/lib/evolution/evolution-source-registry
  2128 ?        Sl     0:01 /usr/bin/pulseaudio --start --log-target=syslog
  2134 ?        Ssl   29:29 compiz
  2175 ?        Sl     0:01 /usr/lib/evolution/evolution-calendar-factory
  2188 ?        SLl    2:39 /usr/bin/gnome-software --gapplication-service
  2190 ?        Sl     0:37 nautilus -n
  2191 ?        Sl     5:14 nm-applet
  2192 ?        Sl     0:03 /usr/lib/unity-settings-daemon/unity-fallback-mount-helper
  2196 ?        Sl     6:49 /usr/bin/vmtoolsd -n vmusr --blockFd 3
  2197 ?        Sl     0:02 /usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1
  2209 ?        Sl     0:00 /usr/lib/gvfs/gvfs-udisks2-volume-monitor
  2224 ?        Ssl    0:01 /usr/lib/udisks2/udisksd --no-debug
  2225 ?        Sl     0:07 /usr/lib/evolution/evolution-calendar-factory-subprocess --factory contacts --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.Calendarx2175x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/Calendar/2175/2
  2248 ?        Sl     0:00 /usr/lib/gvfs/gvfs-goa-volume-monitor
  2258 ?        Sl     0:00 /usr/lib/gvfs/gvfs-gphoto2-volume-monitor
  2266 ?        Sl     0:00 /usr/lib/gvfs/gvfs-afc-volume-monitor
  2274 ?        Sl     0:00 /usr/lib/gvfs/gvfs-mtp-volume-monitor
  2301 ?        Sl     0:00 /usr/lib/evolution/evolution-addressbook-factory
  2302 ?        Sl     0:12 /usr/lib/evolution/evolution-calendar-factory-subprocess --factory local --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.Calendarx2175x3 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/Calendar/2175/3
  2320 ?        Sl     0:04 /usr/lib/evolution/evolution-addressbook-factory-subprocess --factory local --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.AddressBookx2301x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/AddressBook/2301/2
  2326 ?        Sl     0:06 zeitgeist-datahub
  2343 ?        S      0:00 /bin/sh -c /usr/lib/x86_64-linux-gnu/zeitgeist/zeitgeist-maybe-vacuum; /usr/bin/zeitgeist-daemon
  2346 ?        Sl     0:01 /usr/lib/gvfs/gvfsd-trash --spawner :1.5 /org/gtk/gvfs/exec_spaw/0
  2349 ?        Sl     0:08 /usr/bin/zeitgeist-daemon
  2377 ?        Sl     0:04 /usr/lib/x86_64-linux-gnu/zeitgeist-fts
  2426 ?        Sl     0:00 /usr/lib/gvfs/gvfsd-metadata
  2436 ?        Sl     0:51 /usr/lib/gnome-terminal/gnome-terminal-server
  2443 pts/1    Ss     0:05 bash
  2466 ?        Sl     0:10 update-notifier
  2495 ?        Ss     0:11 gpg-agent --homedir /home/xpz/.gnupg --use-standard-socket --daemon
  2533 ?        Sl     0:05 /usr/lib/x86_64-linux-gnu/deja-dup/deja-dup-monitor
  3818 ?        Sl     0:23 /usr/lib/x86_64-linux-gnu/notify-osd
 13584 ?        I<     0:00 [xfsalloc]
 13585 ?        I<     0:00 [xfs_mru_cache]
 13593 ?        S      0:00 [jfsIO]
 13594 ?        S      0:00 [jfsCommit]
 13595 ?        S      0:00 [jfsSync]
 14180 ?        Sl     0:00 /usr/lib/gvfs/gvfsd-network --spawner :1.5 /org/gtk/gvfs/exec_spaw/3
 14226 ?        Sl     0:00 /usr/lib/gvfs/gvfsd-dnssd --spawner :1.5 /org/gtk/gvfs/exec_spaw/6
 36112 ?        I<     0:00 [kworker/u257:0]
 36113 ?        I<     0:00 [kworker/u257:1]
 37386 ?        Sl     1:02 gedit /home/xpz/demo/environ/test_environ.cpp
 48272 ?        Ss     0:00 /usr/sbin/cupsd -l
 48273 ?        Ssl    0:00 /usr/sbin/cups-browsed
 48760 ?        S      0:00 /sbin/dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /var/run/dhclient-ens33.pid -lf /var/lib/NetworkManager/dhclient-cd0d2336-1ab2-3225-b7ca-ce93e4bf8c05-ens33.lease -cf /var/lib/NetworkManager/dhclient-ens33.conf ens33
 49062 ?        I      0:02 [kworker/0:2]
 49420 ?        I      0:00 [kworker/u256:0]
 49446 ?        I      0:00 [kworker/0:1]
 49471 ?        I      0:00 [kworker/u256:2]
 49534 ?        I      0:00 [kworker/0:0]
 49577 pts/1    R+     0:01 ps ax
xpz@ubuntu:~/demo/exec$ 

 

你可能感兴趣的:(C++,linux)