Getting unwanted characters at the end of a message - c

The following c program is used to send a message from parent process to the child process(created using fork()) via a pipe and is run on the linux terminal!
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc,char *arg[]){
pid_t child;
int pipefd[2];
int ret;
char message[20];
ret =pipe(pipefd);
if((child=fork())==0){
printf("The child process \n");
close(pipefd[0]);
write(pipefd[1],"Hello from parent",17);
}
else{
close(pipefd[1]);
read(pipefd[0],message,17);
printf("Message from parent %s\n",message);
}
return 0;
}
The above code prints the message "Hello from parent" but at the end of parent part an # sign is printed! what is the reason and how can i rectify it?

Send also null character that is at the end of the string. Same for reading.
write(pipefd[1],"Hello from parent",18);

Related

I want to create two communication between parent and child residing in different C files using ordinary pipes (IPC)

I am trying to send message from parant.c to child.c and I am successfully receiving it in the child.c
My question is that how can I send message back to the parent using second pipe from child.c I want the exact sequence of code.
Here is my parent.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int fd[2];
char buf[] = "HELLO WORLD!", receive[100];
if (pipe(fd))
{
perror("pipe");
return -1;
}
switch (fork())
{
case -1:
perror("fork");
return -1;
case 0:
// child
close(fd[1]); // close write end
dup2(fd[0], STDIN_FILENO); // redirect stdin to read end
close(fd[0]); // close read end
execl("./child", NULL); // execute child
default:
// parent
close(fd[0]); // close read end
write(fd[1], buf, sizeof(buf)); // write to write end
close(fd[1]); // close write end
wait(NULL);
}
printf("\nEND~\n");
return 0;
}
I am sending buf ("Hello world") to the child by executing ./child file.
Here is my child.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int fd[2];
pid_t pid = fork();
char buf[100], child_msg[] = "From Child: Hello Parent";
if (pipe(fd))
{
perror("pipe");
return -1;
}
switch (pid)
{
case -1:
perror("fork");
return -1;
case 0:
read(STDIN_FILENO, buf, sizeof(buf));
printf("%s ", buf);
close(fd[1]);
default:
wait(NULL);
}
return 0;
}
I am receiving Hello world in this file. but now how can I send child_msg back to the parent? I don't how to do that. I am stuck at this for last 14 hours.
From main pipe:
pipe() creates a pipe, a unidirectional data channel ...
So, you need 2 pipes, i.e., you have to create 2 pipes in your main process that will also be inherited by the child process.
From your code, you are execing another program, in such cases you might
be better off with other IPCs and not pipe!

Why does this function produce so many child processes?

I wrote this code in order to calculate the factorial of a number using processes and pipe(). I wanted to pass on the result from the child process to the child process. For example, to create calculate 5! the main which is the father sends the number 1 in the pipe. Then the first child is created and does 1*2, then it pushes in the pipe the number 2, the second child does 2*3 pushes the result in the pipe etc... Also, I use argv[1][0] thinking that we run the program like this (./ex3 5) where 5 is the number of which the factorial we would like to find. After running the program though, I noticed that a lot of child process was created (I only wanted 4). Why is that?
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int fd[2];
int w,count=2;
void child_creator(){
pid_t child;
child=fork();
if (child==0) {
close(fd[1]);
read(fd[0],&w,sizeof(w));
close(fd[0]);
w=w*count;
count++;
printf("I am child %d , my father is %d , the prod is %d\n",getpid(),getppid(),w);
sleep(1);
close(fd[0]);
write(fd[1],&w,sizeof(w));
close(fd[1]);
}
}
int main(int argc , char **argv){
int fact=argv[1][0]-'0';
pipe(fd);
w=1;
for (int i=0; i<fact-1; i++){
printf("this is i %d\n", i);
child_creator();
}
return 0;
}
After a suggested answer I tried this code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int fd[1000][2];
int w,count=1,j=0;
void child_creator(){
pid_t child;
j++;
pipe(fd[j]);
child=fork();
if (child==0) {
close(fd[j-1][1]);
read(fd[j-1][0],&w,sizeof(w));
close(fd[j-1][0]);
w=w*count;
printf("I am child %d , my father is %d , the prod is %d\n",getpid(),getppid(),w);
sleep(1);
close(fd[j-1][0]);
write(fd[j][1],&w,sizeof(w));
close(fd[j][1]);
exit(0);
}
}
int main(int argc , char **argv){
int fact=argv[1][0]-'0';
w=1;
for (int i=0; i<fact-1; i++){
count++;
child_creator();
sleep(2);
}
return 0;
}
Both the parent and child are returning to the for loop in main(). Since the child doesn't need to do anything after it writes its result, it should just exit rather than returning.
You also have problems with your handling of the pipe file descriptors. You do close(fd[1]) at the beginning of the child, but later try to write(fd[1],&w,sizeof(w)). You can't write to a closed FD. You don't need to close anything until the child is exiting, and exiting a process automatically closes all its files.
void child_creator(){
pid_t child;
child=fork();
if (child==0) {
read(fd[0],&w,sizeof(w));
w=w*count;
count++;
printf("I am child %d , my father is %d , the prod is %d\n",getpid(),getppid(),w);
sleep(1);
write(fd[1],&w,sizeof(w));
exit(0);
}
}

