Sponsored Content
Top Forums Programming [Solved] Problem with fork() while reading files Post 302811811 by pfpietro on Friday 24th of May 2013 09:05:50 AM
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 ?
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
PTHREAD_ATFORK(3)					   BSD Library Functions Manual 					 PTHREAD_ATFORK(3)

NAME
pthread_atfork -- register handlers to be called when process forks LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <pthread.h> int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); DESCRIPTION
The pthread_atfork() function registers the provided handler functions to be called when the fork(2) function is called. Each of the three handlers is called at a different place in the fork(2) sequence. The prepare handler is called in the parent process before the fork hap- pens, the parent handler is called in the parent process after the fork has happened, and the child handler is called in the child process after the fork has happened. The parent and child handlers are called in the order in which they were registered, while the prepare handlers are called in reverse of the order in which they were registered. Any of the handlers given may be NULL. The intended use of pthread_atfork() is to provide a consistent state to a child process from a multithreaded parent process where locks may be acquired and released asynchronously with respect to the fork(2) call. Each subsystem with locks that are used in a child process should register handlers with pthread_atfork() that acquires those locks in the prepare handler and releases them in the parent handler. RETURN VALUES
The pthread_atfork() function returns 0 on success and an error number on failure. ERRORS
The following error code may be returned: [ENOMEM] Insufficient memory exists to register the fork handlers. SEE ALSO
fork(2) STANDARDS
The pthread_atfork() function conforms to IEEE Std 1003.1c-1995 (``POSIX.1''). HISTORY
The pthread_atfork() function first appeared in NetBSD 2.0. CAVEATS
After calling fork(2) from a multithreaded process, it is only safe to call async-signal-safe functions until calling one of the exec(3) functions. The pthread_*() functions are not async-signal-safe, so it is not safe to use such functions in the child handler. BUGS
There is no way to unregister a handler registered with pthread_atfork(). BSD
February 12, 2003 BSD
All times are GMT -4. The time now is 09:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy