LinuxQuestions.org
Visit Jeremy's Blog.
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 08-18-2004, 02:51 PM   #1
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
X Error with system()/fork()


While running my 'execute' ( execute("echo hi") ) function, I get this error:
Quote:
hi
Xlib: unexpected async reply (sequence 0x2d8)!
system() by itself works without the error, but I want the 'execute' function to run a command and not hang the gui waiting for it to finish.

The execute function:
Code:
void execute(const char *cmd)
{
  switch(fork())
  {
    case -1:
      perror("execute: fork");
      return;
    case 0:
       if (system(cmd) == -1)
        printf("execute: fork failed");
    default:
  }
  return;
}

Last edited by aaa; 08-18-2004 at 02:58 PM.
 
Old 08-18-2004, 03:21 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Instead of using system() after fork(), try using one of the exec() functions after fork().
 
Old 08-18-2004, 03:57 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
or call exit() after youve run the command, although exec would be better. the problem is 2 processes are returning from your function both trying to access the Display* returned by XOpenDisplay which is confusing X.
 
Old 08-18-2004, 05:05 PM   #4
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Original Poster
Rep: Reputation: 47
exec appears more straightforward, but 'execlp("sh", "-c", "du -sh /", NULL)' says 'du -sh /: command not found'. I don't want to seperate the arguments myself as I don't know how many will be entered in.

Anyways, system() by itself with an '&' appears to work, but I keep getting random "Segmentation Fault"s. I think it is because I did not add the '&' properly.

Also, while using fork(), do I have to worry about zombie processes or anything like that?

Here is the function using 'execlp':
Code:
void execute(const char *cmd)
{
   switch(fork())   
   {
     case -1:
       perror("execute: fork");
       return;
     case 0:   
       if (execlp("sh", "-c ",  cmd, NULL) == -1)
         perror("execute: exec() failed");
       default:   
  }
  return;
}
Here is the function using system() with '&':
Code:
void execute(const char *cmd)
{
  char * copy = malloc(sizeof(cmd) + sizeof(char));
  strcpy(copy, cmd);
  strcat(copy, '&');
  system(copy);
  free(copy);
  return;
}
 
  


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
An error Occurred during the file system check. Dropping you to shell; the system wil aneikei Linux - Newbie 3 02-11-2010 07:38 PM
Programming: fork, exec and system memory atienza Programming 2 08-24-2005 07:26 AM
Changing the fork system call seshuch Programming 0 10-02-2004 05:18 AM
NETPERF: ERROR --> send_udp_stream: error on remote: Interrupted system call dravya Linux - General 1 05-29-2004 05:49 PM
error loading operating system (lilo error) KoKi Slackware 1 03-03-2004 04:46 PM

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

All times are GMT -5. The time now is 02:48 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