|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
bb666 sorted your problem?
|
| Sponsored Links |
|
|
|
|||
|
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? |
|
|||
|
Quote:
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 mainLast edited by jj1814; 02-07-2002 at 01:06 PM.. |
|
|||
|
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 |
|
||||
|
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) |
|
||||
|
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 |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |