Search code examples
cexecxterm

How to execl an Xterm


I wonder how can I invoke an Xterm through execl. For example, for the following code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include "util.h"
#define XTERM_PATH "/usr/bin/xterm"
#define XTERM "xterm"
int main() {
    int exStatus;
    pid_t childpid;
    childpid = fork();
    if ( childpid == -1 ) {
        perror( "Failed to fork" );
        exit( 0 );
    }
    if ( childpid == 0 ) {      // Child process

        exStatus = execl( XTERM_PATH, XTERM, "+hold", "-e", "./shi", "shi", (char *)NULL );
        if ( exStatus == -1 ) {
            perror( "Failed to execute shell" );
            exit( 0 );  
        }
    }
    else {
        wait(NULL);
    }   
    return 0; 
}

Where shi is just a simple program print out HelloWorld onto the screen. After I run the program, Xterm did not show up. I wonder what went wrong.


Solution

  • If you use -hold (not +hold) for any recent xterm version, and your program really is in the current directory (as indicated by "./shi"), then an xterm should show up.