| | |
fork() and wait()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
I am supposed to create a chain of N processes, where N is the command line parameter. Each process creates a child process, prints out its own PID and its parent's PID, and then waits for its child to terminate by calling the wait() function.
Here is my code:
Any ideas why it's not working properly?
Example output:
[balhoffe1@gator labfork]$ ./lab2 4
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6382
and my parent pid=6381
I am the child
my pid=6381
and my parent pid=5670
[balhoffe1@gator labfork]$ I am the child
I am the child
my pid=6384
and my parent pid=1
my pid=6382
and my parent pid=1
I am the child
my pid=6383
and my parent pid=1
Here is my code:
c Syntax (Toggle Plain Text)
#include <stdio.h> int main(int argc, char *argv[]) { int number = atoi(argv[1]); int i; int pid; for (i=0; i<number-1; i++) { pid=fork(); if (pid==0) wait(); else { printf("I am the child\n"); printf("my pid=%d\n", getpid()); printf("and my parent pid=%d\n", getppid()); } } }
Example output:
[balhoffe1@gator labfork]$ ./lab2 4
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6382
and my parent pid=6381
I am the child
my pid=6381
and my parent pid=5670
[balhoffe1@gator labfork]$ I am the child
I am the child
my pid=6384
and my parent pid=1
my pid=6382
and my parent pid=1
I am the child
my pid=6383
and my parent pid=1
Last edited by ~s.o.s~; Mar 25th, 2007 at 1:50 pm. Reason: Added code tags, learn to use them.
![]() |
Similar Threads
- Common Novell Misconceptions / Why They Lie! (Novell)
- the difference of using fork() and exec() with system() (C++)
- ambiguous part 3 (C)
- fork() updating a taskTable (C)
- Fork and exec to run external needed app (C++)
- WindowsXP makes me wait! (Windows NT / 2000 / XP)
- Reduce 10 Second Scandisk Wait Time (Windows tips 'n' tweaks)
Other Threads in the C Forum
- Previous Thread: Linked List
- Next Thread: passing data around
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