Cant read messages from processes using multiple pipes in c

Im creating pipes for multiple children. The code catches a signal fro the keyboard and then the child should sent a message to the father with its result. The handling of the signal works fine but i cant sent the result to the father. I am i doing something wrong here?
The code of the father process is :
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
pid_t *childs; //array for storing childs pids
int number_of_childs;//variable for the number of childs
int exit_count=0;
int *fd;
/*Handler for the SIGINT signal*/
void control_handler(int sig)
{
if (sig==SIGINT){
int j,bytes;
char message[512];
/*Sending SIGUSR1*/
for (j=0;j<number_of_childs;j++)
{
kill(childs[j],SIGUSR1);
}
/*Reading from PIPES*/
for (j=0;j<number_of_childs;j++)
{
close(fd[(2*j)+1]);
bytes = read(fd[(2*j)], message, sizeof(message));
printf("Read from: %s\n",message);
close(fd[2*j]);
}
}
if (sig==SIGUSR2)
{
exit_count++;
}
}
main (int argc,char *argv[]){
int i,child_status;
char cast[512];
char cast2[512];
char cast3[512];
int pid;
number_of_childs=atoi(argv[1]);
signal(SIGINT,control_handler);
signal(SIGUSR2,control_handler);
/*Creating array for children pipes*/
fd=(int*)malloc((2*number_of_childs)*sizeof(int));
/*array that holds the pids for every child used in sending signals*/
childs=malloc(number_of_childs*sizeof (pid_t));
for (i=0;i<number_of_childs;i++){
pid=fork();
/*Create pipes to communicate with all children*/
if(pipe(fd+(2*i))==-1)
{
perror("pipe");exit(1);
}
/*Fathers code goes here*/
if(pid!=0)
{
printf("Parent process: PID= %d,PPID=%d, CPID=%d \n",getpid(),getppid(),pid);
childs[i]=pid; // Keep all your childs in an array
printf("Child:%d\n",childs[i]);
}
/*If you are a child*/
else
{
/*Change the code for the childs and set the time of execution*/
sprintf(cast,"%d",i+1); // make the time char
sprintf(cast2,"%d",(2*i)+1); //make the pipe char
sprintf(cast3,"%d",number_of_childs);
execl("./Child.out","",cast,cast2,cast3,NULL);
}
}
/*Father should never terminate*/
while (exit_count!=number_of_childs);
printf("Father pospastex!!\n");
}
the code for the child children is :
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#define WRITE 0
#define READ 1
/*Global declerations*/
int alarmflag=0;
double result=0;
int count=0;
int global_pipe;
int *fd;
/*Handler for the alarm and SIGUSR1 signal*/
void signal_handler (int sig)
{
if(sig==SIGALRM)
{
printf("Im child with pid:%d im going to die my value is %lf \n",getpid(),result);
alarmflag=1;
}
if(sig==SIGUSR1)
{
count++;
char message[512];
if(count==1)
{
close(fd[global_pipe-1]);
sprintf(message,"%d,%lf",getpid(),result);
write(fd[global_pipe],message,strlen(message)+1);
close(fd[global_pipe]);
//printf("PID:%d report: %lf\n",getpid(),result);
}
if(count==2)
{
close(fd[global_pipe-1]);
sprintf(message,"%d,%lf",getpid(),result);
write(fd[global_pipe],message,strlen(message)+1);
close(fd[global_pipe]);
//printf("PID:%d report2 : %lf\n",getpid(),result);
//kill(getppid(),SIGUSR2);
//exit(0);
}
}
if(sig==SIGINT)
{
/*Do nothing*/
}
}
double p_calculation ()
{
int i=2;
result=3;
double prosimo=-1;
while(!alarmflag)
{
prosimo=prosimo*(-1);
result=result+(prosimo*(4/((double)i*((double)i+1)*((double)i+2))));
i=i+2;
}
}
main(int argc,char *argv[])
{
int pipe;
int size_fd;
size_fd=(atoi(argv[3]));
pipe=(atoi(argv[2]));
global_pipe=pipe;
fd=(int*)malloc(size_fd*sizeof(int));
/*handling signals*/
signal(SIGALRM,signal_handler);
signal(SIGUSR1,signal_handler);
signal(SIGINT,signal_handler);
/*Notify for execution time*/
printf("PID : %d with PPID : %d executing for %d seconds \n",getpid(),getppid(),atoi(argv[1]));
//printf("pipe:%d\n",pipe);
/*end this after the value passed as argument*/
alarm(atoi(argv[1]));
p_calculation();
/*Notify for finish*/
printf("Done!!!\n");
}
There are a number of problems related to your pipes:
you call pipe AFTER you call fork, so you end up with two independent pipes (one in the child and one in the parent). The parent listens to the one it creates (and noone is writing to), so it never sees anything.
you pass the index in your global fd array to the child rather than the file descriptor of the pipe. The child has its own global fd array that contains random garbage, so you are essentially writing to a random file descriptor rather than the pipe.
you don't close the unneeded pipe ends in the various processes they exist in, so you cna never reliably get EOFs
If you search for pipe+child here on stackoverflow, you'll see a large number of questions with example code trying to do things similar to what you are doing -- you may find it useful to read through those questions and answers.

Redirecting output from long-running child to parent process

I have two executables - parent process and it's child process, running in a long mode (for example server etc). All I need is to redirect child's stdout and stderr to parent's process and write them to file or print to tty, don't matter now.
This is pretty straightforward task if we talking about simple child, but with long-running child with partial outputting it is a problem.
For example let's look at popular solution with usage of pipe (error checking and other non-important parts omitted):
parent.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int link[2];
pid_t pid;
char str[4096];
pipe(link);
pid = fork();
if (pid == 0) {
dup2(link[1], STDOUT_FILENO);
close(link[0]);
execl("/path_to_bin/fast_child", "fast_child", (char *)0);
}
else
{
close(link[1]);
read(link[0], str, sizeof(str));
printf("Pipe contents: %s\n", str);
wait(NULL);
}
return 0;
}
fast_child.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
printf("I'm fast child\n");
return 0;
}
Using this type of child process in excellent and non-problematic way to get str(out|err) in parent, but using this code as child cause problems of output disappearing in parent:
slow_child.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
for (;;)
{
printf("I'm slow child\n");
sleep(1);
}
return 0;
}
I'm thinking about socket usage as problem solving solution but I'm sure that this is not so efficient way and Unix provides better tools to do this (as usual :)
Thank you.
You need to flush your output from the child every now and then or the parent task won't see anything. use fflush(stdout) at appropriate points. Or you could switch off the buffering on stdout, but that might have a performance impact on your child, as it will do a system call on each character written.

Linux IPC pipe is not working

so I am new to inter-process communications and processes in linux, so I really cannot figure out what the problem is. The following program I wrote is the same problem I am having on a homework assignment consisting of using pipes condensed down. It is basically sending one character from the child to the parent, but it does not print out the character.
it print out:
hello from child
sending a
hello from parent
trying to receive...
received: reaping child
where on the third line it should say
received: a
Any answers are appreciated, and also if you have any helpful criticism of anything else in the program. Thanks everyone
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
pid_t pid = fork();
int comm[2];
pipe(comm);
if (pid == 0)
{
char send = 'a';
int check;
close(comm[0]);
printf("hello from child\n");
printf("sending %c\n", send);
check = write(comm[1], &send, 1);
printf("%d\n", check);
exit(1);
}
else if (pid > 0)
{
char get = ' ';
int check;
close(comm[1]);
printf("hello from parent\n");
printf("trying to receive...\n");
read(comm[0], &get, 2);
printf("received: %c\n", get);
printf("reaping child\n");
wait(NULL);
return 0;
}
return 0;
}
You got the pipe and the fork in the wrong order! Your process forks, then both processes call pipe, so 2 separate pipes are being created. The one you're writing into has nobody reading it, and the one you're reading from had nothing written to it.

Resources