vfork() error

A

asit

#include <stdio.h>
#include <unistd.h>

int glob = 6;

int main() {
int var;
pid_t pid;
var = 88;
printf("before vfork\n");
if((pid=vfork())<0) {
printf("vfork error");
}
else if(pid==0) {
glob ++;
var ++;

}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
return 0;
}

In the above code, why the o/p is garbage for the variable var
followed by segmentation fault ??
 
J

jameskuyper

asit said:
#include <stdio.h>
#include <unistd.h>

int glob = 6;

int main() {
int var;
pid_t pid;
var = 88;
printf("before vfork\n");
if((pid=vfork())<0) {

<unistd.h>, pid_t, and vfork() are all specific to Unix systems; as
such you're likely to get better answers to your question on
comp.unix.programmer
printf("vfork error");
}
else if(pid==0) {
glob ++;
var ++;

}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
return 0;
}

In the above code, why the o/p is garbage for the variable var
followed by segmentation fault ??

It may have something to do with the following (from the man page for
vfork on my desktop machine):

"... the behaviour is undefined if the process created by vfork()
either modifies any data other than a variable of type pid_t used to
store the return value from vfork(), or returns from the function in
which vfork() was called, or calls any other function before
successfully calling _exit() or one of the exec family of functions."

As I read it, that makes both the second printf() call and the return
statement problematic.
 
A

asit

"... the behaviour is undefined if the process created  by  vfork()
either modifies any data other than a variable of type pid_t used to
store the return value from vfork(), or returns from the  function  in
which vfork() was called, or calls any other function before
successfully calling _exit() or one of the exec family of  functions."

It doesn't modifies any data...it only accesses it
 
J

jameskuyper

jameskuyper said:
<unistd.h>, pid_t, and vfork() are all specific to Unix systems; as
such you're likely to get better answers to your question on
comp.unix.programmer


It may have something to do with the following (from the man page for
vfork on my desktop machine):

"... the behaviour is undefined if the process created by vfork()
either modifies any data other than a variable of type pid_t used to
store the return value from vfork(), or returns from the function in
which vfork() was called, or calls any other function before
successfully calling _exit() or one of the exec family of functions."

As I read it, that makes both the second printf() call and the return
statement problematic.

It doesn't modifies any data...it only accesses it

Yes, but as I've already pointed out, that's not the relevant part of
that clause. The first relevant problem is that it calls a function
that is neither _exit() nor any member of the exec family of
functions. Even if that didn't render the behavior undefined, your
code also "returns from the function in which vfork() was called", so
either way, the behavior is undefined.

As I indicated before, you'll get better answers to this question from
Unix experts like the ones you'll find at comp.unix.programmer. This
message is cross-posted to that newsgroup, and follow-ups are re-
directed there.
 
D

David Schwartz

[snip]
        if((pid=vfork())<0) {
                printf("vfork error");

Your question is not *about* the C language and is not appropriate for
comp.lang.c. Your question would not be appropriate on an English
language group either, even though you asked it in English.

In any event, the answer to your question is simple -- 'vfork' cannot
be used the way you used it. The 'vfork' function is an optimized
'fork' that can be used in certain special cases where 'fork' may do
extra work that is not needed. Your code does not fall into those
special cases and so cannot use 'vfork'. You must use 'fork'.

Fortunately, the optimizations are not very important anymore anyway.
Massive effort has gone into optimizing 'fork'. So you can consider
'vfork' essentially obsolete.

Specifically, you are not permitted to do what you are specifically
trying to do. It appears you are deliberately trying to use 'vfork' in
a way that causes it to have different behavior from 'fork' by
specifically violating the constraints 'vfork' imposes.
Unsurprisingly, it doesn't work the way you expect when you break the
rules.

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top