Sponsored Content
Full Discussion: fork problem
Top Forums Programming fork problem Post 96892 by qntmteleporter on Tuesday 24th of January 2006 12:42:43 PM
Old 01-24-2006
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 born.\n");
exit (0);
default:
fprintf(stdout, "From parent: Child %d has ID %d.\n", i+1, pidp);
}
}

exit(0);
}

I understand that this program is not protected while it is writing to stdout. Therefore, certain inconsistencies are bound to happen. But I am not able to comprehend the following observation.

When I run this program, the output is on the standard output and is the following (as expected):

From child: I am born.
From parent: Child 1 has ID 6412.
From parent: Child 2 has ID 6413.
From child: I am born.
From child: I am born.
From parent: Child 3 has ID 6414.
From child: I am born.
From parent: Child 4 has ID 6415.

However, when the output is redirected to a file (even if "tee" is used), the output has a lot of duplicated lines:

From child: I am born.
From parent: Child 1 has ID 6454.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From parent: Child 3 has ID 6456.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From child: I am born.
From parent: Child 1 has ID 6454.
From parent: Child 2 has ID 6455.
From parent: Child 3 has ID 6456.
From parent: Child 4 has ID 6457.

I figure that it is due to the stdout stream becuase using fflush before the print statements in the code fixes this inconsistency.

But could somebody explain what's exactly happening?

regards,
Quantum Teleporter!
 

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

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

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

5. Programming

Fork and then exec problem with signals

Hi All, In my program i am handling SIGHUP signal. In the handler i fork and then exec on child process same binary file which is running. Parent process will die after 10 mins. Now my child process which was exec with same binary file is not receiving SIGHUP signal. Below is the progran code:... (6 Replies)
Discussion started by: sushil_shalin
6 Replies

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

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

8. Programming

Problem with fork() and execlp process

Hello everyone, this is my first post. I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers. The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to... (3 Replies)
Discussion started by: Johnathan_1017
3 Replies

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

10. 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
ct_pr_event_get_pid(3CONTRACT)			       Contract Management Library Functions			    ct_pr_event_get_pid(3CONTRACT)

