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 520777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:10:51+00:00 2026-05-13T08:10:51+00:00

this is my function: void connection(int sock) // sock is a descriptor of socket

  • 0

this is my function:

    void connection(int sock)   // sock is a descriptor of socket
{
        char buffer[MAX];
        int n;                 // number of bytes read or write into a socket
        int f;
        f = fork();
if(write(sock,"HELLO\n", 5) < 0)
{
perror("Error: \n");
}
write(sock, "\n> ",3);
do {

memset(buffer,'0',MAX); // 

n = read(sock,buffer,MAX -1 );

if (strncmp("get",buffer,3) == 0)
        {

        if(f == 0)
        {
        write(sock, "TOP:\n",4);
        dup2(sock,1);
        if(execl("/usr/bin/top","/usr/bin/top","-bn1",0) == -1)
        write(sock, "ERROR",5);

        }
        else

        {
        waitpid(f, NULL, 0);
        write(sock, "\n> ",3);
        }

        }
else if (strncmp("quit",buffer,4) == 0)
{
        write(sock, "EXIT\n",4);
        close(sock);
        exit(0);
}

else
{
write(sock,"Wrong order\n", 12);
write(sock, "> ",2);
}

}
while(n);
}

This function is responsible for information exchange among client and server. The server should sends the results of “top” action.

This function is a part of TCP server program.

When client connect with server with telnet help, he see:

> Hello
> //(and here I can write "get", to see top results)

I’ d like to see “>” again, after call “get”, so I’ ve put:
else

        {
        waitpid(f, NULL, 0);
        write(sock, "\n> ",3);
        }

But I do not see my “>”. I can see it, only when I write in telnet something else. What should I put in my child process in fork, to invoke my parent process?

Regards.

  • 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-13T08:10:51+00:00Added an answer on May 13, 2026 at 8:10 am

    1) memset(buffer,'0',MAX); writes the ASCII character ‘0’ to all bytes in your buffer. Therefore, there is no guarantee that the string you read in null-terminated.

    2) Your “command line” handling doesn’t handle a closed socket right. It exits the do...while loop, returning from connection() … which is probably not what you want. You probably want it to do the same thing as a “quit” command. The “quit” command cose should break; instead of exit(0);, and the exit(0); should be put after the while(n);.

    3) The child process and parent process will, right after fork(), have the same files open. Each process should close the files it will no longer use. At the very least, this means the parent (f != 0) needs to close sock because it is now the property of the child. Also, the server (actually, listener) process should go back to listening for more connections after spawning the child process. To do this, put this after the f = fork() line:

    if(f != 0)  // Or just 'if(f)' if your coding standards allow it.
    {   // Parent continues here
        close(sock);
        return; // Or 'return f;' if you want to save the child's process ID for later.
    }
    // Child starts here
    

    If the parent process has files open that the child should not mess with, they should be closed right after this if(f != 0) block.

    4) There’s no guarantee that a call to read() will get you a single line, unless something is performing line buffering (maybe your terminal?).

    5) You should write your prompt out right before you do the read(), inside your do...while loop.

    6) When you execl(), your current process goes away, and there’s no way to get back. I recommend you investigate the popen() set of commands and use them instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.