0

I have to take a user input number 'n' in the parent process and then pass it to the child process.The child process then takes 'n' user input values and stores them in an array.It then call a thread and send this array as an argument.The thread sums all the values in the array and send it back to the child process which prints it.

   #include<stdio.h>
   #include<stdlib.h>
   #include<unistd.h>
   #include<sys/types.h>
    #include<pthread.h>
    #include<fcntl.h>

   void *sum(void *a)
 {printf("in thread" );
int * arr=(int *)a;
int i;
int sum=0;
int size=sizeof(arr)/sizeof(arr[0]);
for(i=0;i<size;i++)
{
    sum=sum +arr[i];
}
pthread_exit(sum);
 }
int main()
{
int pipefd[2];
pid_t childpid;
pthread_t tid;

pipe(pipefd);
int r;
int n;

childpid=fork();


if (0==childpid)
{


    printf("in child process" );
    close(pipefd[1]);
    read(pipefd[0],r,sizeof(int));

    close(pipefd[0]);
    int *ret;
    int a[r];
    int i;

    for (i = 0; i <r; i++)
    {   printf("enter values: ");
        scanf("%d",&a[i]);
    }



    pthread_create(&tid,NULL,sum,(void *)a);
    pthread_join(tid,(void *)&ret);
            printf("%d",*ret );


}
else
{   printf("in parent process" );
printf("enter a number" );
    scanf("%d",&n);
    close(pipefd[0]);
    write(pipefd[1],n,sizeof(int));
    close(pipefd[1]);

}
return 0;
 }

I have checked this code a dozen of times and nothing seems to be wrong.The process stops after taking the value of 'n'.The child process never runs.

SynAck
  • 427
  • 5
  • 19

1 Answers1

0

Both read and write expect to receive the memory address of a buffer to use. You need to take the address of the variables you're trying to fill.

E.g.

write(pipefd[1], &n, sizeof(int));

and

read(pipefd[0], &r, sizeof(int));

By the way, the child process most likely is running. It's just your value for r is coming back as 0. Simple debugging trick: Use fprintf(stderr, "stuff to print"); to check your results at various stages. You can easily verify the child process is running, for example.

Hod
  • 2,236
  • 1
  • 14
  • 22
  • 1
    okay now the scanf() in the child process won't work.'enter values' appears n times but there is no place to input these values.I put fprintf in thread function and thanks now i know that thread is atleast running – SynAck Mar 07 '17 at 20:55
  • Check this SO post: http://stackoverflow.com/questions/26687602/stdin-stdout-and-stderr-are-shared-between – Hod Mar 07 '17 at 20:59
  • 1
    the answer says he has never taken stdin in a child process .But my question says that i have to take user input in the child process – SynAck Mar 07 '17 at 21:09
  • You can pass a file descriptor yourself. `dup` stdin to something different. You can either read from that or try to redup it back to stdin in the child process. – Hod Mar 07 '17 at 21:57
  • See this post for most on `dup` with stdin, etc. http://stackoverflow.com/questions/9084099/re-opening-stdout-and-stdin-file-descriptors-after-closing-them – Hod Mar 07 '17 at 22:05