![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Can a child process return a specific value to a parent process ? | Ametis1970 | High Level Programming | 8 | 04-10-2008 12:22 AM |
| about child process | compbug | UNIX for Dummies Questions & Answers | 12 | 03-22-2006 07:55 PM |
| gdb to child process | shriashishpatil | UNIX for Advanced & Expert Users | 4 | 12-12-2005 07:57 AM |
| KDM child process | larryase | UNIX for Dummies Questions & Answers | 6 | 01-24-2005 05:41 PM |
| Child Process PID | skannan | High Level Programming | 2 | 06-10-2002 08:54 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
hai im programming a simple program like you wanted, but its not working properly !!! well first of all i want it to print A then B then A then B , five times but i dont know y "for" is not working ? with or without "for" it prints only "AB" if this program works ill include it in my own script for my FC3 box!! can you point whats wrong with my code? Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
int main(){
pid_t one, two;
char A = 'A';
char B = 'B';
int i=0;
int desc = open ("aba.txt", O_CREAT | O_RDWR , 0700 );
if ( desc < 0 ) {
perror ( "open");
exit(-1);
}
lseek(desc,0,sizeof(char));
for (i=0;i<5;i++){
printf("%d\n",i);
one = fork();
if ( one == 0 ) {
write(desc,&A,sizeof(char));
exit(0);
}
else {
two = fork();
if ( two == 0 ) {
write(desc,&B,sizeof(char));
}
exit(0);
}
waitpid(one,NULL,0);
waitpid(two,NULL,0);
}
close(desc);
system("cat aba.txt");
}
|
|
||||
|
am I lame ?? sounds like i didnt grab something !!! ive changed my code now!! whats wrong?? y for 2 loops, it prints 3 loops? Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
int main(){
pid_t one, two;
char A = 'A';
char B = 'B';
int i=0;
int desc = open ("aba.txt", O_CREAT | O_RDWR , 0700 );
if ( desc < 0 ) {
perror ("open");
exit(-1);
}
lseek(desc,0,sizeof(char));
one = fork();
switch ( one ) {
case 0 : for(i=0;i<2;i++){
write(desc,&A,sizeof(char));
two = fork();
if ( two == 0 ) {
write(desc,&B,sizeof(char));
}
waitpid(one,NULL,0);
waitpid(two,NULL,0);
}
exit(0);
case -1 : perror("fork");
exit(-1);
default : close(desc);
system("cat aba.txt");
exit(0);
}
}
|
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|