Sponsored Content
Full Discussion: Program output overflows
Top Forums UNIX for Dummies Questions & Answers Program output overflows Post 302306103 by hansel13 on Saturday 11th of April 2009 03:20:08 AM
Old 04-11-2009
Program output overflows

I Wrote code that forks into two processes, a parent process, and a child process. The parent process will take the arguments to main() and send them one character at a time to the child process through a pipe (one call to write for each character). The child process will count the characters sent to it by the parent process and print out the number of characters it received from the parent.


The child should return normally (in other words, do not have the parent kill the child).

For some reason when my program executes, once in a while the command line prompt will come up before my program is done outputting. so basically the commandline gets a line of my output.. So I just press enter and everything is OK.

Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>    /* standard I/O routines.                */
#include <unistd.h>   /* defines pipe(), amongst other things. */

#define ARRAY_SIZE 1000

int main(int argc, char* argv[])
{
    int data_pipe[2];
    int pid;
    int i = 0;
    char readbuffer[ARRAY_SIZE];
    char *in_str; /*= argv[1];*/

    for(i = 0; i < sizeof(readbuffer); i++)
        readbuffer[i] = 0;

    for(i = 1; i < argc; i++)
    {
        if(i > 1)
        {
            argv[i] = strncat(argv[i - 1],argv[i],1000);
        }
    }

    if(argc > 1)
        in_str = argv[i - 1];

    //printf("Printing string: %s\n", argv[i - 1]);
    //printf("The sentence entered is %u characters long.\n",strlen(argv[i - 1]));

    /* first, create a pipe. */
    pipe(data_pipe);

    /* now fork off a child process, and set their handling routines. */
    pid = fork();

    if(pid == -1)       /* fork failed. */
    {
        perror("fork");
        exit(1);
    }
    else if(pid == 0)           /* inside child process. */
    {
        close(data_pipe[1]);
        read(data_pipe[0], readbuffer, sizeof(readbuffer));
        printf("child: counted %d characters\n", strlen(readbuffer));

        //for(i = 0; i < 30; i++)
        //      printf("%d:  %c\n", i, readbuffer[i]);
        //printf("Received String: %s\n", readbuffer);
        sleep(5);
        exit(0);
    }
    else /* inside parent process */
    {
        close(data_pipe[0]);
        while (*in_str != '\0')
        {
            in_str++;
            write(data_pipe[1], &in_str, 1);
        }
        exit (0);
    }

    return 0;
}

 

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Capturing output from C++ program

Hi I have a C++ program that generates a lot of log information on the console, I need this output (printed using printf function) to go to a file since I will use crontab to schedule the job. I know I can do this: myprog > myfile but I don't know how to enter this in crontab. I use... (3 Replies)
Discussion started by: GMMike
3 Replies

2. UNIX for Dummies Questions & Answers

how can I use the stream output in other program

Hello I wander if im doing : ls -l and its giving me lets say 3 results : -rw-r--r-- 1 blah other 1789 May 19 2003 foo.c -rw-r--r-- 1 blah other 1014 May 19 2003 foo.h -rw-r--r-- 1 blah other 270 May 19 2003 foo1.c now I would like to use the first... (1 Reply)
Discussion started by: umen
1 Replies

3. Programming

C program Output

The output I got for this pgm is "4 4 4 4". Can any one help me to understand how I got this output. Also please suggest me some links to learn about argumnets evaluation in C. # include <stdio.h> void func(int a, int b, int c, int d) { printf("%d %d %d %d", a, b, c, d); } int... (3 Replies)
Discussion started by: arunviswanath
3 Replies

4. Programming

ambiguity in program output

1 #include <fcntl.h> 2 3 main(int argc, char *argv) 4 { 5 char buf; 6 int fd, count = 0; 7 8 if (argc > 1) 9 fd = open(argv, O_RDONLY); 10 else 11 fd = 0; /* Use standard input */ 12 13 while (read(fd, buf, 1) > 0) { 14 if (count < 5) write(1, buf, 1); 15 ... (3 Replies)
Discussion started by: bishweshwar
3 Replies

5. Programming

Please help me with my output for my program

Hello All, iam a new memeber today i joined this forum. hope i will get help. the below program takes input strings and give reverse of input string. && mv /home/test1/programs/display /home/test1/programs/old echo " Please enter the test " read a echo "$a" > file wc -c file > file1 perl... (1 Reply)
Discussion started by: ameyrk
1 Replies

6. Shell Programming and Scripting

Output Display in a perl program

Hi All, I have created a sample perl program in one of the unix environment as below #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<H1>Hello World</H1>"; When I execute it in unix, I get the below Content-type: text/html <H1>Hello World</H1> However, when I... (1 Reply)
Discussion started by: mr_manii
1 Replies

7. Shell Programming and Scripting

getting no output with my perl program

hi, i have posted the same kind of the question in some other forum of the same site. but realized that it is supposed to be here so i am reposting it .this is the perl script written to check for particular pattern. my file 1 would look like this hwk:678:9878:asd:09: abc cfgb 12 nmjk ......... (3 Replies)
Discussion started by: anurupa777
3 Replies
preap(1)                                                           User Commands                                                          preap(1)

NAME
preap - force a defunct process to be reaped by its parent SYNOPSIS
preap [-F] pid... DESCRIPTION
A defunct (or zombie) process is one whose exit status has yet to be reaped by its parent. The exit status is reaped via the wait(3C), waitid(2), or waitpid(3C) system call. In the normal course of system operation, zombies may occur, but are typically short-lived. This may happen if a parent exits without having reaped the exit status of some or all of its children. In that case, those children are reparented to PID 1. See init(1M), which periodically reaps such processes. An irresponsible parent process may not exit for a very long time and thus leave zombies on the system. Since the operating system destroys nearly all components of a process before it becomes defunct, such defunct processes do not normally impact system operation. However, they do consume a small amount of system memory. preap forces the parent of the process specified by pid to waitid(3C) for pid, if pid represents a defunct process. preap will attempt to prevent the administrator from unwisely reaping a child process which might soon be reaped by the parent, if: o The process is a child of init(1M). o The parent process is stopped and might wait on the child when it is again allowed to run. o The process has been defunct for less than one minute. OPTIONS
The following option is supported: -F Forces the parent to reap the child, overriding safety checks. OPERANDS
The following operand is supported: pid Process ID list. EXIT STATUS
The following exit values are returned by preap, which prints the exit status of each target process reaped: 0 Successfully operation. non-zero Failure, such as no such process, permission denied, or invalid option. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu (32-bit) | +-----------------------------+-----------------------------+ | |SUNWesxu (64-bit) | +-----------------------------+-----------------------------+ SEE ALSO
proc(1), init(1M), waitid(2), wait(3C), waitpid(3C), proc(4), attributes(5) WARNINGS
preap should be applied sparingly and only in situations in which the administrator or developer has confirmed that defunct processes will not be reaped by the parent process. Otherwise, applying preap may damage the parent process in unpredictable ways. SunOS 5.10 26 Mar 2001 preap(1)
All times are GMT -4. The time now is 08:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy