Go Back   The UNIX and Linux Forums > Top Forums > Programming
.
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 01-31-2002
Registered User
 

Join Date: Jan 2002
Location: UK
Posts: 6
bb666 sorted your problem?
Sponsored Links
  #9 (permalink)  
Old 02-03-2002
Registered User
 

Join Date: Jan 2002
Location: romania
Posts: 10
Nope, not quite.
I was able to make a father process with more then one child by myself. But what I really want is after creating a number of child prcesses, starting with one of them as a father, it should again fork a few times, thus obtaing a tree-structure of processes.
So I guess I need some kind of recursive algorithm to do that. But here comes the problem: if I fork in a recursive way, I can't seem to get that tree structure right.
And also another problem: using the program shown by Perderabo and also my own, I changed the line:
printf("I am a child process and my pid is %d\n", getpid());
with this one:
printf("I am a child process id=%d father=%d\n",getpid(),getppid());
so I could see if the father is the right one, and after a few forks, all the child processes were generated by the process with pid=1. I avoided that by placing a sleep(2) command right before the end of the program, but I'm wondering: who's this process and is there another way to stop that?
  #10 (permalink)  
Old 02-07-2002
Registered User
 

Join Date: Feb 2002
Location: US
Posts: 14
Quote:
Originally posted by Perderabo
Your post helped me out with a fork I am writing, but I am having a little trouble because the execve command only executes the "ls" command when i type one in - no matter WHAT command I type in. if i modify execve("/bin/ls", .....) to only read execve("/bin/", .....), it doesnt execute ANYTHING. what do i need to do? Here is my code:

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

main()
 {
  cout<<"You have entered my 1814 shell"<<endl;
//  string command;
  char line[1024], *doit[20];
  int n, pid, parentpid, childpids[0];


  while (line !="exit")
      {

        cout<<"GIMME:#";
        cin.getline(line,sizeof(line));
        cout<<"The command you typed was: " <<line<<endl;
        if (strcmp(line,"exit")==0)
           return 0;

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());
        if (pid=fork())
          {
             int status;
             waitpid(-1, &status, 0);
             childpids[n]=pid;
          }
                  else
          {
             printf("I am a child process and my pid is %d\n", getpid());
             doit[0] = "ls";
             doit[1] = "-la";
             doit[2] = NULL;
             execve("/bin/ls", doit, NULL);
             exit(0);
          }
      printf("I am still the parent process and my pid is %d\n",
         getpid());
   }//end of while
 if (line =="exit")
 return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main


Last edited by jj1814; 02-07-2002 at 01:06 PM..
  #11 (permalink)  
Old 02-08-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
We are not allowed to do your schoolwork for you. But I'll give you a few hints. Code like this:
Quote:
Originally posted by jj1814

Code:
             doit[0] = "ls";
             doit[1] = "-la";
             doit[2] = NULL;
             execve("/bin/ls", doit, NULL);

is not going to run a "date" command. It's simply incredible that you think it might. execve is not going to ignore its arguments. You need to parse the command line and dynamically build arguments for the execve call.

And you should switch to execv anyway. As it is, you are clobbering the environment.

And, for now, require the user to enter "/usr/bin/date" rather than "date" so you don't need to search the PATH. You can get fancy later.
  #12 (permalink)  
Old 02-12-2002
Registered User
 

Join Date: Feb 2002
Location: US
Posts: 14
well...

i meant to have commented that code out. I was using it for another purpose. At any rate, I got my shell to work. thaaaaaaaaaanx


Code:
mars:$ cat modshell1.3.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
#include <iomanip.h>
#include <sys/wait.h>

int main()
 {
  cout<<"You have entered my 1814 shell"<<endl;
//  string command;
  char line[256], *doit[20];
  int n, pid, parentpid, childpids[0];


  while (line !="exit")
      {

        cout<<"GIMME:#";
        cin.getline(line, 256);
//     cin.getline(line,sizeof(line));
        cout<<"The command you typed was: " <<line<<endl;
        doit[0] = strtok(line, " ");
        int i=1;
        while (doit[i]=strtok(NULL, " "))
            i++;
        doit[i]=NULL;

        if (strcmp(line,"exit")==0)
           return 0;

        parentpid=getpid();
        printf("I am the parent process and my pid is %d\n", getpid());
        if (pid=fork())
 {
             int status;
             waitpid(-1, &status, 0);
             childpids[n]=pid;
          }
                  else
          {
             printf("I am a child process and my pid is %d\n", getpid());
             cout<<"Parameters are: "<<endl;
              for (int j=0; j<i; j++)
                  cout<<doit[j]<<endl;
             cout<<"       EXECUTE CHILD"<<endl;
             execve(doit[0], doit, NULL);
             exit(0);
          }
       cout<<"     KILL CHILD AND RETURN TO PARENT"<<endl;
       printf("I am still the parent process and my pid is %d\n",
         getpid());
   }//end of while
 if (line =="exit")
 return 0;

printf("I am still the parent process and my pid is %d\n", getpid());
}//end of main

  #13 (permalink)  
Old 02-13-2002
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Compiling with g++ on an OpenBSD 3.0 box, I get a "Memory Fault", and it dumps core when I try to run a command, like "ls".
The exit value is 139, and gdb reports a "Program received signal SIGSEGV, Segmentation fault.
0x1ad8 in main () "

Is this machine specific?
(I'm a no-C / C++ goof - Trying, though)
  #14 (permalink)  
Old 02-14-2002
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
I just downloaded the code and works for me on Sun. I don't see anything machine specific here. He is not testing for too many arguments and, if you entered 20 or more arguments to that ls, you could get a memory fault. Could that be it?
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
hell & mathematics ogoy Shell Programming and Scripting 5 05-26-2008 11:58 PM
hell and sqlite ogoy Shell Programming and Scripting 2 05-21-2008 01:07 AM
rpm hell! knmwt15000 UNIX for Dummies Questions & Answers 7 03-27-2002 06:06 AM
negative UID/GID?!! I can see 'em but what the hell do they mean?! hellz UNIX for Dummies Questions & Answers 2 09-07-2001 04:18 PM



All times are GMT -4. The time now is 05:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0