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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:31:57+00:00 2026-05-13T22:31:57+00:00

I want the parent process to take the arguments to main() and send the

  • 0

I want the parent process to take the arguments to main() and send the characters in them one at a time to the child process through a pipe starting with argv[1] and continue through the rest of the arguments.(one call to write for each character).

I want the child process to count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever.

What am i doing wrong? do i need to use exec()?

output that isnt correct:

    ~ $ gc a03
gcc -Wall -g a03.c -o a03
~ $ ./a03 abcd ef ghi

child: counted 12 characters
~ $

here is the program..

#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{

    int length = 0;
    int i, count;

    int     fdest[2];          // for pipe
    pid_t   pid;              //process IDs
    char    buffer[BUFSIZ];



    if (pipe(fdest) < 0)          /* attempt to create pipe */
        printf("pipe error");

    if ((pid = fork()) < 0)  /* attempt to create child / parent process */

    {
        printf("fork error");
    } 


    /* parent process */
    else if (pid > 0) {      
        close(fdest[0]);

        for(i = 1; i < argc; i++)    /* write to pipe */
        {
            write(fdest[1], argv[i], strlen(argv[1]));
        }

        wait(0);

    } else {   

        /* child Process */
        close(fdest[1]);

        for(i = 0; i < argc; i++)
        {
            length +=( strlen(argv[i])); /* get length of arguments */
        }

        count = read(fdest[0], buffer, length);
        printf("\nchild: counted %d characters\n", count);

    }

    exit(0);

}
  • 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-13T22:31:58+00:00Added an answer on May 13, 2026 at 10:31 pm

    You said that “the child process should not use the arguments to main() in any way whatsoever”. However, I see that your child process is using argc. Doesn’t this defeat your restriction?

    You also say that you want “one call to write for each character”. Your current implementation uses one call to write for each argument, not each character. Was this a typo? If not, you will want to use something more like this:

    char nul='\0', endl='\n';
    for (a=1; a < argc; ++a) {
        for (c=0; c < strlen(argv[a]); ++c) {
            write(fdest[1], &argv[a][c], 1);
        }
        write(fdest[1], &nul, 1);
    }
    write(fdest[1], &endl, 1);
    

    This will write one character at a time, with each argument as a NULL-terminated string and a newline character at the end. The newline is only there to serve as a marker to indicate that there is no more data to send (and is safe to use since you won’t be passing in a newline in a CLI argument).

    The child process will just need to be a loop that reads incoming bytes one by one and increments a counter if the byte is not '\0' or '\n'. When it reads the newline character, it breaks out of the input processing loop and reports the value of the counter.

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

Sidebar

Related Questions

I want my child process to send a signal to the parent process without
Hi i want to cast the Parent object to Child object in C# public
I have parent process that opens child process . I need to perform some
On OS X 10.4/5/6: I have a parent process which spawns a child. I
I am using an unnamed pipe for interprocess communication between a parent process and
I want to compile a source C in a parent process and then ,
Basically the child process runs indefinitely until killed in the background, and I want
I have a child process which runs in a pseudo terminal. The parent process
I'm creating child processes with fork() in C/C++. When the parent process ends (or
I want to return a unique status code to a waiting parent process from

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.