Jump to content

Curious.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
rescueme

rescueme

    Newbie

  • Members
  • PipPipPip
  • 30 posts
I tried google-in the string_invert function and no luck yet. and uhm Surfed through this site for a bit. feel constraint now.




Feel free to give me some guidance!!?!

Edited by rescueme, 06 February 2009 - 11:46 AM.


#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
I think you'll be needing a for/while loop, strings, arrays, maybe even pointers.
Posted Image

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Given a string my_string, you can access individual elements of it with the subscript operator [], just as if it were an array of characters.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
rescueme

rescueme

    Newbie

  • Members
  • PipPipPip
  • 30 posts
Thanks.

I have those thoughts in mind.

#5
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
main points:

1. fork(), what doesn't its return value signal?
2. what's changed and what's unchanged when a process fork to produce a child process.

This is indeed a very good problem. With this solved, you most like will understand what fork does.

#6
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Without the constraint that each process can output but one character, this will be a complete beginner problem. I know rescueme is not.

#7
rescueme

rescueme

    Newbie

  • Members
  • PipPipPip
  • 30 posts
Hm...
technically Ive never been exposed to fork yet..
which is why i am kinda confused in this.

all i know is that fork is a process where it copies the process and then identifies the parent and child.

everything else is pooff. clueless.

Edited by rescueme, 06 February 2009 - 11:47 AM.


#8
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I have solved you problem with both recursion and loop. As it's an assignment, I can not paste my code. Basically, you can tink the following code to see what fork does.


#include <unistd.h>
#include <stdio.h>

int main()
{
    pid_t pid;
     switch( (pid=fork() ) )
    {
     case -1: /* fork failed */
           fprinf(stderr, "fork() failed!\n");
           exit (-1);
     case 0: /* child process */
           printf("In the child process\n");
           printf("Code in this block will not be"
                  "executed in the parent process!"
           );
           break;
     default: /* parent process */
           wait(); /* guess what it does? */                      
           printf("In the parent process\n");
           printf("Code IN THIS bLock wiLL not be"
                  "executed In the cHiLD process!"
           );
           break;
    }
    printf("Code here will be executed by both parent"
             "and child processes\n"
             "If you see these messages twice, than it's"
             " correct!\n"
    );
    

}


Above code is not compiled and tested. Try to run it and tink with it, hopefully you will understand what fork does. That's the whole point why you are given this assignment.

Edited by Lance, 05 February 2009 - 07:15 PM.
with -> will


#9
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Revised. Have almost everything you need to do your job.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    pid_t pid;
     switch( (pid=fork() ) )
    {
     case -1: /* fork failed */
           fprintf(stderr, "fork() failed!\n");
           exit (-1);
     case 0: /* child process */
           printf("In the child process\n");
           printf("Code in this block will not be"
                  "executed in the parent process!"
           );
           break;
     default: /* parent process */
           wait(); /* guess what it does? */                      
           printf("In the parent process\n");
           printf("Code IN THIS bLock wiLL not be"
                  "executed In the cHiLD process!"
           );
           break;
    }
    printf("Code here will be executed by both parent"
             "and child processes\n"
             "If you see these messages twice, than it's"
             " correct!\n"
    );
}


#10
rescueme

rescueme

    Newbie

  • Members
  • PipPipPip
  • 30 posts
Thank you!

I believe in my course book I was given a piece such as this. except i was more like this.


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>


int main(void) {

pid_t childpid;
childpid == fork();
if (childpid == -1) {

perror (*Failed to fork*) ;
return 1;

}

if {childpid==0)
printf(*I am child %ld\n", (long)getpid()};

else
printf(*I am parent%ld\n", (long)getpid()};
return 0;
}

I am just really confused with the whole idea of using string_

in this whole process.

#11
rescueme

rescueme

    Newbie

  • Members
  • PipPipPip
  • 30 posts
Wow,, your pretty amazing with this stuff.

Edited by rescueme, 06 February 2009 - 11:45 AM.


#12
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Yeah, fork() is very confusing, because its so different from other functions.

You need actually draw a flow chart, with both parent process and child process, to understand why it should return value as it does.

It will take a while before it makes sense to you. In unix, a process can be created only through fork. All other stuff that creates new processes do it through fork, except probably the init process.

Edited by Lance, 05 February 2009 - 07:52 PM.
and -> create