Results 1 to 2 of 2
Hi, I found this assignment on the Internet and decided to take the challenge, since i'm new to linux I haven't been coming along too good, the program ( written ...
- 01-23-2009 #1Just Joined!
- Join Date
- Jan 2009
- Posts
- 2
Print out the PID of the current and parent process
Hi, I found this assignment on the Internet and decided to take the challenge, since i'm new to linux I haven't been coming along too good, the program ( written using C ) has some issues, hope to get your help

here is are the instructions:
here is what i have so far:PHP Code:1. Print out the PID of the current and parent process.
2. Fork a child process that performs the following:
a. Announce its creation.
b. Print out its PID and the PID of its parent.
c. Sleep for 5 seconds.
d. Executes the outside executable described in Part II, giving the outside program at least three arguments.
3. Announce the creation of a child and report the child's PID.
4. Show the system's process chart, using "ps -afl" and the system() call.
5. Wait for the child to die and announce that death. Verify that the child died normally and print out the return code that the child process exited with ( see waitpid() man page for information ).
6. Announce that the main program will die and return 0.
PART II
1. Announce the start of it's execution.
2. Announce its PID and the Parent's PID.
3. Announce the name of the executable run (i.e. its name ). The name CANNOT be hard-coded into the child program!
4. Output each argument to standard output, labeling appropriately.
5. Exit, returning 0.
PHP Code:/*Program headers that will be neccessary during program run*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
int main(int arrigc,char **grriv)
{
/*Declare both parent and child PID before use*/
pid_t getpid(void);
pid_t getppid(void);
int child = 0;
char buffer[64];
int childpid, parentpid;
/**************CODE BLOCK 1******************/
/*Clear anything that is on the screen before strating the program*/
system("clear");
/*Code block 1 prints the process ID and the Parent Process ID*/
printf("Directory : %s\n",getcwd(buffer,64));
printf("%s \n",grriv[0]);
printf("%s: Hi everyone I'm a process\n",grriv[0]);
if ((childpid = getpid()) < 0)
{
perror("Error With Child PID\n");
exit(-1);
}
else
{
if ((parentpid = getppid()) < 0)
{
perror("Error with Parent PID\n");
exit(-1);
}
}
printf("%s: My PID is %d and my parent's PID is %d\n",grriv[0],getpid(), getppid());
/**************END CODE BLOCK 1************* */
/*************CODE BLOCK 2************************/
/*Code block 2 forks a child and prits its ID and its parent ID*/
printf("%s: Forking a Child off.\n",grriv[0]);
printf("Child: I have been created!\n");
printf("Child: My PID is %d and my parents's PID is %d\n",getpid(), getppid());
printf("%s: I created a child and its PID is %d\n",grriv[0],getpid());
printf("%s: Printing System process chart using 'ps-al' \n",grriv[0]);
system("ps -al");
/***********END CODE BLOCK 2***************/
/************CODE BLOCK 3*************************/
printf("%s: Waiting for my Child to die \n",grriv[0]);
printf("Executing %s with arguments\n",grriv[0]);
printf("%s: Starting to Execute\n",grriv[0]);
child = fork();
/************END CODE BLOCK 3************************/
}
how would i go about implementing number 2 ( D) ?
- 01-23-2009 #2
So a few general comments first:
1) The arguments to main() are traditionally called argc (argument count) and argv (argument vector). You can call them whatever you want, but it's much clearer to people reading your code to use these names, and some other programming languages put the arguments in an array called ARGV automatically.
2) You do not need to provide prototypes of the getpid() and getppid() functions. By having them there, it looks like you are going to declare those functions. In reality, the prototypes are imported from unistd.h.
3) You announce that you are going to create a child, but then don't do it. The child is only created once you actually call fork(). Your lines that start with "CHILD:" will actually be executed by the parent.
As for your specific question, you want to look into the exec*() family of system calls. Check the man page ("man 3 exec") for more information.
Good luck! I did this same exercise a while back, and I remember learning a lot from it.DISTRO=Arch
Registered Linux User #388732


Reply With Quote