C
Clunixchit
am I lame ??
sounds like i didnt grab something !!!
ive changed my code now!!
whats wrong??
y for 2 loops, it prints 3 loops?
#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));
}
exit(0);
case -1 : perror("fork");
exit(-1);
default : waitpid(one,NULL,0);
two = fork();
if ( two == 0 ) {
write(desc,&B,sizeof(char));
waitpid(two,NULL,0);
}
close(desc);
system("cat aba.txt");
exit(0);
}
}
in the end i wish to output ABABABABAB
sounds like i didnt grab something !!!
ive changed my code now!!
whats wrong??
y for 2 loops, it prints 3 loops?
#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));
}
exit(0);
case -1 : perror("fork");
exit(-1);
default : waitpid(one,NULL,0);
two = fork();
if ( two == 0 ) {
write(desc,&B,sizeof(char));
waitpid(two,NULL,0);
}
close(desc);
system("cat aba.txt");
exit(0);
}
}
in the end i wish to output ABABABABAB