Problem with fork() while reading files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with fork() while reading files
# 1  
Old 05-24-2013
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
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Very basic problem with fork() using c

Hi guys, I have the following code: int main(int argc, char *argv) { int pid1,pid2,i=0; pid1=fork(); i+=2; if(!pid1) i++; if(i%3) pid2=fork(); if (pid2==0) { printf("sea \n "); i-=1; } if(i>=2)... (4 Replies)
Discussion started by: pfpietro
4 Replies

2. Programming

[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 ? #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>... (5 Replies)
Discussion started by: pfpietro
5 Replies

3. Programming

problem with mutltiple fork()

Hi, can someone please help me with creating mutltiple fork.. I was expecting something like this: I am a child: 1 PID: 1215 I am a child: 2 PID: 1216 I am a child: 3 PID: 1217 I am a child: 4 PID: 1218 I am a child: 5 PID: 1219 I am a child: 6 PID: 1215 I am a child: 7 PID: 1216 I am a... (4 Replies)
Discussion started by: Placenzo
4 Replies

4. Programming

help in C of fork() problem

i am a beginner of C, and i tired to fork multiple child and all of them belongs to same parents and each of child responsible for printing individual data. but i don't have any idea how to do...... Can any body help? thanks a lot really. (7 Replies)
Discussion started by: wendy1089
7 Replies

5. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

6. Programming

problem implementing fork

Hi, I was honing my linux programming skill when this nuisance started bugging me. I wanted to create an empty file creator program. While creating a large file it must print # for progress bar. But the output shows it happening reverse way. ie. first it copies file and shows the progress... (7 Replies)
Discussion started by: dheerajsuthar
7 Replies

7. UNIX for Advanced & Expert Users

Problem due to Fork Error

Hi, I have developed a datastage job...which has many process running in parallel..but because of Fork Error my job is not working:( Can any body help me out to solve this Fork error problem.:rolleyes: My Os is SUNOS. IS there any setting in Unix through admin where in if i set some paramter... (8 Replies)
Discussion started by: Amey Joshi
8 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

10. Programming

fork problem

Hi, Consider the following piece of code: int main(void) { int i; pid_t pidp; for (i=0;i<4;i++) { switch (pidp=fork()) { case -1: fprintf(stdout, "Error during fork.\n"); exit (1); case 0: fprintf(stdout, "From child: I am... (4 Replies)
Discussion started by: qntmteleporter
4 Replies
Login or Register to Ask a Question