What's wrong with my piping code? - c

I'm trying to create a pipe for a custom C shell that can run something like cat example.txt | -wc.
But right now when I run it, I get the shell prompt looping infinitely on the command line.
Can someone tell me what's wrong with how I'm implementing piping, please?
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **args){
pid_t pid,pid2, wpid;
int pipefd[2];
int status;
pipe(pipefd);
pid=fork();
if(pid==0)
{
pid2=fork();
if(pid2==0)
{
dup2(pipefd[0],0);
close(pipefd[1]);
if(execvp(args[1],args)==-1)
{
perror("Shell Error");
}
}
else
{
dup2(pipefd[1],1);
close(pipefd[0]);
if(execvp(args[4],args)==-1)
{
perror("Shell Error");
}
}
}
else
{
//Return to Shell
}
close(pipefd[0]);
close(pipefd[1]);
}

Related

grep exec on a File Content in C

I am trying to apply the grep on file content using excel. But, it is not working. I am stuck on it. I don't know how to get the content of the file in the exec call.
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main() {
int link[2];
pid_t pid;
char foo[4096];
if (pipe(link)==-1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if(pid == 0) {
const char *input = "output1.txt";
int fd = open(input, O_RDONLY);
dup2(fd, 0);
execl("grep", "grep" ,"com", NULL);
close(fd);
exit(1);
} else {
}
return 0;
}
The output1.txt is shown below:
c1.txt
c1.txt~
commands1
commands1~
commandSample1.txt
commandSample1.txt~
commandSample2.txt
foo.txt
output1.txt
output2.txt
text.txt
text.txt~
Suggestions would be great.
The first argument for execl must be a full path. Or you can use execlp instead.

C exec() use a pipe as default input stream

I want to do the following things:
Child process run a program by exec()
Parent process read from STDIN and do something to the input
Pass the input to child process's default input stream.
I know that the child and the parent share the same STDIO, and I'm not familiar with pipe or how to make child read from other pipe.
Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd[2];
pid_t pid = fork();
pipe(fd);
if (!pid) {
dup2(fd[0], STDIN_FILENO);
char arg_pipe_id[10];
sprintf(arg_pipe_id, "<&%d", fd[0]);
close(fd[1]);
// Error here, cannot use <&id or <id in this execl
execl("/bin/bash", "/bin/bash", "-i", arg_pipe_id, NULL);
} else {
char input[2048];
close(fd[0]);
while (fgets(input, 2048, stdin)) {
...
process the input...
...
if (condition) {
write(fd[1], input, strlen(input));
}
}
close(fd[1]);
kill(pid, SIGKILL);
wait(NULL);
}
}
I'd appreciate it if you could help me!

How to communicate from children process(exec) with parent through pipe()?

I have these two files and i call exec.c from main.c using exec(). As far as I understand exec.c should inherit the pipe but it says there is no link pipe in exec.c. What is the problem here?
main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define die(e) \
do \
{ \
fprintf(stderr, "%s\n", e); \
exit(EXIT_FAILURE); \
} while (0);
int main(int argc, char *argv[])
{
int link[2];
pid_t pid;
char foo[4096];
if (pipe(link) == -1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if (pid == 0)
{
dup2(link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
execvp("./exec", argv);
die("execl");
}
else
{
close(link[1]);
int nbytes = read(link[0], foo, sizeof(foo));
printf("Output: (%.*s)\n", nbytes, foo);
wait(NULL);
}
return 0;
}
exec.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
int main(int argc, char *argv[])
{
char a;
a='A';
write(link[1],&a,sizeof(a));
return 0;
}
I am just practicing and want to output the data that is save from pipe()
What I am doing wrong, can you help me to debug?
TIA!
In the main.c program you connect the pipe through standard output of the child process.
That means the child process passes information to the parent process through its normal standard output.
From this follows that the exec.c program could be as simple as this:
#include <stdio.h>
int main(void)
{
printf("A");
}
More specifically, your exec.c Source file doesn't have any idea of the pipe, and definitely not about the variable link, and will simply fail to build.

C named pipe does not work with multiprocess

I would like to create a named pipe in the parent process and after write a string to it in the child process and finally read this string in the parent process. When run the program I dont get back the prompt like still waiting for end of child process. Why the child process not finished?
Current output:
Expected output:
(picture created without multiprocesses)
My source code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main() {
int pipefd[2];
pid_t cpid;
char szoveg[32];
int fd, ret;
char buf[32];
buf[0]=0;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(-1);
}
if (cpid == 0) {
printf("%d: Child process\n",getpid());
strcpy(buf,"Some text \0");
printf("%d:write to fifo: %s:%ld\n",getpid(),buf,strlen(buf));
write(fd,buf,strlen(buf));
exit(0);
} else {
printf("%d: Parent process\n",getpid());
ret=mkfifo("FifoName",00666);
if (ret == -1) {
perror("mkfifo()");
exit(-1);
}
fd=open("FifoName",O_RDWR);
if (fd == -1) {
perror("open() error!");
exit(-1);
}
wait(NULL);
ret=read(fd,buf,32);
printf("%d:read() Read %d bytes: %s\n",getpid(),ret,buf);
close(fd);
unlink("FifoName");
exit(0);
}
}
William Pursell right. The problem was the missing fd=open("FifoName",O_RDWR); line from child process.

I'm getting a final link error when I compile a c program

I'm writing this program:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
int main(){
int fd[2];
pid_t pid;
pipe(fd);
pid = fork();
if(pid>0)
{
while(1){
char * command = (char*)malloc(55);
printf("Command: ");
fgets(command,55,stdin);
write(fd[1],&command,55);
wait(NULL);
}
}
else if(pid)
{
char * command = (char*)malloc(55);
read(fd[0],&command,55);
printf("Child command: %s",command);
}
return 0;
}
and when I compile I'll get this error:
I can't figure out what could be. I know that I'm not using all libraries and checking for errors but i think that's not the issue here. Thanks! Have a great day!

Resources