process in c is not exiting even after exit() function is called - c

I was practicing pipes in system programming when i realized that my program isn't exiting. I added exit() in both child and parent, but the child still isn't exiting. Please help...
Here is the code:
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
//#include "apue.h"
main() {
int n,max=20;
pid_t pid;
int fd[2];
char line[max];
int i;
for(i=0;i<20;i++) {
line[i]='\0';
}
if(pipe(fd)<0) {
perror("pipe error");
}
if((pid=fork())<0) {
perror("fork error");
}
else if(pid > 0) {
close(fd[0]);
write(fd[1], "hello world\n", 12);
exit(1);
} else {
close(fd[1]);
read(fd[0], line, max);
}
puts(line);
exit(1);
}

First of all, fork returns 0 in the child not in the parrent. So, when you write
else if(pid > 0) {
close(fd[0]);
write(fd[1], "hello world\n", 12);
exit(1); }
You are in the parrent process. To be in the child process space, you shoud use else if(pid **==** 0)
The seccond thing you should do to make sure everything works fine, you should not call in the child process code space the function exit(). You would better wait your child process in the parrent process. For this you should use the wait() function in the parrent process.
The good code would be:
main() {
int n,max=20;
pid_t pid;
int fd[2];
char line[max];
int i;
int status;
for(i=0;i<20;i++) {
line[i]='\0';
}
if(pipe(fd)<0) {
perror("pipe error");
}
pid=fork();
if(pid <0) {
perror("fork error");
}
else if(pid == 0) { // Here is the child process
close(fd[0]);
write(fd[1], "hello world\n", 12);
**// Do not kill child process because is dangeorus to do this when you use pipes**
} else { // Parrent process
close(fd[1]);
read(fd[0], line, max);
puts(line);
wait(&status); // Wait the child process to end its job
}
return 0;
}

Related

How to fix data write or read using pipe in c program is giving wrong output?

I am trying to get an integer input in the child process and send it to the parent process using pipe()
but I receive garbage values every time in the parent process.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
pid_t pid;
int fd[2];
char *args[] = {"", NULL};
int cnum,pnum;
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if(pipe(fd) == -1)//fd[0] for read fd[1] for write
{
perror("pipe");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
printf("\n**In the child process**\n");
printf("Enter Number : ");
scanf("%d",&cnum);
write(fd[1],&cnum,sizeof(int));
close(fd[1]);
}
else
{
wait(NULL);
close(fd[1]);
printf("\n**In the parent precess**\n");
read(fd[0],&pnum,sizeof(int));
close(fd[0]);
printf("Number recieved = %d\n",pnum);
printf("PID = %d\n", getpid());
execv("./sayHello", args);
printf("Error");
}
}
Output of the above code
**In the child process**
Enter Number : 212
**In the parent precess**
Number recieved = 1036468968
PID = 22528
Hillo Amol
PID = 22528
I give input of 212 but in parent 1036468968 received.
You call fork before you create the pipe FDs. After you call fork, the parent and the child both create their own pair of pipe FDs, and there's no shared pipe between them.
Create the pipe before you fork and it could work.
As drorfromthenegev suggest problem is arising due to I am calling pipe() after fork().
So I call pipe() first and the i call fork() and it works..
Workable solution
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
pid_t pid;
int fd[2];
char *args[] = {"", NULL};
int cnum,pnum;
if(pipe(fd) == -1)//fd[0] for read fd[1] for write
{
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
printf("\n**In the child process**\n");
printf("Enter Number : ");
scanf("%d",&cnum);
write(fd[1],&cnum,sizeof(int));
close(fd[1]);
}
else
{
wait(NULL);
close(fd[1]);
printf("\n**In the parent precess**\n");
read(fd[0],&pnum,sizeof(int));
close(fd[0]);
printf("Number recieved = %d\n",pnum);
printf("PID = %d\n", getpid());
execv("./sayHello", args);
printf("Error");
}
}

How could I know if all processes have been already created before sending signals?

