Linux: Using pipe to send the information in between the processes - c

The pipe is created with fd[] and fd1[]. I am writing with the pipes. Taking two pipes gave me error as i was referencing to STDIN_FILENO, so i deleted it. I just used the pipe to read and write. Now it is still not reading from the pipe. Pipe with file descriptor fd[] works. but the fd1[] does not.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv)
{
int fd[2];
pid_t childpid;
pipe(fd);
int fd2[2];
pipe(fd2);
int array[5] = {1,2,3,4,5};
int array2[5] = {11,12,13,14,15};
//fprintf(stderr,"size%ld",sizeof(int));
//char buffer[111];
int *subarr1;
int buffer_num;
int status;
if ((childpid = fork()) == 0) {
fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());
//dup2(fd[1], STDOUT_FILENO);
close(fd[0]); //read end
write(fd[1],array,100);
close(fd[1]);
//execl("/bin/ls", "ls", "-l", NULL);
perror("The exec of ls failed\n");
if ((childpid = fork()) == 0)
{
fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());
//dup2(fd2[1], STDOUT_FILENO);
close(fd2[0]); //read end
write(fd2[1],array2,sizeof(array2));
close(fd2[1]);
//execl("/bin/ls", "ls", "-l", NULL);
perror("The exec of ls failed\n");
}
else
{// parent
wait(NULL);
}
}
else {
wait(&status);
fprintf(stderr,"\nTHis is parent");
subarr1 = (int*)malloc(sizeof(int)*(5));
fprintf(stderr,"\nProcess:%d Parent:%d",getpid(),getppid());
//dup2(fd[0], STDIN_FILENO);
close(fd[1]);
int j;
for(j=0;j<5;j++)
{
read(fd[0],&buffer_num,sizeof(int));
subarr1[j] = buffer_num;
//printf("%s",buffer);
}
for(j=0;j<5;j++)
{
fprintf(stderr,"\n%d\n",subarr1[j]);
}
close(fd[0]);//read end
//dup2(fd2[0], STDIN_FILENO);
buffer_num = 0;
close(fd2[1]);
//read(fd2[0],&buffer_num,100);
for(j=0;j<5;j++)
{
read(fd2[0],&buffer_num,sizeof(int));
subarr1[j] = buffer_num;
//printf("%s",buffer);
}
for(j=0;j<5;j++)
{
fprintf(stderr,"\n%d\n",subarr1[j]);
}
//fprintf(stderr, "\nThis is second pipe%s", buffer);
close(fd2[0]);
/*//printf("\nAAAAAAAA%s\n",buffer);
dup2(fd2[0], STDIN_FILENO);
close(fd2[1]);
printf("\nAAAAAAAA%s\n",buffer);
read(fd2[0],buffer,100);
printf("%s",buffer);
close(fd2[0]);//read end
printf("\nAAAAAAAA%s\n",buffer);
*/
//execl("/usr/bin/sort", "sort", "-n", NULL);
perror("The exec of sort failed\n");
}
return 0;
}

This call:
read(fd2[0],&buffer_num,100);
tries to read 100 bytes into memory starting at the local (int) variable buffer, so overwrites whatever else is on the stack after it, smashing the stack and causing undefined behavior (probably crashing at some point).
You have similar problems with your write calls, writing 100 bytes from objects that are much smaller, but those likely only write garbage you then ignore, rather than causing a crash (though that is likewise undefined behavior).

Related

Trying to replicate basic bash pipe but i get a stdin: Input/output error

I am currently working on a university project to basically built my own simple shell. Everything is working great so far. The only thing giving me trouble is pipes. To make it easier for myself to figure out why they are not working as intended I wrote this little testing program where I try to replicate the bash behaviour of cat | ls. But i now sadly get this error cat: stdin: Input/output error and i really can't figure it out.
Here is my program:
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
extern char **environ;
char *argv1[] = {"cat",NULL};
char *argv2[] = {"ls",NULL};
int fd[2];
pid_t pid;
int ret;
pipe(fd);
pid = fork();
if (pid == 0)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
execve("/bin/cat", argv1, environ);
exit (0);
}
else if (pid > 0)
{
close(fd[1]);
dup2(fd[1], STDIN_FILENO);
execve("/bin/ls", argv2, environ);
waitpid(pid, &ret, 0);
}
return (0);
}
You want a pipe like:
ls | cat
But, you're setting this up like:
cat | ls
And, in your current code, for the ls side, you're doing:
close(fd[1]);
dup2(fd[1], STDIN_FILENO);
This is wrong for two reasons:
You're closing the wrong side of the pipe, so the dup2 gets a closed fd as its first argument
You're attaching the output side of the pipe to the command's input side
So, we need to reverse the pipe order and fix the closing.
Also, after doing dup2(X,...) we want to do close(X).
Also, note that doing waitpid _after execve will have no effect unless the execve fails.
Here is the refactored and working code:
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int
main(void)
{
extern char **environ;
char *argv_cat[] = { "cat", NULL };
char *argv_ls[] = { "ls", NULL };
int fd[2];
pid_t pid;
int ret;
pipe(fd);
pid = fork();
// we want:
// ls | cat
if (pid == 0) {
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
execve("/bin/cat", argv_cat, environ);
exit(0);
}
else if (pid > 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execve("/bin/ls", argv_ls, environ);
waitpid(pid, &ret, 0);
}
return (0);
}
UPDATE:
waitpid after execve is pointless. –
William Pursell
Not quite. It reaps the [stuck] child process so that it doesn't become a child of the init/systemd process.
In the above example, I had forgotten to add a close(STDOUT_FILENO) before the waitpid to "release" the cat process.
Here is the adjusted code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
int opt_f;
int
main(int argc,char **argv)
{
extern char **environ;
char *argv_cat[] = { "cat", NULL };
char *argv_ls[] = { "ls", NULL };
int fd[2];
pid_t pid;
int status;
--argc;
++argv;
for (; argc > 0; --argc, ++argv) {
char *cp = *argv;
if (*cp != '-')
break;
cp += 2;
switch (cp[-1]) {
case 'f':
opt_f = ! opt_f;
break;
}
}
pipe(fd);
pid = fork();
// we want:
// ls | cat
if (pid == 0) {
if (opt_f)
fprintf(stderr,"cld: getpid=%d\n",getpid());
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
execve("/bin/cat", argv_cat, environ);
exit(0);
}
else if (pid > 0) {
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
const char *ls = opt_f ? "/bin/gooch" : "/bin/ls";
execve(ls, argv_ls, environ);
fprintf(stderr,"execve failure of '%s' -- %s\n",ls,strerror(errno));
// release other process (cat)
close(STDOUT_FILENO);
// reap the child
pid_t ret = waitpid(pid, &status, 0);
fprintf(stderr,"ret=%d pid=%d status=%8.8X\n",ret,pid,status);
}
return (0);
}

How to pass STDIN to a program and store its output to a variable? C

I need to execute a file from the bash using and store its output to a variable, there's also the needs to pass to its stdin a string s. Something like this in bash:
usr:~$ s | program args
I know how to call the program and give him args:
execvp(program,args);
So my problem is giving to that his stdin and store output to a variable(string)!
P.S.:can't use system and popen.
Some example code for you to experiement. This excute ls | cat.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv) {
int fd[2];
int pid;
char* cmd1[2] = {"ls", NULL};
char* cmd2[2] = {"cat", NULL};
int status;
pid = fork();
if (pid == 0) {
pipe(fd);
pid = fork();
if (pid == 0) {
printf("cmd1\n");
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
execvp(cmd1[0], cmd1);
printf("Error in execvp\n");
}
else {
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
printf("cmd2\n");
execvp(cmd2[0], cmd2);
printf("Error in execvp\n");
}
}
else {
close(fd[0]);
close(fd[1]);
wait(&status);
printf("%d\n", status);
wait(&status);
printf("%d\n", status);
}
}

Pipe commands with fork and dup2

I wrote the following code in order to pipe two commands:
#include <stdlib.h>
#include <unistd.h>
char *program_1[3] = {"/bin/cat", "/dev/random", NULL};
char *program_2[2] = {"/bin/ls", NULL};
char *program_3[2] = {"/usr/bin/sort", NULL};
int main(void)
{
int fd[2];
int pid;
pipe(fd);
if ((pid = fork()) == 0) //Child process
{
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
execve(program_3[0], program_3, NULL);
}
else if (pid > 0) //Parent process
{
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
execve(program_2[0], program_2, NULL);
}
return (EXIT_SUCCESS);
}
Each pair of program_x / program_y where x != y works fine, except this one.
When i pipe sort into ls, ls well prints its output on stdout, but then, sort throw this error: sort: Input/output error.
When I type sort | ls into bash, it prints ls result as my program, but then waits for input.
Am I doing someting wrong ?
edit: I'm trying to reimplement the shell's behaviour
The problem is that when ls finishes, the parent process will exit which will close the read-end of the pipe, which will lead to an error being propagated to the write-end of the pipe which is detected by sort and it write the error message.
That it doesn't happen in the shell is because shells handle pipes differently than your simple example program, and it keeps the right-hand side of the pipe open and running (possibly in the background) until you pass EOF (Ctrl-D) to the sort program.
Your program isn't quite equivalent to what a shell typically does.
You're replacing the parent with ls; whereas shell would create who child processes and connect them and wait for them to finish.
It's more like:
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
char *program_2[2] = {"/bin/ls", NULL};
char *program_3[2] = {"/usr/bin/sort", NULL};
int main(void)
{
int fd[2];
pid_t pid;
pid_t pid2;
pipe(fd);
if ((pid = fork()) == 0) //Child process
{
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
execve(program_3[0], program_3, NULL);
}
else if (pid > 0) //Parent process
{
if ( (pid2 = fork()) == 0) {
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
execve(program_2[0], program_2, NULL);
}
}
waitpid(pid, 0, 0);
waitpid(pid2, 0, 0);
return (EXIT_SUCCESS);
}
I finally found the solution, we were close to:
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
char *cat[3] = {"/bin/cat", "/dev/random", NULL};
char *ls[2] = {"/bin/ls", NULL};
char *sort[2] = {"/usr/bin/sort", NULL};
int main(void)
{
int fd[2];
pid_t pid;
pid_t pid2;
pipe(fd);
if ((pid = fork()) == 0)
{
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
execve(cat[0], cat, NULL);
}
else if (pid > 0)
{
if ( (pid2 = fork()) == 0)
{
dup2(fd[0], STDIN_FILENO);
close(fd[1]);
execve(ls[0], ls, NULL);
}
waitpid(pid2, 0, 0);
close(fd[0]);
}
waitpid(pid, 0, 0);
return (EXIT_SUCCESS);
}
We need to close the read end of the pipe once the last process ends, this way, if the first process tries to write on the pipe, an error will be throwed and the process will exit, else if it only reads from stdin as sort, it will keep reading as stdin is still open

strange behavior of stdout redirected to a pipe

Here is a minimal example demonstrating my problem. I have a program forking a new subprocess and redirecting stdout to it. It works fine. Then I fork a second subprocess and redirect stdout to it and I close the first pipe. I would expect that the first subprocess receives EOF in its input pipe and terminates. Instead it remains in reading state until the main task exits. I do not understand why. I would expect the first pipe to be closed and the first child process to become a zombie.
Here is the code demonstrating the issue:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int popenin(char *command) {
int pin[2];
pid_t pid;
if (pipe(pin) != 0) exit(1);
pid = fork();
if (pid < 0) exit(1);
if (pid == 0) {
close(pin[1]);
dup2(pin[0], 0);
close(pin[0]);
execlp("bash", "bash", "-c", command, NULL);
perror("Error:");
exit(1);
} else {
close(pin[0]);
return(pin[1]);
}
}
int main() {
int fd;
fd = popenin("gzip > foo1.gz");
dup2(fd, 1);
close(fd);
printf("foo 1 content\n");fflush(stdout);
fd = popenin("gzip > foo2.gz");
close(1);
dup(fd);
close(fd);
printf("foo 2 content\n");fflush(stdout);
sleep(10000);
}
This program creates two files foo1.gz and foo2.gz, both empty and there are two gzip processes running in the system. I'd expect to see the first file completed, closed and the first gzip process to exit.
If I modify the minimal example in the following way, it works as expected.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int popenin(char *command) {
int pin[2];
pid_t pid;
if (pipe(pin) != 0) exit(1);
pid = fork();
if (pid < 0) exit(1);
if (pid == 0) {
close(pin[1]);
dup2(pin[0], 0);
close(pin[0]);
execlp("bash", "bash", "-c", command, NULL);
perror("Error:");
exit(1);
} else {
close(pin[0]);
return(pin[1]);
}
}
int main() {
int fd;
fd = popenin("gzip > foo1.gz");
dup2(fd, 1);
close(fd);
printf("foo 1 content\n");fflush(stdout);
close(1); // close(1) is moved before popenin
fd = popenin("gzip > foo2.gz");
dup(fd);
close(fd);
printf("foo 2 content\n");fflush(stdout);
sleep(10000);
}
Can somebody explain why the first version does not work?

Writing to a pipe in a child process called by execl

I don't understand how to use the created pipe from mp.c in the child process sp.c. I (think I) can't seem to access proper file descriptor when using execl for outside process.
/***************mp.c*****************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *procpath = "/mypath/sp";
char *procname = "sp";
pid_t pid;
int fd[2];
int ret;
char buf[20];
memset(&buf[0], 0, sizeof(buf));
ret = pipe(fd);
if(ret == -1){
perror("pipe");
exit(1);
}
pid = fork();
printf("%d\n",pid);
if (pid == 0){
//dup2(mypipefd[1],STDOUT_FILENO);
ret = execl(procpath, procpath, "1","2",NULL);
perror("execl failed to run slave program");
exit(1);
}
else if (pid > 0){
/* Parent process*/
printf("execl ret val = %d",ret);
printf("Parent process \n");
close(fd[1]);
read(fd[0],buf,15);
// close(fd[1]);
close(fd[0]);
printf("buf: %s TEST\n", buf);
printf("buf: %s TEST\n", buf);
}
else{
printf("call to fork failed, no child\n");
exit(-1);
}
exit(0);
}
and the created process...
/***************sp.c*****************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[]){
int ret;
//printf("Child process \n");
int fd[2];
pipe(fd);
//dup2(fd[1],1);
//int out;
/*ret = dup2(fd[1],1);
if (ret = -1){
printf("%s\n", strerror(errno));
};*/
//sprintf()
//printf("%d\n", ret);
//mypipefd = argv[1];
printf("Child process \n");
//close(fd[0]);
write(fd[1], "Hello there!",12);
close(fd[1]);
exit(0);
}
The problem is that you are creating a different pipe in each application. In order to correctly communicate over a pipe, two program should share the same pipe ( one of the file descriptor create by the pipe function).
Basically to solve this problem, you must create the pipe in one application and send the file descriptor to the other program without calling again the system call pipe. A file descriptor can be sent to another process by using a socket unix domain. Look a this post Can I share a file descriptor to another process on linux or are they local to the process?.

Resources