NAME
ct_pr_event_get_pid, ct_pr_event_get_ppid, ct_pr_event_get_signal, ct_pr_event_get_sender, ct_pr_event_get_senderct, ct_pr_event_get_exit- status, ct_pr_event_get_pcorefile, ct_pr_event_get_gcorefile, ct_pr_event_get_zcorefile - process contract event functions SYNOPSIS
cc [ flag... ] file... -D_LARGEFILE64_SOURCE -lcontract [ library... ] #include <libcontract.h> #include <sys/contract/process.h> int ct_pr_event_get_pid(ct_evthdl_t evthdl, pid_t *pidp); int ct_pr_event_get_ppid(ct_evthdl_t evthdl, pid_t *pidp); int ct_pr_event_get_signal(ct_evthdl_t evthdl, int *signalp); int ct_pr_event_get_sender(ct_evthdl_t evthdl, pid_t *pidp); int ct_pr_event_get_senderct(ct_evthdl_t evthdl, ctid_t *pidp); int ct_pr_event_get_exitstatus(ct_evthdl_t evthdl, int *statusp); int ct_pr_event_get_pcorefile(ct_evthdl_t evthdl, char **namep); int ct_pr_event_get_gcorefile(ct_evthdl_t evthdl, char **namep); int ct_pr_event_get_zcorefile(ct_evthdl_t evthdl, char **namep); DESCRIPTION
These functions read process contract event information from an event object returned by ct_event_read(3CONTRACT) or ct_event_read_criti- cal(3CONTRACT). The ct_pr_event_get_pid() function reads the process ID of the process generating the event. The ct_pr_event_get_ppid() function reads the process ID of the process that forked the new process causing the CT_PR_EV_FORK event. The ct_pr_event_get_signal() function reads the signal number of the signal that caused the CT_PR_EV_SIGNAL event. The ct_pr_event_get_sender() function reads the process ID of the process that sent the signal that caused the CT_PR_EV_SIGNAL event. If the signal's sender was not in the same zone as the signal's recipient, this information is available only to event consumers in the global zone. The ct_pr_event_get_senderct function reads the contract ID of the process that sent the signal that caused the CT_PR_EV_SIGNAL event. If the signal's sender was not in the same zone as the signal's recipient, this information is available only The ct_pr_event_get_exitstatus() function reads the exit status of the process generating a CT_PR_EV_EXIT event. The ct_pr_event_get_pcorefile() function reads the name of the process core file if one was created when the CT_PR_EV_CORE event was gener- ated. A pointer to a character array is stored in *namep and is freed when ct_event_free(3CONTRACT) is called on the event handle. The ct_pr_event_get_gcorefile() function reads the name of the zone's global core file if one was created when the CT_PR_EV_CORE event was generated. A pointer to a character array is stored in *namep and is freed when ct_event_free() is called on the event handle. The ct_pr_event_get_zcorefile() function reads the name of the system-wide core file in the global zone if one was created when the CT_PR_EV_CORE event was generated. This information is available only to event consumers in the global zone. A pointer to a character array is stored in *namep and is freed when ct_event_free() is called on the event handle. RETURN VALUES
Upon successful completion, ct_pr_event_get_pid(), ct_pr_event_get_ppid(), ct_pr_event_get_signal(), ct_pr_event_get_sender(), ct_pr_event_get_senderct(), ct_pr_event_get_exitstatus(), ct_pr_event_get_pcorefile(), ct_pr_event_get_gcorefile(), and ct_pr_event_get_zcorefile() return 0. Otherwise, they return a non-zero error value. ERRORS
The ct_pr_event_get_pid(), ct_pr_event_get_ppid(), ct_pr_event_get_signal(), ct_pr_event_get_sender(), ct_pr_event_get_senderct(), ct_pr_event_get_exitstatus(), ct_pr_event_get_pcorefile(), ct_pr_event_get_gcorefile(), and ct_pr_event_get_zcorefile() functions will fail if: EINVAL The evthdl argument is not a process contract event object. The ct_pr_event_get_ppid(), ct_pr_event_get_signal(), ct_pr_event_get_sender(), ct_pr_event_get_senderct(), ct_pr_event_get_exitstatus(), ct_pr_event_get_pcorefile(), ct_pr_event_get_gcorefile(), and ct_pr_event_get_zcorefile() functions will fail if: EINVAL The requested data do not match the event type. The ct_pr_event_get_sender()a functions will fail if: ENOENT The process ID of the sender was not available, or the event object was read by a process running in a non-global zone and the sender was in a different zone. The ct_pr_event_get_pcorefile(), ct_pr_event_get_gcorefile(), and ct_pr_event_get_zcorefile() functions will fail if: ENOENT The requested core file was not created. The ct_pr_event_get_zcorefile() function will fail if: ENOENT The event object was read by a process running in a non-global zone. EXAMPLES
Example 1 Print the instigator of all CT_PR_EV_SIGNAL events. Open the process contract bundle. Loop reading events. Fetch and display the signalled pid and signalling pid for each CT_PR_EV_SIGNAL event encountered. #include <sys/types.h> #include <fcntl.h> #include <stdio.h> #include <libcontract.h> ... int fd; ct_evthdl_t event; pid_t pid, sender; fd = open("/system/contract/process/bundle", O_RDONLY); for (;;) { ct_event_read(fd, &event); if (ct_event_get_type(event) != CT_PR_EV_SIGNAL) { ct_event_free(event); continue; } ct_pr_event_get_pid(event, &pid); if (ct_pr_event_get_sender(event, &sender) == ENOENT) printf("process %ld killed by unknown process ", (long)pid); else printf("process %ld killed by process %ld ", (long)pid, (long)sender); ct_event_free(event); } ... ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |Safe | +-----------------------------+-----------------------------+ SEE ALSO
ct_event_free(3CONTRACT), ct_event_read(3CONTRACT), ct_event_read_critical(3CONTRACT), libcontract(3LIB), contract(4), process(4), attributes(5), lfcompile(5) SunOS 5.11 19 Jul 2004 ct_pr_event_get_pid(3CONTRACT)
All times are GMT -4. The time now is 01:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy