[Solved] Problem with fork() while reading files


 
Thread Tools Search this Thread
Top Forums Programming [Solved] Problem with fork() while reading files
# 1  
Old 05-24-2013
[Solved] Problem with fork() while reading files

Good evening everyone. I have my finals and I'm facing a problem:

I have a for cycle that is supposed to fork 2 children but somehow it forks only the first one.

What am I doing wrong ?
Code:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#define MAX 255

// ./conta_caratteri c1 c2 N file_in file_out //

void contare(char *file);
int n,a, status, terminazione;
char *file_in, *file_out;

int main(int argc, char *argv[])
{


int send, pid[MAX],fd, i;
file_in = argv[1];

i=0;
for(i=0; i<2; i++);
{
    pid[i]=fork();
    if ( pid[i] == 0 )
        {
        contare(file_in);
        exit(0);
        }
    if ( pid > 0 )
    {
        printf(" Creato figlio %d \n ", pid[i]);
        for ( i=0; i<2; i++ )
            {
            terminazione = wait(&status);
            if (terminazione == 0)
            printf("Figlio non terminato bene. %d \n", status);
            else
            printf("Figlio %d terminato %d \n", terminazione, status);
            }    
    }    
}




return;
}


void contare(char *file)
{
printf("%c \n\n", a);

int fd = open(file, O_RDONLY);
if ( fd < 0 )
    {
    perror("Failure nell apertura");
    exit(EXIT_FAILURE);
    }
else
    printf("File %s aperto. \n", file_in);
    close(fd);
return;
}

So, the program is pretty simple: I'm supposed to call the program ./program file_name
and its supposed to fork 2 children that will open the same file (argv[1] = file_name ). But as far as I can see I'm missing something because the program forks only one child.

Help ?
# 2  
Old 05-24-2013
Code:
for(i=0; i<2; i++);

The ; after the for makes it independent of the code block after it. It counts twice, then after it finishes, execute the code block after it once.

Remove the unneeded ;
# 3  
Old 05-24-2013
Found the problem! It wasnt the ; but the wait cycle!

Thank you all!
# 4  
Old 05-28-2013
No, the ; is definitely a problem too. The loop below it will be run once and only once, instead of twice like you wanted.
# 5  
Old 05-28-2013
Quote:
Originally Posted by pfpietro
Code:
            terminazione = wait(&status);
            if (terminazione == 0)
            printf("Figlio non terminato bene. %d \n", status);
            else
            printf("Figlio %d terminato %d \n", terminazione, status);

In case you have not realized it, you are using terminazione incorrectly. It will never be zero since wait returns a pid on success (for which you should use pid_t, not int) and -1 on errors. This means that the else-clause is always executed.

Also, status is not the exit status of the process unless the process exited normally and returned 0. Otherwise, because status encodes information other than a simple exit status, the standardized macros must be used to inspect status.

I suggest that you carefully read your wait manual page.

Perhaps the time it took me to highlight these problems was wasted, because you may have already fixed them. I dont know because you did not post your solution. Please dont be selfish or inconsiderate. When you solve your problem, post your solution so that novices can benefit from what you have learned (and so experts can help further if there are still issues pending).

Regards,
Alister

Last edited by alister; 05-28-2013 at 05:55 PM..
# 6  
Old 05-29-2013
Thanks guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Need Help in reading Response file

Hi All, I have a requirement to read response file which looks like below Ex: NAME=SAM DOB=01/01/1980 ADDRESS= 7658 James Street NewYork 0000 Now, I need to take NAME, DOB, ADDRESS into variables I am fine taking NAME and DOB I need help on how can I... (6 Replies)
Discussion started by: mallak
6 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Reading Array And Send An Email

I am trying to find an example for reading an array instead of reading a file and send out an email in ksh. Can you please help is that possible? Algorithm #!/bin/ksh i=0 set -A ARR if then let i=$ ARR="A does n't match with B" fi if then let i=$ ARR="P does n't match with Q"... (11 Replies)
Discussion started by: Ariean
11 Replies

3. UNIX for Dummies Questions & Answers

Problem with fork() while reading files

Good evening everyone. I have my finals and I'm facing a problem: I have a for cycle that is supposed to fork 2 children but somehow it forks only the first one. What am I doing wrong ? #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>... (1 Reply)
Discussion started by: pfpietro
1 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Problem with listing my files

Hello, In my directory i have got a list of files like below unix1a.csv unix1b.csv unix1c.csv unix1d.csv unix1y.csv I have done ls -lrt unix1.csv, how can i get my unix1y.csv also get listed along with this.. (5 Replies)
Discussion started by: sathyaonnuix
5 Replies

5. Shell Programming and Scripting

[Solved] Reading the last word in a file from a script

Hello everybody, My first time here and my english is not very good I hope you understand me. I'm trying to read a file that contains two zip archive names. Here my file content is: package1.zip package2.zip At the end of the line there is a \n character. I read this file from a... (2 Replies)
Discussion started by: Aurea
2 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Programming

solved: glibc detection corruption using a fork in popen

Hi, I am having a hell of a time getting this to work. So basically, I have opened a popen to run a program that is going to prompt an action to occur half way through, when it gets to this I need to create a separate process and do some stuff, then return to the original process. This works... (0 Replies)
Discussion started by: imrank27
0 Replies

8. Shell Programming and Scripting

HELP!!!! Problem reading in files

HI, I have wrote a script which reads in a file that has a list of files within it. The script then searches the directory for these files and when found will output to an email with a layout table indicating the date of the file and an ok status if the file = todays date if not a waiting... (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

9. Programming

fork() problem

i'm just trying to make 2 process read from the same 1 line a time. For some reason only the child reads. #include<stdio.h> #include <sys/types.h> void getlinefromfilep(void); void getlinefromfilec(void); int see=0; FILE * fileptr1; //need globe variable to tell pro3 to stop main()... (3 Replies)
Discussion started by: ddx08
3 Replies
Login or Register to Ask a Question