Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1006079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:24:05+00:00 2026-05-16T08:24:05+00:00

void turtle (int gtot) { int msg; fcntl(gtot,F_SETFL,O_NONBLOCK); read(gtot,&msg,4); gotoxy(12, 21); printf(The value of

  • 0
void turtle (int gtot)
{
    int msg;
    fcntl(gtot,F_SETFL,O_NONBLOCK);
    read(gtot,&msg,4);

    gotoxy(12, 21); printf("The value of buffer for turtle is %d",msg);

    //react to god's message
    xcoor += msg;
    msg = 0;
            sleep(sleep_time);
 }

 void god (int gtot )
 {
        char choice, sign;
        int distance;
        scanf("%c",&choice);
        //choice = getchar();
        gotoxy(1,18);
        printf("Enter the distance which should be moved");
        fflush(stdout);
        gotoxy(50, 14);
        scanf ("%d", &distance);
        int newd = distance;
        //printf ("The distance that is to be moved by %d", distance);

        if (choice == 'h')
        {
            write(gtoh,&distance,4);
        }
        else if (choice == 't')
        {
            write(gtot,&newd,4);
            gotoxy(12,23);
                            printf("I am writing %d as the number", distance); 
                            fflush(stdout);
        }
        //printf("You chose %s", &choice);
        sleep(sleep_time);
    }
    main(){
int gtot[2];
pipe (gtot);
pid_turtle = fork ();
if (pid_turtle == 0)
  {
      close (gtot[1]);
      turtle (gtot[0]);
  }

pid_god = fork ();
if (pid_god == 0)
  {
      close (gtot[0]);
      god (gtot[1]);
  }
   }

When I write from the pipe from God function to the Turtle function. I expect it to return nothing when the user gives no input and the numbers when the user gives any. But the printf statement is printing outputs like

       The value of buffer for turtle is 0106368
       The value of buffer for turtle is 05291328

Which seems to me like the memory address of the number. What is the error that I am making in the program.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:24:05+00:00Added an answer on May 16, 2026 at 8:24 am

    Several observations about your program:

    In function turtle:

    1. You do not initialize the variable msg.
    2. You set the gtot file descriptor for O_NONBLOCK.
    3. BUT you do not check the return value of read.

    This is a significant problem. read is returning immediately and you are printing the uninitialized value of msg.

    • The way you fork and close is also contributing. You have closed gtot[1] prior to fork-ing the “god” process. If you choose to use this one-parent of two-child-processes approach, don’t close the file handles until you are done fork-ing.

    • Also, it appears that you intended for at least the turtle function, and probably the god function, to loop. As written, your turtle function will immediately exit: it has no loop and it performs read with the O_NONBLOCK flag set.

    • But wait, there is more. When you do call fcntl to set O_NONBLOCK, what you are doing is also resetting every flag other than O_NONBLOCK. Here is a function taken from the libc documentation which handles the other flags while setting or resetting the non-blocking flag:

    /* Set the O_NONBLOCK flag of desc if value is nonzero,
    or clear the flag if value is 0.
    Return 0 on success, or -1 on error with errno set. */

    int
    set_nonblock_flag (int desc, int value)
    {
      int oldflags = fcntl (desc, F_GETFL, 0);
      /* If reading the flags failed, return error indication now. */
      if (oldflags == -1)
        return -1;
      /* Set just the flag we want to set. */
      if (value != 0)
        oldflags |= O_NONBLOCK;
      else
        oldflags &= ~O_NONBLOCK;
      /* Store modified flag word in the descriptor. */
      return fcntl (desc, F_SETFL, oldflags);
    }
    

    There are some other things which could also be contributing to your problem:

    • It would be best to replace every occurence of “4” with sizeof(int) wherever you are calling read and write. It is possible that integers are 8 bytes on your machine (64 bit integer?), although pretty unlikely. In the future, it is more likely that int would be 8 bytes, and your code is very fragile in this regard.

    There is also something “Strange” about your program that I am observing:

    • Why is the decimal output from printf being prefixed with a leading 0 (zero)? That would be an octal representation, but the printf specifier is not "%o". "%d" should not show leading zeros unless you use a width-specifier with a leading zero, a la "%08d". So I don’t know what to make about that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }
void PrintNow(const std::vector<int> &v) { std::cout << v[0] << std::endl; } std::vector<int>().push_back(20); // this
void gctinp (char *inp, int siz) { puts (Input value: ); fgets (inp, siz,
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
void test(int p1[10], int p2) { int l1; int l2[10]; printf(params are at %d
void operator<< (const Integer& left, const Integer& right) { cout << \n: << right.i;
void MainWindow::addRadioToUI() { int button_cunter=4; while(!database.isEmpty()) { button_cunter++; QPushButton *one = new QPushButton(Play: +name(get_r.getTrackId()));
void (int a[]) { a[5] = 3; // this is wrong? } Can I
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
void some_func(int param = get_default_param_value());

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.