Visit Our UNIX and Linux User Community


fork()ing hell!!


 
Thread Tools Search this Thread
Top Forums Programming fork()ing hell!!
# 8  
Old 01-31-2002
bb666 sorted your problem?
# 9  
Old 02-03-2002
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  
Old 02-07-2002
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  
Old 02-08-2002
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  
Old 02-12-2002
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  
Old 02-13-2002
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  
Old 02-14-2002
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?

Previous Thread | Next Thread
Test Your Knowledge in Computers #46
Difficulty: Easy
Regarding computer networking, the exact meaning of the term VPN is 'virtual protected network.'
True or False?

8 More Discussions You Might Find Interesting

1. Programming

Issue when fork()ing processes

Hi guys! I'll simplify my problem. I have the following code: #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #define max 25 #define buffdim 50 void p1(); void p2();... (2 Replies)
Discussion started by: pfpietro
2 Replies

2. Shell Programming and Scripting

quoting hell - help needed!!

I am writing a bash script to automate the installation of web environment on a base install of Fedora. And I'm at the limit of my last nerve and my bash skills. My brain is screaming at me: "Give up and use perl", but I am trying to stick to bash since the script will modify the perl environment... (6 Replies)
Discussion started by: lbe
6 Replies

3. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

4. What is on Your Mind?

The Hell of colaboration in UNIX and Linux

I don't want to speak about the goods or bads of both kinds of Operating systems, I only want to share a little experience with you to comment it. I live in Spain and I have home some old unix systems, some of them that I want to sell or change for other things, like a pair of Sun Blade 2000... (0 Replies)
Discussion started by: Golfonauta
0 Replies

5. UNIX for Dummies Questions & Answers

Confussed as hell

:eek: (1 Reply)
Discussion started by: Kevinfine
1 Replies

6. Shell Programming and Scripting

hell & mathematics

I've been able to generate output based on the code scarfake provided me (thanks again man). A little background so everyone more or less knows whats going on: I needed code that would propagate a database with 100,000 entries, for capacity testing purposes, something like a stress test. ... (5 Replies)
Discussion started by: ogoy
5 Replies

7. Shell Programming and Scripting

hell and sqlite

Hi everyone, I have a requirement that requires me to fill an sqlite database with 100,000 entries (no duplicates). I will start out by giving the command that will insert the values necessary to populate the database: # sqlite /var/local/database/dblist "insert into list... (2 Replies)
Discussion started by: ogoy
2 Replies

8. UNIX for Dummies Questions & Answers

rpm hell!

I've just installed redhat 6.2 on one of my systems and am trying to install the gcc c compiler after downloading an rpm from the redhat site. The damn thing gives me: only major numbers <= 3 are supported by this version of RPM what do I do, it does the same with the latest rpm of php ... (7 Replies)
Discussion started by: knmwt15000
7 Replies

Featured Tech Videos