Reputation: 1396
I'm attempting to do the following...
Create a new process
obtain the PID of a process
put a process to sleep for a defined period of time
check process ID on the terminal
My program runs, but the output is not what I'd expect it to be and I'm not quite sure where I'm going wrong. Thanks for your time, I really appreciate it!
Code:
int main() {
int i;
pid_t pid=0;
/** use fork() system call to create a new process */
/** if the pid returned by fork() is negative, it indicates an error */
if(fork()<0) {
perror("fork");
exit(1);
}
if(pid==0) {
printf("child PID= %d\n", (int) getpid());
for(i=0; i<10; i++) {
printf("child: %d\n", i);
/** put process to sleep for 1 sec */
sleep(1);
}
} else {
/* parent */
/** print the parent PID */
printf("parent PID= %d\n", (int) getpid());
for(i=0; i<10; i++) {
printf("parent: %d\n", i);
sleep(1);
}
}
exit(0);
}
The output is supposed to look something like...
parent PID=8900
child PID=4320
parent:0
child:0
child:1
parent:1
child:2
parent:2
child:3
parent:3
parent:4
etc.
But i'm getting...
child PID= 97704
child: 0
child PID= 106388
child: 0
child: 1
child: 1
child: 2
child: 2
child: 3
child: 3
child: 4
child: 4
child: 5
child: 5
child: 6
child: 6
child: 7
child: 7
Upvotes: 0
Views: 2358
Reputation: 1
Use pid
for comparisons instead of calling another fork()
. Set pid
equal to fork()
so you can compare it to check for errors in the pid
.
int main() {
int i;
pid_t pid=0;
pid = fork();
/** use fork() system call to create a new process */
/** if the pid returned by fork() is negative, it indicates an error */
if(pid<0) {
perror("fork");
exit(1);
}
if(pid==0) {
printf("child PID= %d\n", (int) getpid());
for(i=0; i<10; i++) {
printf("child: %d\n", i);
/** put process to sleep for 1 sec */
sleep(1);
}
} else {
/* parent */
/** print the parent PID */
printf("parent PID= %d\n", (int) getpid());
for(i=0; i<10; i++) {
printf("parent: %d\n", i);
sleep(1);
}
}
exit(0);
}
Upvotes: 0
Reputation: 1435
As already stated above, you're not assigning pid
to anything, so it's always zero. You should also change your condition to be pid
instead of calling another fork()
.
int main() {
int i;
pid_t pid=0;
pid = fork(); /* Add this */
/** use fork() system call to create a new process */
/** if the pid returned by fork() is negative, it indicates an error */
if(pid<0) { /* Change this */
perror("fork");
exit(1);
}
Also, don't be surprised if your expected output still looks different than what you're expecting. There is no way to tell when the child or parent will be called (especially if you took out the sleep). It depends on a variety of things.
EDIT: I see what you're saying. You want to check the process ID via the terminal? You could add a getchar();
to the end of your program to pause the program from exiting then you could open another terminal and run ps
. You need to make sure you add #include <stdio.h>
, to use it though.
Upvotes: 1
Reputation: 473
You don't really assign the output of fork() to pid, so pid stays at zero.
Upvotes: 2