0

I am doing a project in that I have to create a child process by using fork and then parent process tells to child process to execute another C program "read.c"(which reads all integers from a .txt file and compute average) by using execve then I have to send that average value to parent process through pipe. I don't know how to get "average value" the result of the "read.c" program in child process of "process.c" program. Some of may friends said that I have to pass file descriptor useful for pipe(pfd[2]) into execve and from other program(read.c) i have to use pipe to write data to process.c program. but i don't know how to do that so it is not working properly. I am posting my codes for both process.c and read.c and please tell me which changes I should make to make it perform well.

process.c

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

main()
{

    int pid;
    int pfd[2];
    int i;
    char string1[12];
    snprintf(string1,12,"%i",pfd[1]);

    char *args[] = {"read",&string1[0],NULL};

    pipe(pfd);
    pid = fork();

    if(pid == 0)
    {

        execve("read",args,NULL);

        int size = 100;
        char buf[size]; 

        close(pfd[1]);
        read(pfd[0],buf,size);
        printf("%s\n",buf);

    }

}

read.c

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


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

    int pfd[2];
    pfd[1]= atoi(argv[1]); 
    close(pfd[0]);

    FILE *ptr_file;
    char buf[1000];
    int sum = 0;
    int avg =0;
    int no = 0;
    int count = 0;

    ptr_file =fopen("data.txt","r");
    if (!ptr_file)
        return 1;

    while (fgets(buf,1000, ptr_file)!=NULL)
    {
        printf(" %s \n",buf);
        no = atoi(buf);
        sum = sum + no;
        printf("Sum = %d \n", sum);
        count++;
    }

    fclose(ptr_file);
    avg = sum/count;
    printf("Average : %d \n",avg);
    write(pfd[1],"hello",6);/*i just write here hello to see that it is working or not*/
    return 0;
} 

If you have any solution to get output from read.c file to child process of process.c then please tell me.

Coder
  • 1,415
  • 2
  • 23
  • 49
TusharU
  • 1
  • 3

1 Answers1

0

Your "read.c" should print the result to the standard output. So, open the file and calculate the result as you are doing. Then use a printf() to print the result as this. This can be run from command line and the output should go to the terminal.

printf("%f\n", result);
close (1); // Just in case, Making sure to flush for pipe

Here is code that establishes the pipe between parent and child. I think this should help to resolve your project.

Snip of process.c

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

int main (int argc, char *argv[], char *envp[])
{
    int fd[2], pid;

    pipe (fd);
    pid = fork()

    if (pid == 0)
    {
        /* Child process */
        close (1);       // Close STDOUT.
        dup(fd[1]);      // STDOUT will be fd[1]
        close (0);       // Close STDIN, Don't need this.
        close (2);       // Close STDERR, Don't need this.
        close (fd[0]);   // Don't need this.
        close (fd[1]);   // Don't need this.

        execvpe ("./read", argv, envp); // Choose your exec() version

        /* exec() family of calls never returns. Process reed executes here. */
    }
    else
    {
        /* Parent process executes here */

        char chr[256];
        float average = 0;
        int ret = 0;

        /* Parent process. Making it very simple without any dups etc. */
        /* No error checks are performed... */
        /* You may experiment with enhancements here!!! */

        ret = read (fd[0], chr, 255); // Blocking Read syscall
        if (ret <= 0) 
        {
            printf ("Error or Nothing is read. Debug the problem!!!\n");
            exit (1);
        }

        chr[ret] = '\0';
        printf ("Average from read Child process[PID is %d] = %s\n", pid, chr);
    }
}
sukumarst
  • 265
  • 1
  • 5