I'm writing a Unix program where the parent process has to send signals to children and a grandson. How could I know if all processes have been already created before sending signals? Because sometimes they don't exist yet. Thanks a lot!
void t(int sig)
{
kill(SIGKILL, pidc1);
kill(SIGKILL, pidc2);
kill(SIGKILL, pidg2);
kill(SIGKILL, pidc3);
}
void handler()
{
write(1, "Signal SIGUSR1\n", 15);
}
pid_t pidc1, pidc2, pidc3, pidg2;
int main(int argc, char **argv)
{
struct sigaction action;
int status;
action.sa_flags = 0;
action.sa_handler = handler;
sigaction(SIGUSR1, &action, NULL);
pidc1 = fork();
if(pidc1 == 0)
{
printf("Child 1\n");
}
pidc2 = fork();
if(pidc2 == 0)
{
printf("Child 2\n");
pidg2 = fork();
if(pidg2 == 0)
{
printf("Grandson 2\n");
}
wait(&status);
}
pidc3 = fork();
if(pidc3 == 0)
{
printf("Child 3\n");
}
kill(pidg2, SIGUSR1);
kill(pidc3, SIGUSR1);
signal(SIGALRM, t);
alarm(10);
wait(&status);
}
Preliminary note: The child code parts in your example program fall through to their parent's code, which is certainly not intended; I'll assume something like return sleep(5); at the end of each block. Also note that the printf()s may malfunction with fork()s and buffered output.
Barmar wrote:
If you need to wait for the grandchild processes to be created, you need some kind of communication from the child to the parent, so it can send the grandchild's PID. Shared memory and a mutex would be a way to do this.
That's absolutely correct. (The direct children are no problem, since the parent knows their PIDs.) Another way to communicate the grandchild's PID is a pipe; your example main() could become:
int main(int argc, char **argv)
{
int status;
sigaction(SIGUSR1, &(struct sigaction){.sa_handler = handler}, NULL);
setbuf(stdout, NULL); // printf() may malfunction without this
pidc1 = fork();
if (pidc1 == 0)
{
printf("Child 1\n"); return sleep(5);
}
int pipefd[2];
pipe(pipefd); // for communicating the grandson's PID
pidc2 = fork();
if (pidc2 == 0)
{
printf("Child 2\n");
pidg2 = fork();
if (pidg2 == 0)
{
printf("Grandson 2\n"); return sleep(5);
}
write(pipefd[1], &pidg2, sizeof pidg2); // write pidg2 to write end
wait(&status); return sleep(5);
}
pidc3 = fork();
if(pidc3 == 0)
{
printf("Child 3\n"); return sleep(5);
}
read(pipefd[0], &pidg2, sizeof pidg2); // read pidg2 from pipe's read end
kill(pidg2, SIGUSR1);
kill(pidc3, SIGUSR1);
}

Executing wc command on child process using pipeline

I'm writing a program that executes the word count command on the child process. The father process should send a sequence of lines entered by the user trough a pipeline to the child process.
I tried to do this but I ended up with an error.
This is my code:
int main ()
{
int fd[2];
char buff;
int pid;
int pip;
pid = fork();
pip = pipe(fd);
if (pid != 0)
{
pip = pipe(fd);
if (pipe == 0)
{
while (read(fd[0], &buff,1) > 0 )
{
write (fd[1],&buff,1);
}
close(fd[0]);
_exit(0);
}
}
else
{
dup2(fd[1],1);
close(fd[1]);
execlp ("wc","wc",NULL);
_exit(-1);
}
return 0;
}
I've also tried to use dup2 to associate the standard input from the child to the read descriptor of the pipe created by the father process.
But I get this error : wc: standard input: Input/output error.
How can I solve this?
UPDATED (the error is solved but I get an infinite loop)
int main ()
{
int fd[2];
char buff;
int pid;
int pip;
pip = pipe(fd);
if (pip == 0)
{
pid = fork();
if (pid != 0)
{
while (read(fd[0], &buff,1) > 0 )
{
write (fd[1],&buff,1);
}
close(fd[0]);
}
else {
dup2(fd[1],1);
close(fd[1]);
execlp ("wc","wc",NULL);
_exit(-1);
}
}
return 0;
}
#include <unistd.h>
int main ()
{
int fd[2];
char buff;
int pid;
int pip;
int status;
pip = pipe(fd);
if (pip == 0)
{
pid = fork();
if (pid != 0)
{
close(fd[0]);
while (read(0, &buff,1) > 0 )
{
write (fd[1],&buff,1); /* your old loop forwarded internally in the pipe only*/
}
close(fd[1]);
} else {
dup2(fd[0],0); /* you had dup2(fd[1], 1), replacing stdout of wc with the write end from wc */
close(fd[0]);
close(fd[1]);
execlp ("wc","wc",NULL);
_exit(-1);
}
}
wait(&status); /* reap the child process */
return 0;
}

