Sponsored Content
Top Forums Programming Issue when fork()ing processes Post 302694415 by pfpietro on Thursday 30th of August 2012 03:41:02 PM
Old 08-30-2012
Issue when fork()ing processes

Hi guys!
I'll simplify my problem. I have the following code:

Code:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define max 25
#define buffdim 50

void p1();
void p2();
void wait_child();
void done(int sinal);

int main(int argc, char** argv)

{
int pid[1], i,j;

    for (i = 0; i < 2; i++)
    {

        pid[i] = fork();

        if(pid[i]==0)
        {
            if(i==0)
            {
            printf("I'm %d and i=0\n", pid[i]);
            p1();
            }
            else if (i==1)
            {
            printf("I'm %d and i=1\n", pid[i]);
            p2(argv[1]);
            }
        }
        else
        {
        printf("P0 : Created child %d\n", i + 1,pid[i]);
        }
    }
    for (j=0; j<2; j++)
    {
    wait_child();
    }

    return 0;
}

void p1()
{
    signal(SIGUSR1, done);
}

void p2(char* file_in)
{
int fd;
int a=8, c=10;

    
    printf("%d e %d.\n", a,c);
    exit(0);
}    


void wait_child()
{
    int death, status;
    death=wait(&status);
    printf("Child %d dead. Status %d.\n", death, status);
}

void done(int sinal)
{
    printf("Done\n");
}

In my head I created a for cicle that supposed to fork 2 sub processes. But, intstead of it it forks 3!!

I putted all these printf's in the middle of the for cicle to understand what he was doing, but even with the print'fs I wasn't able to do so.

what I get running the program is:

Code:
piero@ubuntu:~$ ./correggi test_input a
P0: Created child 1
I'm 0 and i=0
P0: Created child 2
P0: Created child 2
I'm 0 and i=1
8 e 10.
Child 2457 dead. Status 0.
I'm 0 and i=1
8 e 10.
Child 2458 dead. Status 0.
Child -1 dead. Status 0.
Child 2456 dead. Status 0.
piero@ubuntu:~$

Could anyone explain me what is going on ? why does he forks a 3rd process and a -1 process ??? Is the sub process getting inside the for cicle and forking someone else ?
 

10 More Discussions You Might Find Interesting

1. Programming

fork()ing hell!!

Hello I am having serious trouble with the fork command, i basically want to create 9 or 10 child processes and store their pid numbers in array while the children stay resident until i kill() them later , i cannot seem to control how many are made as they all seem to create their own children. ... (16 Replies)
Discussion started by: theultimatechuf
16 Replies

2. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

3. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

4. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

5. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

6. UNIX for Dummies Questions & Answers

Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better. After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in... (1 Reply)
Discussion started by: uiop44
1 Replies

7. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

8. Shell Programming and Scripting

fork processes

Hi, How to count how many processes opened by fork function in perl. Thanks (10 Replies)
Discussion started by: Anjan1
10 Replies

9. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

10. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies
CLOCK_GETCPUCLOCKID(3)					     Linux Programmer's Manual					    CLOCK_GETCPUCLOCKID(3)

NAME
clock_getcpuclockid - obtain ID of a process CPU-time clock SYNOPSIS
#include <time.h> int clock_getcpuclockid(pid_t pid, clockid_t *clock_id); Link with -lrt (only for glibc versions before 2.17). Feature Test Macro Requirements for glibc (see feature_test_macros(7)): clock_getcpuclockid(): _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L DESCRIPTION
The clock_getcpuclockid() function obtains the ID of the CPU-time clock of the process whose ID is pid, and returns it in the location pointed to by clock_id. If pid is zero, then the clock ID of the CPU-time clock of the calling process is returned. RETURN VALUE
On success, clock_getcpuclockid() returns 0; on error, it returns one of the positive error numbers listed in ERRORS. ERRORS
ENOSYS The kernel does not support obtaining the per-process CPU-time clock of another process, and pid does not specify the calling process. EPERM The caller does not have permission to access the CPU-time clock of the process specified by pid. (Specified as an optional error in POSIX.1-2001; does not occur on Linux unless the kernel does not support obtaining the per-process CPU-time clock of another process.) ESRCH There is no process with the ID pid. VERSIONS
The clock_getcpuclockid() function is available in glibc since version 2.2. CONFORMING TO
POSIX.1-2001. NOTES
Calling clock_gettime(2) with the clock ID obtained by a call to clock_getcpuclockid() with a pid of 0, is the same as using the clock ID CLOCK_PROCESS_CPUTIME_ID. EXAMPLE
The example program below obtains the CPU-time clock ID of the process whose ID is given on the command line, and then uses clock_get- time(2) to obtain the time on that clock. An example run is the following: $ ./a.out 1 # Show CPU clock of init process CPU-time clock for PID 1 is 2.213466748 seconds Program source #define _XOPEN_SOURCE 600 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { clockid_t clockid; struct timespec ts; if (argc != 2) { fprintf(stderr, "%s <process-ID> ", argv[0]); exit(EXIT_FAILURE); } if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) { perror("clock_getcpuclockid"); exit(EXIT_FAILURE); } if (clock_gettime(clockid, &ts) == -1) { perror("clock_gettime"); exit(EXIT_FAILURE); } printf("CPU-time clock for PID %s is %ld.%09ld seconds ", argv[1], (long) ts.tv_sec, (long) ts.tv_nsec); exit(EXIT_SUCCESS); } SEE ALSO
clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2012-10-29 CLOCK_GETCPUCLOCKID(3)
All times are GMT -4. The time now is 12:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy