creating child processes

From: Clunixchit (chitlesh_at_gmail-dot-com.no-spam.invalid)
Date: 05/27/05


Date: Fri, 27 May 2005 07:45:58 +0000 (UTC)

am I lame ??
sounds like i didnt grab something !!!
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



Relevant Pages

  • Re: Memory Errors please help
    ... I have placed a test that for loops up to 182 and the program errors ... char * document; ... Attempted to read or write protected memory ... This is because a c-style string takes an extra byte to hold the null ...
    (microsoft.public.vc.language)
  • Re: Nested loop with no overlap
    ... > all the loops are for the same integers range. ... void swap(char *l, char *r) ... void reverse ... int compare ...
    (comp.programming)
  • Re: Reversing a string
    ... 100000 loops, best of 3: 6.24 usec per loop ... Note that in the second version, I changed the code a little bit so that it no longer iterates over every char in the string and instead just calls listo convert it to a list of chars in order to call list.reverseon it. ...
    (comp.lang.python)
  • creating child process
    ... sounds like i didnt grab something!!! ... whats wrong?? ... y for 2 loops, it prints 3 loops? ... int main{ ...
    (comp.lang.c)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)