Why my fork() does not output anything?

As code , why I don't get any outputs , can anybody tell me the issue? like that I have two fork() and each will run in a child process and my parent process will not be exit, seems right, but still don't get anything output.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
/***********************************/
printf("--beginning of program\n");
int counter = 0;
pid_t pid1 = 0;
pid_t pid2 = 0;
while(1){
if(pid1 == 0)
pid1 = fork1();
if(pid2 == 0)
pid2 = fork2();
}
printf("--end of program--\n");
return 0;
}
/* Two fork() */
pid_t fork1(){
pid_t pid = fork();
if(pid ==0 )
{
while(1){
sleep(1);
fprintf(stdout," fork1 ");
}
}
return pid;
}
pid_t fork2(){
pid_t pid = fork();
if(pid ==0 )
{
while(1){
sleep(1);
fprintf(stdout," fork1 ");
}
}
return pid;
}
stdout is buffered, it will normally only be flushed on a newline or if you explicitly flush it.
You can get your code to output the lines from the children processes by adding a newline in your statements:
fprintf(stdout, "fork1\n");
Or by explicitly flushing the buffer after the fprintf:
fflush(stdout);

execlp redirect stdin in c

Question is simple
I've searched around but I couldn't find the solution
char *data1;
char *data2;
pid_t pid = fork();
int stat;
if (pid == 0){
execlp("Program B");
} else {
wait(&stat);
if (WIFEXITED(stat))
printf("%d\n", WEXITSTATUS(stat));
}
the thing is I need to send data1 and data2 to Program B as stdin
but I couldn't find the solution
how can I deal with this?
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
{
char *data1 = "First message.\n";
char *data2 = "What the rest of the world has been waiting for.\n";
pid_t pid;
int p[2];
if (pipe(p) < 0)
perror("pipe() failed");
else if ((pid = fork()) < 0)
perror("fork() failed");
else if (pid == 0)
{
dup2(p[0], STDIN_FILENO);
close(p[0]);
close(p[1]);
execlp("cat", "cat", (char *)0);
perror("execlp() failed");
}
else
{
close(p[0]);
write(p[1], data1, strlen(data1));
write(p[1], data2, strlen(data2));
close(p[1]);
int status;
int corpse = wait(&status);
if (WIFEXITED(status))
printf("%d exited with status %d\n", corpse, WEXITSTATUS(status));
}
return 0;
}
Note how many closes are necessary.
Building a pipe to stdin is the way to go,
Like
char *data1;
char *data2;
int stat;
pid_t pid;
if( pipe(pfd) < 0 ) {
perror("pipe");
return 1;
}
pid = fork();
if (pid == 0)
{
// Close the writing end of the pipe
close(pfd[1]);
execlp("Program B");
}
else if(pid==-1)
{
perror("fork");
}
else
{
// Write to the pipe.
if (write(pfd[1], "This is my data \n", 16) != 16)
perror("write");
close(pfd[1]);
wait(&stat);
if (WIFEXITED(stat))
printf("%d\n", WEXITSTATUS(stat));
}
You can provide data as argument list to the new process.
Syntax:- int execlp(const char *path, const char *arg0, ..., NULL);
So your call can look something like this
// convert the input data into string format i.e data1 and data2 should be strings
execlp("Program B","Program B",data1,data2,NULL);
In program B use appropriately to convert it into whatever type you want to.

Resources