Ok so short story is:
I have a program that need to do X and it does 0.25X. I use 2 forks and 4 pipes, and I dont know how to debug this. (using eclipse c/c++ in linux env).
Long story:
I have a program that need to calculate gcd (Greatest common divisor) from a text file containing pairs of ints. This program has a father (main) and 2 childerns (forks) that need to talk to the father through pipes. (2 pipes for each childern.)
When I complie and run the program in ubuntu, I get no error but the proram doesnt complete its tasks. I have no idea where/why it breaks. How can I debug this? Im coding in eclipse c/c++ and debugger can't handle the forks.. When I debug it is reading all numbers from file (doesnt calculate gcd) but when I run in ubuntu terminal it only reads first line and breaks. here is the full code:
int main(int argc, char **argv) {
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "usage: %s <FILE NAME>\n", argv[0]);
return EXIT_FAILURE;
}
int pfd_child1_r[2], pfd_child1_w[2], pfd_child2_r[2], pfd_child2_w[2];
if (pipe(pfd_child1_r) == -1 || pipe(pfd_child1_w) == -1
|| pipe(pfd_child2_r) == -1 || pipe(pfd_child2_w) == -1) {
perror("cannot pipe()");
return EXIT_FAILURE;
}
createChilds(pfd_child1_r, pfd_child1_w, pfd_child2_r, pfd_child2_w);
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
perror("fopen(): ");
return EXIT_FAILURE;
}
char line[100];
char *token;
int numbers[2], num, line_count = 1, counter = 0, result = 0;
while (fgets(line, sizeof(line), fp) != NULL) {
token = strtok(line, " ");
while (token != NULL) {
num = atoi(token);
if (num < 1 || counter == 2) {
fprintf(stderr, "illegal input at line %d\n",
line_count);
return EXIT_FAILURE;
}
numbers[counter] = num;
counter++;
token = strtok(NULL, " ");
}
counter = 0;
if (line_count % 2 == 0) { // use first child
write(pfd_child1_w[1], &numbers[0], sizeof(int));
write(pfd_child1_w[1], &numbers[1], sizeof(int));
} else { // use second child
write(pfd_child2_w[1], &numbers[0], sizeof(int));
write(pfd_child2_w[1], &numbers[1], sizeof(int));
}
if (line_count > 1) { // after first run alternate to get result
if (line_count % 2 == 0) { // read from second child
read(pfd_child2_r[0], &result, sizeof(int));
printf("%d %d\t\tgcd: %d\n", numbers[0], numbers[1], result);
} else { // read from first child
read(pfd_child1_r[0], &result, sizeof(int));
printf("%d %d\t\tgcd: %d\n", numbers[0], numbers[1], result);
}
}
line_count++;
}
fclose(fp);
return EXIT_SUCCESS;
}
void createChilds(int pfd_child1_r[2], int pfd_child1_w[2], int pfd_child2_r[2],
int pfd_child2_w[2]) {
switch (fork()) {
case -1:
perror("cannot fork()");
exit(EXIT_FAILURE);
case 0: /* First child: */
if (close(pfd_child1_r[0]) == -1) { /* Read end is unused */
perror("cannot close()");
exit(EXIT_FAILURE);
}
if (close(pfd_child1_w[1]) == -1) { /* Write end is unused */
perror("cannot close()");
exit(EXIT_FAILURE);
}
/* Duplicate stdout on write end of pipe; close duplicated descriptor */
if (pfd_child1_w[1] != STDOUT_FILENO) { /* Defensive check */
if (dup2(pfd_child1_r[1], STDOUT_FILENO) == -1) {
perror("cannot dup2()");
exit(EXIT_FAILURE);
}
if (close(pfd_child1_r[1]) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
}
/* Duplicate stdin on read end of pipe; close duplicated descriptor */
if (pfd_child1_w[1] != STDIN_FILENO) { /* Defensive check */
if (dup2(pfd_child1_w[0], STDIN_FILENO) == -1) {
perror("cannot dup2()");
exit(EXIT_FAILURE);
}
if (close(pfd_child1_w[0]) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
}
execlp("./v1_child", "./v1_child", NULL); /* Writes to pipe */
exit(EXIT_SUCCESS);
default: /* Parent go to next child */
break;
}
switch (fork()) {
case -1:
perror("cannot fork()");
exit(EXIT_FAILURE);
case 0: /* Second child: exec 'wc' to read from pipe */
if (close(pfd_child2_r[0]) == -1) { /* Read end is unused */
perror("cannot close()");
exit(EXIT_FAILURE);
}
if (close(pfd_child2_w[1]) == -1) { /* Write end is unused */
perror("cannot close()");
exit(EXIT_FAILURE);
}
/* Duplicate stdout on write end of pipe; close duplicated descriptor */
if (pfd_child2_w[1] != STDOUT_FILENO) { /* Defensive check */
if (dup2(pfd_child2_r[1], STDOUT_FILENO) == -1) {
perror("cannot dup2()");
exit(EXIT_FAILURE);
}
if (close(pfd_child2_r[1]) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
}
/* Duplicate stdin on read end of pipe; close duplicated descriptor */
if (pfd_child2_w[1] != STDIN_FILENO) { /* Defensive check */
if (dup2(pfd_child2_w[0], STDIN_FILENO) == -1) {
perror("cannot dup2()");
exit(EXIT_FAILURE);
}
if (close(pfd_child2_w[0]) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
}
execlp("./v1_child", "./v1_child", NULL); /* Writes to pipe */
exit(EXIT_SUCCESS);
default: /* Parent falls through */
break;
}
/* Parent closes unused file descriptors for pipe */
if (close(pfd_child1_r[1]) == -1 || close(pfd_child1_w[0]) == -1
|| close(pfd_child2_r[1]) == -1 || close(pfd_child2_w[0]) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
the second file is the gcd file, i still have not finish it and the loop the should keep geting the numbers is not present. but i just want to get the first line working properly then i will fix the rest.
int gcd(int n1, int n2) {
if (n2 == 0)
return n1;
return gcd(n2, n1 % n2);
}
int main(int argc, char **argv) {
int n1, n2, result;
if (scanf("%d %d", &n1,&n2) != 2) {
fprintf(stderr, "error reading numbers in child\n");
return -1;
}
if (n1 > n2)
result = gcd(n1, n2);
else
result = gcd(n2,n1);
printf("%d", result);
}
How to Debug
An easy way to debug is always to add fprintf(stderr, "...") statements to the child program. Then you can run the program and also see what the child processes are doing.
Transfer Values
Since you redirect stdin and stdout and use sscanf/printf in the v1_child program which calculates gcd, I assume you want to transfer the values as strings.
An easy way could be to use fprintf to write ints as formatted strings. You could use fdopen to associate a stream to an existing pipe file descriptor.
Accordingly, you must convert numbers from and to strings.
Variable Length Data and Buffered I/O
If you use strings to transfer values, each pair of values has a variable length. Typically, a newline character is used in a C program to signal a complete input record.
Another reason for reading/writing a whole line is that read/write calls can also transfer only a partial number of bytes. You must therefore know when an input record is completed. An alternative would be to have a binary format, which would automatically represent a format with fixed lengths.
By using streams you work with buffered I/O, with fflush you can ensure that all buffered data is written through the underlying write function of the stream.
Functions
One could divide the features into several functions to make the flow a little easier to understand.
Possible Improvements
This should perhaps already be a start.
Another possible improvement could be the use of strtol instead of atoi, since atoi does not perform error checking. Similar sscanf does not report conversion errors (e.g. non-numeric characters at the end of the line), at least we look at the number of assigned input items.
There are presumably still possibilities to improve the readability of the code.
With waitpid the exit status code of the child could be checked in the parent.
Program
Your code, slightly modified in the points mentioned above, could then look like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void create_pipe(int *);
void close_fd(int);
int child(const int *, const int *);
int read_input_line(FILE *fp, char *line, int max, int *numbers, int line_count);
void write_to_child(FILE *fp, const int *numbers);
int read_from_child(FILE *fp);
int main(int argc, char *argv[]) {
if (argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "usage: %s <FILE NAME>\n", argv[0]);
return EXIT_FAILURE;
}
int pfd_child1_r[2];
int pfd_child1_w[2];
int pfd_child2_r[2];
int pfd_child2_w[2];
create_pipe(pfd_child1_r);
create_pipe(pfd_child1_w);
create_pipe(pfd_child2_r);
create_pipe(pfd_child2_w);
pid_t pid1 = fork();
if (pid1 == 0) { //child 1
close_fd(pfd_child2_r[0]);
close_fd(pfd_child2_r[1]);
close_fd(pfd_child2_w[0]);
close_fd(pfd_child2_w[1]);
return child(pfd_child1_r, pfd_child1_w);
} else if (pid1 > 0) {
close_fd(pfd_child1_r[1]);
close_fd(pfd_child1_w[0]);
pid_t pid2 = fork();
if (pid2 == 0) { //child 2
close_fd(pfd_child1_r[0]);
close_fd(pfd_child1_w[1]);
return child(pfd_child2_r, pfd_child2_w);
} else if (pid2 > 0) {
close_fd(pfd_child2_r[1]);
close_fd(pfd_child2_w[0]);
FILE *fp_child1_w = fdopen(pfd_child1_w[1], "w");
FILE *fp_child2_w = fdopen(pfd_child2_w[1], "w");
FILE *fp_child1_r = fdopen(pfd_child1_r[0], "r");
FILE *fp_child2_r = fdopen(pfd_child2_r[0], "r");
if (!fp_child1_w || !fp_child2_w || !fp_child1_r || !fp_child2_r) {
perror("fdopen() failed");
return EXIT_FAILURE;
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
perror("fopen(): ");
return EXIT_FAILURE;
}
char line[100];
int numbers[2], line_count = 0;
while (read_input_line(fp, line, sizeof(line), numbers, line_count) == 2) {
if (line_count % 2 == 0) {
write_to_child(fp_child1_w, numbers);
} else {
write_to_child(fp_child2_w, numbers);
}
if (line_count % 2 == 0) {
int result = read_from_child(fp_child1_r);
printf("%d %d\t\tgcd: %d\n", numbers[0], numbers[1], result);
} else {
int result = read_from_child(fp_child2_r);
printf("%d %d\t\tgcd: %d\n", numbers[0], numbers[1], result);
}
line_count++;
}
//fclose closes also associated file descriptor
fclose(fp_child1_w);
fclose(fp_child2_w);
fclose(fp_child1_r);
fclose(fp_child2_r);
fclose(fp);
return EXIT_SUCCESS;
} else {
perror("second fork failed");
return EXIT_FAILURE;
}
} else {
perror("first fork failed");
return EXIT_FAILURE;
}
}
int read_input_line(FILE *fp, char *line, int max, int *numbers, int line_count) {
char *token;
int num, counter = 0;
line[0] = '\0';
if (fgets(line, max, fp) != NULL) {
token = strtok(line, " ");
while (token != NULL) {
num = atoi(token);
if (num < 1 || counter == 2) {
fprintf(stderr, "illegal input at line %d\n", line_count + 1);
exit(EXIT_FAILURE);
}
numbers[counter] = num;
counter++;
token = strtok(NULL, " ");
}
}
return counter;
}
int read_from_child(FILE *fp) {
char buf[128];
int result = -1;
if (fgets(buf, sizeof(buf), fp)) {
if (sscanf(buf, "%d", &result) == 1)
return result;
}
return -1;
}
void write_to_child(FILE *fp, const int *numbers) {
fprintf(fp, "%d %d\n", numbers[0], numbers[1]);
fflush(fp);
}
int child(const int *pfd_child_r, const int *pfd_child_w) {
dup2(pfd_child_r[1], STDOUT_FILENO);
dup2(pfd_child_w[0], STDIN_FILENO);
close_fd(pfd_child_r[0]);
close_fd(pfd_child_r[1]);
close_fd(pfd_child_w[0]);
close_fd(pfd_child_w[1]);
execlp("./v1_child", "./v1_child", NULL);
fprintf(stderr, "execution of v1_child failed\n");
exit(EXIT_FAILURE);
}
void create_pipe(int *fd) {
if (pipe(fd) == -1) {
perror("cannot pipe()");
exit(EXIT_FAILURE);
}
}
void close_fd(int fd) {
if (close(fd) == -1) {
perror("cannot close()");
exit(EXIT_FAILURE);
}
}
The corresponding v1_child.c could look like:
#include <stdio.h>
#include <stdlib.h>
int gcd(int n1, int n2) {
if (n2 == 0)
return n1;
return gcd(n2, n1 % n2);
}
int main(void) {
int n1, n2, result;
char buf[128];
while(fgets(buf, sizeof(buf), stdin)) {
if (sscanf(buf, "%d %d", &n1, &n2) != 2) {
fprintf(stderr, "error reading numbers in child\n");
return -1;
}
if (n1 > n2)
result = gcd(n1, n2);
else
result = gcd(n2, n1);
printf("%d\n", result);
fflush(stdout);
}
return EXIT_SUCCESS;
}
Test
With the input of
5 25
49 14
64 462
1155 84
the output would be
5 25 gcd: 5
49 14 gcd: 7
64 462 gcd: 2
1155 84 gcd: 21
You write binary values to the pipes, but try to read text representation. So either use a variation of printf (like dprintf) in the parent, or (better) use read (and write) in the child.
Related
I'm trying to use pipe command and I can't understand how to.
I've a lot of versions but I can't make it work.
first of all the hierarchy:
main prog - nicecmp - that will execute the child prog and print the result
child prog - loopcmp - that will execute his child prog and get the returned value and send it back to the parent in nicecmp.
loopcmp's childs - lencmp/lexcmp - both prog will be executed in loopcmp and return value between -1 to 2. (100% works)
shortly, I need to create a pipe and a new process that will run new program (loopcmp - added in the end of the code) using execvp, and I need to print the res of the loopcmp in the parent.
I can send it directly from the prog that I executed and I can use WEXITSTATUS in the child after the end of the loopcmp.
what's the right way to do so (from the progrem execution or after that I've returned from the loopcmp)
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LINELEN (80)
#define READFROM ("./loopcmp")
typedef enum { eLexcmp, eLencmp, eNumOfCmp } eCmpstr;
const char* cmpstr[eNumOfCmp] = { "./lexcmp", "./lencmp" };
int lencmp(const char *str1, const char *str2);
int lexcmp(const char *str1, const char *str2);
char *mygets(char *buf, int len);
int mygeti();
int main(int argc, char *argv[])
{
char str1[LINELEN + 1];
char str2[LINELEN + 1];
int index, rc, status, res;
int pfd[2];/* Pipe file descriptors */
if (pipe(pfd) == -1) /* Create pipe */
exit(-2); // pipe failed !
char* myargs[4];
myargs[0]=strdup(READFROM);
while (1)
{
printf("Please enter first string:\n");
if (mygets(str1, LINELEN) == NULL)
break;
printf("Please enter second string:\n");
if (mygets(str2, LINELEN) == NULL)
break;
myargs[2] = strdup(str1);
myargs[3] = strdup(str2);
do {
printf("Please choose:\n");
for (int i = 0; i < eNumOfCmp; i++)
printf("%d - %s\n", i, cmpstr[i]);
index = mygeti();
} while ((index < 0) || (index >= eNumOfCmp));
myargs[1] = strdup(cmpstr[index]);
rc = fork();
if (rc < 0) // fork failed !
{
printf("fork failed\n");
return -2;
}
else if (rc == 0) { // child !
if (close(pfd[1]) == -1) /* Write end is unused */
exit(-2);
/* Duplicate stdin on read end of pipe; close duplicated descriptor */
if (pfd[0] != STDIN_FILENO) { /* Defensive check */
if (dup2(pfd[0], STDIN_FILENO) == -1)
exit(-2);
if (close(pfd[0]) == -1)
exit(-2);
}
execvp(myargs[0],myargs);
}
else { // parent
if (close(pfd[1]) == -1) /* Write end is unused */
exit(-2);
/* Duplicate stdin on read end of pipe; close duplicated descriptor */
if (pfd[0] != STDIN_FILENO) { /* Defensive check */
if (dup2(pfd[0], STDIN_FILENO) == -1)
exit(-2);
if (close(pfd[0]) == -1)
exit(-2);
}
read(pfd[0], &res, sizeof(int));
printf("%d\n", res);
if (close(pfd[0]) == -1)
exit(-2);
}
}
return 0;
}
loopcmp ->
int main(int argc, char *argv[])
{
int status,rc,res = 0;
if (argc != 4)
{
return -1;
}
char* myargs[3];
for(int i=0;i<3;i++){
myargs[i]=argv[i+1];
}
rc = fork();
if (rc < 0) //fork failed
{
return -2;
}
else if (rc == 0) //I'm the child
{
if(execvp(myargs[1], myargs)==-1)
return -2;
}
else // parent
{
wait(&status);
res = WEXITSTATUS(status);
if(res ==254) // invalid file path ! (254== -2)
return -2 ;
}
write(fileno(stdout),&res,sizeof(int));
return res;
}
I am writing program in C on Linux which has to fork 2 children.
First child will send two random numbers over pipe to the second child. It will listen for SIGUSR1 signal and will then terminate.
The second child will duplicate(dup2) pipe input as STDIN and file fp as STDOUT. It will then execl program which will print out some data according to its input and end.
My problem is, that the execl'd program will never terminate and I don't know why. Any help or tips will be appreciated.
main.c (parent):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
const int BUFFER_SIZE = 30;
int pipefd[2] = {0,0};
int parent_pid = 0;
int first_pid = 0;
int second_pid = 0;
int sleep_time = 5;
int debug = 0;
FILE *fp;
void parent_func() {
int wstatus = 0;
sleep(sleep_time);
kill(first_pid, SIGUSR1);
wait(&wstatus);
waitpid(second_pid, &wstatus, 0);
}
static void sigusr1_handler(int sig) {
if (sig == SIGUSR1) {
fputs("TERMINATED", stderr);
close(pipefd[1]);
exit(0);
}
}
void first_func() {
struct sigaction act;
char buffer[BUFFER_SIZE];
close(pipefd[0]);
memset(&act, '\0', sizeof(act)); // clear the sigaction struct
act.sa_handler = &sigusr1_handler; // sets function to run on signal
if (sigaction(SIGUSR1, &act, NULL) < 0) { // assign sigaction
fputs("cannot assign sigaction - exiting...", stderr);
exit(1);
}
while (1) {
sprintf(buffer, "%d %d\n", rand(), rand());
write(pipefd[1], buffer, strlen(buffer));
puts(buffer);
sleep(1);
}
}
void second_func() {
close(pipefd[1]);
fp = fopen("out.txt", "w");
char buf[30];
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
//dup2(fileno(fp), STDOUT_FILENO);
execl("./test", "", NULL);
perror("Error");
}
int main(int argc, char *argv[]) {
int fork_val = 0;
parent_pid = getpid();
if (pipe(pipefd)) {
fputs("cannot create pipe - exiting...", stderr);
return 1;
}
if (debug) {
sleep_time *= 10;
}
if ((fork_val = fork()) == -1) {
fputs("cannot fork process - exiting...", stderr);
return 1;
} else if (fork_val == 0) {
first_func();
} else {
first_pid = fork_val;
if ((fork_val = fork()) == -1) {
fputs("cannot fork process - exiting...", stderr);
return 1;
} else if (fork_val == 0) {
second_func();
} else {
second_pid = fork_val;
parent_func();
}
}
fclose(fp);
exit(0);
}
test.c (the execl'd file):
#include "nd.h"
#include "nsd.h"
#include <stdio.h>
#include <stdlib.h>
int main() {
int num1 = 0;
int num2 = 0;
char buffer[100];
while (fgets(buffer, 100, stdin) != NULL) {
if (sscanf(buffer, "%d %d", &num1, &num2) == 2) {
(num1 < 0) ? num1 = (num1 * -1) : num1;
(num2 < 0) ? num2 = (num2 * -1) : num2;
if (num1 == 1 || num2 == 1) {
puts("1");
} else if (num1 == num2) {
if (nd(num1) == 1) {
puts("prime");
} else {
printf("%d\n", num1);
}
} else if (nd(num1) == 1 && nd(num2) == 1) {
puts("prime");
} else {
printf("%d\n", nsd(num1, num2));
}
} else {
fputs("error\n", stderr);
}
}
fputs("DONE", stderr);
exit(0);
}
To be able to detect an end of file from a pipe you need to read from a empty pipe with no writer (no process with an open for writing descriptor).
As your writer (first_func()) never closes its descriptor and always writes something in a never ending loop the reader will either wait for some data or read some data.
Be also careful about closing non useful descriptors, if not you may encounter some problems with pipes, such has a single process that is a reader and a writer, so being unable to detect the end of file...
The child_filter has to read values from pipefd and write these in a named pipe.
The problem is that if i try to un-comment the comment[3] (the open of the named-pipe) the function won't print values, it seem to be stuck on read() call. Instead, if i do not open the fifo pipe it works.
I need named pipe for other stuffs.
What shall i modify? Maybe pipe and named-pipe conflicts using them together?
Thanks.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFONAME "./my-fgrep-named-pipe"
typedef struct{
int i;
int v;
int word;
int filename;
char word_string[250];
char filename_string[250];
}parameters;
int pipefd[2];
void child_reader(parameters params){
FILE* fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
if(params.filename==0)
fp = stdin;
else
fp = fopen(params.filename_string, "r");
close(pipefd[0]); /* Close unused read end */
if (fp != NULL){
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
//printf("%s", line);
write(pipefd[1], line, strlen(line));
}
fclose(fp);
}
free(line);
printf("child reader > end\n");
exit(0);
}
void child_filter(parameters params){
char c;
char temp[250];
int i=0;
char *temp2;
int fifofd;
close(pipefd[1]); /* Close unused write pipe end */
printf("read from pipe\n");
if( (fifofd = open(FIFONAME, O_WRONLY)) == -1) printf("Error WW\n");
while (read(pipefd[0], &c, 1) > 0){
if (c == '\n' || c == '\r'){
temp[i] = '\n';
if(i>0){
temp2=strtok(temp, "\n");
//temp2[i] = '\n';
// printf("[%s]\n", temp2);
write(fifofd, temp2, strlen(temp2));
}i=0;
}
else{
temp[i] = c;
i++;
}
}
close(fifofd);
printf("child filter > end\n");
exit(0);
}
void child_writer(parameters params){
char c;
int fifofd;
char temp[250];
int i=0;
char *temp2;
if( (fifofd = open(FIFONAME, O_RDONLY)) == -1) printf("Error RR\n");
while (read(fifofd, &c, 1) > 0){
printf("entry > [%c] \n", c);
}
printf("exit-------------\n");
close(fifofd);
unlink(FIFONAME);
exit(0);
}
int main(int argc, char *argv[]){
char* temp1;
parameters params;
int forkResult;
params.i=0;
params.v=0;
params.word=0;
params.filename=0;
int pid_r, pid_w, pid_f;
if(argc<2){
printf("error\n");
exit(0);
}
if(strcmp(argv[1],"-i") == 0)
params.i++;
if(strcmp(argv[1],"-v") == 0)
params.v++;
if(argc>2){
if(strcmp(argv[2],"-i") == 0)
params.i++;
if(strcmp(argv[2],"-v") == 0)
params.v++;
}
if(params.i == 0 && params.v == 0){
params.word++;
strcpy(params.word_string, argv[1]);
if(argc>2){
params.filename++;
strcpy(params.filename_string, argv[2]);
}
}
else if(params.i != 0 && params.v != 0){
if(argc>3){
params.word++;
strcpy(params.word_string, argv[3]);
}
if(argc>4){
params.filename++;
strcpy(params.filename_string, argv[4]);
}
}
else{
if(argc>2){
params.word++;
strcpy(params.word_string, argv[2]);
}
if(argc>3){
params.filename++;
strcpy(params.filename_string, argv[3]);
}
}
printf("Result: i[%d], v[%d], name[%d], filename[%d]\n", params.i, params.v, params.word, params.filename);
if(params.word==0){
printf("Error X\n");
exit(0);
}
if (pipe(pipefd) == -1) {
printf("pipe error\n");
exit(0);
}
unlink(FIFONAME);
if( mkfifo(FIFONAME, 0666) != 0) printf("Error fifo1\n");
if( (pid_r=fork()) == 0 ){
child_reader(params);
}
if( (pid_f=fork()) == 0 ){
child_filter(params);
}
if( (pid_w=fork()) == 0 ){
child_writer(params);
}
waitpid(pid_r, NULL, 0);
printf("Reader finished\n");
close(pipefd[1]);
waitpid(pid_f, NULL, 0);
close(pipefd[0]);
printf("filter finished\n");
waitpid(pid_w, NULL, 0);
printf("Done!\n");
exit(0);
}
If you open a named pipe for writing then it'll block until the other end is opened for reading. That's an expected behaviour.
I need named pipe for other stuffs
Well, if there's no one reading from the pipe then what other stuff can you do with the write end of the pipe? So, you have to ensure there's a reader from the pipe or delay opening the pipe until there's someone ready to read from it. One other option is to open with O_RDWR.
The problem was that forks dupe file descriptors and so they were still opened.
Due to this reason, child process won't finish.
Fixed code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFONAME "./my-fgrep-named-pipe"
typedef struct{
int i;
int v;
int word;
int filename;
char word_string[250];
char filename_string[250];
}parameters;
int pipefd[2];
void child_reader(parameters params){
FILE* fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
if(params.filename==0)
fp = stdin;
else
fp = fopen(params.filename_string, "r");
close(pipefd[0]); /* Close unused read end */
if (fp != NULL){
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
//printf("%s", line);
write(pipefd[1], line, strlen(line));
}
fclose(fp);
}
free(line);
close(pipefd[1]); /* Close unused read end */
printf("child reader > done\n");
exit(0);
}
void child_filter(parameters params){
char c;
char temp[250];
int i=0;
char *temp2;
int fifofd;
close(pipefd[1]); /* Close unused write pipe end */
if( (fifofd = open(FIFONAME, O_WRONLY)) == -1) printf("Error fifoWW\n");
printf("read from pipe\n");
while (read(pipefd[0], &c, 1) > 0){
if (c == '\n' || c == '\r'){
temp[i] = '\n';
if(i>0){
temp2=strtok(temp, "\n");
//temp2[i] = '\n';
//printf("[%s]\n", temp2);
write(fifofd, temp2, strlen(temp2)); //prima senza +1;
}i=0;
}
else{
temp[i] = c;
i++;
}
}
close(fifofd);
close(pipefd[0]);
printf("child filter > done\n");
exit(0);
}
void child_writer(parameters params){
char c;
char temp[250];
int i=0;
char *temp2;
int size;
int fifofd;
if( (fifofd = open(FIFONAME, O_RDONLY)) == -1) printf("Error fifoRR\n");
do{
printf("entry> [%c] \n", c);
size = read(fifofd, &c, 1);
printf("next size read> %d\n", size);
}while(size > 0);
close(fifofd);
printf("exit-------------\n");
//unlink(FIFONAME);
exit(0);
}
int main(int argc, char *argv[]){
char* temp1;
parameters params;
int esitoFork;
params.i=0;
params.v=0;
params.word=0;
params.filename=0;
int pid_r, pid_w, pid_f;
FILE *myfifo;
if(argc<2){
printf("error \n");
exit(0);
}
if(strcmp(argv[1],"-i") == 0)
params.i++;
if(strcmp(argv[1],"-v") == 0)
params.v++;
if(argc>2){
if(strcmp(argv[2],"-i") == 0)
params.i++;
if(strcmp(argv[2],"-v") == 0)
params.v++;
}
if(params.i == 0 && params.v == 0){ // [3] ho il nome, [4] ho il filename
params.word++;
strcpy(params.word_string, argv[1]);
if(argc>2){
params.filename++;
strcpy(params.filename_string, argv[2]);
}
}
else if(params.i != 0 && params.v != 0){ // [2] ho il nome, [3] ho il filename
if(argc>3){
params.word++;
strcpy(params.word_string, argv[3]);
}
if(argc>4){
params.filename++;
strcpy(params.filename_string, argv[4]);
}
}
else{ // [3] ho il nome, [4] ho il filename
if(argc>2){
params.word++;
strcpy(params.word_string, argv[2]);
}
if(argc>3){
params.filename++;
strcpy(params.filename_string, argv[3]);
}
}
printf("Result: i[%d], v[%d], nome[%d], filename[%d]\n", params.i, params.v, params.word, params.filename);
if(params.word==0){
printf("Error syntax\n");
exit(0);
}
if (pipe(pipefd) == -1) {
printf("pipe error\n");
exit(0);
}
if( mkfifo(FIFONAME, 0666) != 0) printf("Error fifo\n");
if( (pid_r=fork()) == 0 ){
child_reader(params);
}
if( (pid_f=fork()) == 0 ){
child_filter(params);
}
close(pipefd[0]);
close(pipefd[1]);
if( (pid_w=fork()) == 0 ){
child_writer(params);
}
waitpid(pid_r, NULL, 0);
printf("Reader finished\n");
waitpid(pid_f, NULL, 0);
printf("filter finished\n");
waitpid(pid_w, NULL, 0);
printf("Done!\n");
unlink(FIFONAME);
exit(0);
}
I am trying to write a C program that uses pipes to send information between the parent and two children. The goal of the program is to achieve something similar to merge sort, for strings. I read the number of strings and then the Strings. The strings get divided between the 2 children, recursively until each child has only one string. I have to redirect the stdin of the child to read from the stdout of the parent.
For some reason none of the children read more than the first string.
How could I solve this problem?
int main(int argc, char * argv[]) {
int nrrows = 0;
char * buffer = NULL;
size_t n = 0;
getline(&buffer, &n, stdin);
char * endptr;
nrrows = strtol(buffer, &endptr, 10);
char rows[nrrows][MAX_LEN];
int i = 0;
n = 0;
while(i < nrrows) {
char * row = NULL;
getline(&row, &n, stdin);
strcpy(rows[i], row);
i++;
}
if(nrrows == 1) {
fprintf(stderr, "%s", rows[0]);
return 0;
}
int fdcp1[2];
int fdcp2[2];
if(pipe(fdcp1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if(pipe(fdcp2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
pid_t chpid1 = fork();
if(chpid1 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
else if(chpid1 == 0) {
close(fdcp2[0]);
close(fdcp2[1]);
close(fdcp1[1]);
dup2(fdcp1[0], STDIN_FILENO);
execlp("./forksort", "child1", NULL);
}else {
close(fdcp1[0]);
dup2(fdcp1[1], STDOUT_FILENO);
double half = (nrrows / 2);
int h = half;
char b[2];
b[0] = '0' + h;
b[1] = '\n';
write(fdcp1[1], b, sizeof(b));
for(i = 0; i < h; i ++) {
rows[i][strlen(rows[i])] = '\0';
write(fdcp1[1], rows[i], sizeof(rows[i]));
}
pid_t chpid2 = fork();
if(chpid2 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}else if(chpid2 == 0) {
close(fdcp1[0]);
close(fdcp1[1]);
close(fdcp2[1]);
dup2(fdcp2[0], STDIN_FILENO);
execlp("./forksort", "child2", NULL);
}else {
close(fdcp2[0]);
dup2(fdcp2[1], STDOUT_FILENO);
half = (nrrows / 2);
h = half;
char b[2];
b[0] = '0' + (nrrows - h);
b[1] = '\n';
write(fdcp2[1], b, sizeof(b));
for(i = h; i < nrrows; i ++) {
rows[i][strlen(rows[i])] = '\0';
write(fdcp2[1], rows[i], sizeof(rows[i]));
}
}
}
return 0;
}
It's bad news to modify a file descriptor that is associated with an open stream. I would account it highly likely to cause you trouble, and there is, moreover, no need to do that here. The parent should instead use fdopen() to open new streams on top of its ends of the pipes, and conduct I/O with its children via those instead of via the standard streams. In addition to being safer, that leaves the process's original standard streams available for it to communicate with its parent process.
With that approach, you could even stream the strings to be sorted back and forth among the processes, instead of redundantly buffering blocks of them in each process's memory. For instance, you might do something like this:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char * argv[]) {
char * buffer = NULL;
size_t buflen = 0;
int nrrows;
int fdpc1[2];
int fdcp1[2];
int fdpc2[2];
int fdcp2[2];
pid_t chpid1;
pid_t chpid2;
FILE *pipeout;
FILE *pipein1;
FILE *pipein2;
int half;
int i;
fprintf(stderr, "%s!!!!!!!!!!!!!!!!!\n", argv[0]);
getline(&buffer, &buflen, stdin);
fprintf(stderr, "number: %s from %s\n", buffer, argv[0]);
nrrows = strtol(buffer, NULL, 10);
if(nrrows <= 0) {
fprintf(stderr, "This is not a valid >0 number\n");
return EXIT_FAILURE;
} else if (nrrows == 1) {
/* ... read and echo back the one row ... */
getline(&buffer, &buflen, stdin);
fprintf(stderr, "%s", buffer);
return EXIT_SUCCESS;
}
/* There are at least two rows to sort */
if (pipe(fdcp1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if (pipe(fdpc1) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
chpid1 = fork();
if (chpid1 == 0) {
/* this is child process 1 */
close(fdcp1[1]);
close(fdpc1[0]);
dup2(fdcp1[0], STDIN_FILENO);
close(fdcp1[0]);
dup2(fdpc1[1], STDOUT_FILENO);
close(fdpc1[1]);
execlp("./forksort", "child1", NULL);
} else if (chpid1 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
/* this is the parent process */
close(fdcp1[0]);
close(fdpc1[1]);
if (pipe(fdcp2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
if (pipe(fdpc2) < 0) {
fprintf(stderr, "pipe unsuccessfull\n");
return EXIT_FAILURE;
}
chpid2 = fork();
if (chpid2 == 0) {
/* this is child process 2 */
close(fdcp1[1]);
close(fdpc1[0]);
close(fdcp2[1]);
close(fdpc2[0]);
dup2(fdcp2[0], STDIN_FILENO);
close(fdcp2[0]);
dup2(fdpc2[1], STDOUT_FILENO);
close(fdpc2[1]);
execlp("./forksort", "child2", NULL);
} else if (chpid2 < 0) {
fprintf(stderr, "fork unsuccessfull\n");
return EXIT_FAILURE;
}
/* this is the parent process */
close(fdcp2[0]);
close(fdpc2[1]);
/* copy the first half of the lines from input to child 1 */
pipeout = fdopen(fdcp1[1], "w");
if (pipeout == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
half = nrrows / 2;
fprintf(pipeout, "%d\n", half);
for (i = 0; i < half; i += 1) {
getline(&buffer, &buflen, stdin);
fprintf(stderr,"row[%d] from %s: %s", i, argv[0], buffer);
fputs(buffer, pipeout);
}
fclose(pipeout);
/* copy the second half of the lines from input to child 2 */
pipeout = fdopen(fdcp2[1], "w");
if (pipeout == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
fprintf(pipeout, "%d\n", nrrows - half);
for (; i < nrrows; i += 1) {
getline(&buffer, &buflen, stdin);
fprintf(stderr,"row[%d] from %s: %s", i, argv[0], buffer);
fputs(buffer, pipeout);
}
fclose(pipeout);
/* now read and merge sorted lines from the children */
pipein1 = fdopen(fdpc1[0], "r");
pipein2 = fdopen(fdpc2[0], "r");
if (pipein1 == NULL || pipein2 == NULL) {
fprintf(stderr, "fdopen unsuccessful\n");
return EXIT_FAILURE;
}
/* ... */
fclose(pipein1);
fclose(pipein2);
return 0;
}
I read the following file (file.txt) line by line:
1
-5
6
-8
-33
21
The father sends negative numbers to a process, and sends positive numbers to a second process:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void Fils1(int *tube1)
{
int n;
int cpt = 0;
close (tube1[1]);
while (read (tube1[0], &n, 1) >0)
{
cpt+=n;
}
printf("Son 1, count : %i \n", cpt);
exit (1) ;
}
void Fils2(int *tube2)
{
int n;
int cpt = 0;
close (tube2[1]);
while (read (tube2[0], &n, 1) >0)
{
cpt+=n;
}
printf("Son 2, count : %i \n", cpt);
exit (1) ;
}
int main(int argc, char *argv[])
{
FILE* file;
int n;
file = fopen (argv[1], "r");
if (file == NULL){
printf("Error open file %s\n", argv[1]);
exit(1);
}
int tube1[2];
int tube2[2];
if (pipe(tube1) != 0)
{
fprintf(stderr, "Error tube 1\n");
return EXIT_FAILURE;
}
if (pipe(tube2) != 0)
{
fprintf(stderr, "Error tube 2\n");
return EXIT_FAILURE;
}
int pid1 = fork();
if(pid1 == 0)
{
printf("Creation of the first son ! \n");
Fils1 (tube1);
}
else
{
int pid2 = fork();
if(pid2 == 0)
{
printf("Creation of the second son ! \n");
Fils2 (tube2);
}
else
{
printf("I'm the father! \n");
close (tube1[0]);
close (tube2[0]);
while (!feof(file))
{
fscanf (file,"%d",&n);
if (n>0)
{
write (tube1[1], &n, 1);
}
else
{
write (tube2[1], &n, 1);
}
}
fclose (file);
if(wait(NULL) == -1)
{
printf("Error wait()\n");
exit(1);
}
}
}
return EXIT_SUCCESS;
}
Each son is counting, and displays it on screen.
When I execute, I have only that :
I'm the father!
Creation of the first son!
Creation of the second son!
When I expect also
Son1, count : 28
Son2, count : 46
The problem is you're not closing the pipes as you should.
Child1 should close tube2 (both ends)
Child2 should close tube1 (both ends);
The parent should close write end (after the while)